Adds the Slugify service.

This commit is contained in:
2021-02-18 07:50:05 +00:00
parent 30499228c8
commit abcdd135cb
3 changed files with 71 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
# Register our services
services:
cdesai_tff.slugify:
class: Drupal\cdesai_tff\Service\SlugifyService
arguments: []
tags:
- { name: cdesai_tff }
+37
View File
@@ -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);
}
}
+27
View File
@@ -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);
}