Adds the basic Slugify Text Formatter (no settings).
This commit is contained in:
@@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Drupal\cdesai_tff\Plugin\Field\FieldFormatter;
|
||||||
|
|
||||||
|
use Drupal\cdesai_tff\SlugifyServiceInterface;
|
||||||
|
use Drupal\cdesai_tff\DesaiTextFormatterBase;
|
||||||
|
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||||
|
use Drupal\Core\Field\FieldItemInterface;
|
||||||
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of custom Slugify text formatter.
|
||||||
|
*
|
||||||
|
* @FieldFormatter(
|
||||||
|
* id = "cdesai_tff_slugify",
|
||||||
|
* label = @Translation("Custom Sugify Formatter"),
|
||||||
|
* field_types = {
|
||||||
|
* "string",
|
||||||
|
* "text"
|
||||||
|
* }
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class SlugifyTextFormatter extends DesaiTextFormatterBase implements ContainerFactoryPluginInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Slugify Service.
|
||||||
|
*
|
||||||
|
* @var \Drupal\cdesai_tff\SlugifyServiceInterface
|
||||||
|
*/
|
||||||
|
protected $slugifyService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a SlugifyTextFormatter object.
|
||||||
|
*
|
||||||
|
* @param string $plugin_id
|
||||||
|
* The plugin_id for the formatter.
|
||||||
|
* @param mixed $plugin_definition
|
||||||
|
* The plugin implementation definition.
|
||||||
|
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
|
||||||
|
* The definition of the field to which the formatter is associated.
|
||||||
|
* @param array $settings
|
||||||
|
* The formatter settings.
|
||||||
|
* @param string $label
|
||||||
|
* The formatter label display setting.
|
||||||
|
* @param string $view_mode
|
||||||
|
* The view mode.
|
||||||
|
* @param array $third_party_settings
|
||||||
|
* Any third party settings.
|
||||||
|
* @param \Drupal\cdesai_tff\SlugifyServiceInterface $slugify_service
|
||||||
|
* The slugify service.
|
||||||
|
*/
|
||||||
|
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, SlugifyServiceInterface $slugify_service) {
|
||||||
|
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
|
||||||
|
$this->slugifyService = $slugify_service;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||||
|
return new static(
|
||||||
|
$plugin_id,
|
||||||
|
$plugin_definition,
|
||||||
|
$configuration['field_definition'],
|
||||||
|
$configuration['settings'], $configuration['label'],
|
||||||
|
$configuration['view_mode'],
|
||||||
|
$configuration['third_party_settings'],
|
||||||
|
$container->get('cdesai_tff.slugify')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
function getCssClassId() {
|
||||||
|
return 'cdesai-tff-slugify';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
function getValue(FieldItemInterface $item) {
|
||||||
|
return $this->slugifyService->convertToSlug($item);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user