diff --git a/src/Plugin/Field/FieldFormatter/ColorFieldFormatterCss.php b/src/Plugin/Field/FieldFormatter/ColorFieldFormatterCss.php index 38e0c1c..b335777 100644 --- a/src/Plugin/Field/FieldFormatter/ColorFieldFormatterCss.php +++ b/src/Plugin/Field/FieldFormatter/ColorFieldFormatterCss.php @@ -2,11 +2,15 @@ namespace Drupal\color_field\Plugin\Field\FieldFormatter; +use Drupal\color_field\ColorHex; use Drupal\color_field\Plugin\Field\FieldType\ColorFieldType; -use Drupal\Core\Field\FormatterBase; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\color_field\ColorHex; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Utility\Token; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Plugin implementation of the color_field css declaration formatter. @@ -20,7 +24,55 @@ use Drupal\color_field\ColorHex; * } * ) */ -class ColorFieldFormatterCss extends FormatterBase { +class ColorFieldFormatterCss extends FormatterBase implements ContainerFactoryPluginInterface{ + /** + * The token service. + * + * @var \Drupal\token\TokenInterface + */ + protected $tokenService; + + /** + * Constructs an ColorFieldFormatterCss 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\Core\Utility\Token $token_service + * The token service. + */ + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, Token $token_service) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); + $this->tokenService = $token_service; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + // @see \Drupal\Core\Field\FormatterPluginManager::createInstance(). + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'], + $container->get('token') + ); + } /** * {@inheritdoc} @@ -113,10 +165,16 @@ class ColorFieldFormatterCss extends FormatterBase { $elements = []; + $entity = $items->getEntity(); + $tokens = [ + $entity->getEntityType()->id() => $entity, + ]; foreach ($items as $delta => $item) { - $value = $this->viewValue($item); - $selector = $settings['selector']; + $selector = $this->tokenService->replace( + $settings['selector'], + $tokens + ); $important = ($settings['important']) ? ' !important' : ''; $property = $settings['property'];