Adds the Slugify service.
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
# Register our services
|
||||||
|
services:
|
||||||
|
cdesai_tff.slugify:
|
||||||
|
class: Drupal\cdesai_tff\Service\SlugifyService
|
||||||
|
arguments: []
|
||||||
|
tags:
|
||||||
|
- { name: cdesai_tff }
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\cdesai_tff\Service;
|
||||||
|
|
||||||
|
use Cocur\Slugify\Slugify;
|
||||||
|
use Drupal\cdesai_tff\SlugifyServiceInterface;
|
||||||
|
use Drupal\Core\Field\FieldItemInterface;
|
||||||
|
|
||||||
|
class SlugifyService implements SlugifyServiceInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Cocur\Slugify\Slugify
|
||||||
|
*/
|
||||||
|
protected $slugify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates new SlugifyService.
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
$this->slugify = new Slugify();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function convertToSlug(FieldItemInterface $item, $separator = NULL) {
|
||||||
|
// Use the default value if none provided.
|
||||||
|
if (!$separator) {
|
||||||
|
$separator = SlugifyServiceInterface::DEFAULT_SEPARATOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
$origString = $item->getString();
|
||||||
|
return $this->slugify->slugify($origString, $separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\cdesai_tff;
|
||||||
|
|
||||||
|
|
||||||
|
use Drupal\Core\Field\FieldItemInterface;
|
||||||
|
|
||||||
|
interface SlugifyServiceInterface {
|
||||||
|
|
||||||
|
const CONFIG_NAME = 'cdesai_tff.settings';
|
||||||
|
const DEFAULT_SEPARATOR = '_';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the given item to slug format.
|
||||||
|
*
|
||||||
|
* @param \Drupal\Core\Field\FieldItemInterface $item
|
||||||
|
* Field item object that needs to be converted.
|
||||||
|
*
|
||||||
|
* @param string|null $separator (optional)
|
||||||
|
* Separator to use for slug. Defaults to '_'.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
* The converted string.
|
||||||
|
*/
|
||||||
|
public function convertToSlug(FieldItemInterface $item, $separator = NULL);
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user