View source
<?php
namespace Drupal\token_filter\Plugin\Filter;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Utility\Token;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\token\TokenEntityMapperInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TokenFilter extends FilterBase implements ContainerFactoryPluginInterface {
protected $token;
protected $tokenEntityMapper;
protected $renderer;
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token, TokenEntityMapperInterface $token_entity_mapper, RendererInterface $renderer, RouteMatchInterface $current_route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
$this->tokenEntityMapper = $token_entity_mapper;
$this->renderer = $renderer;
$this->routeMatch = $current_route_match;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('token'), $container
->get('token.entity_mapper'), $container
->get('renderer'), $container
->get('current_route_match'));
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['replace_empty'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Replace empty values'),
'#description' => $this
->t('Remove tokens from text if they cannot be replaced with a value'),
'#default_value' => $this->settings['replace_empty'],
];
return $form;
}
public function process($text, $langcode) {
$data = [];
$entity = drupal_static('token_filter_entity', NULL);
$cache = new BubbleableMetadata();
if (!is_null($entity) && $entity instanceof ContentEntityInterface) {
$cache
->addCacheableDependency($entity);
$token_type = $this->tokenEntityMapper
->getTokenTypeForEntityType($entity
->getEntityTypeId());
$data[$token_type] = $entity;
}
$clear = $this->settings['replace_empty'];
$replacements = $this->token
->replace($text, $data, [
'clear' => $clear,
], $cache);
return (new FilterProcessResult($replacements))
->merge($cache);
}
public function tips($long = FALSE) {
$build = [];
$build[] = [
'#markup' => $this
->t('Global and entity tokens are replaced with their values.'),
];
$token_types = [];
$parameters = $this->routeMatch
->getParameters();
foreach ($parameters as $parameter) {
$entity_type = NULL;
if ($parameter instanceof ContentEntityInterface) {
$entity_type = $parameter
->getEntityTypeId();
}
elseif ($parameter instanceof ConfigEntityBundleBase) {
$entity_type = $parameter
->getEntityType()
->getBundleOf();
}
if (isset($entity_type)) {
$token_type = $this->tokenEntityMapper
->getTokenTypeForEntityType($entity_type);
$token_types[] = $token_type;
}
}
$build[] = [
'#prefix' => ' ',
'#theme' => 'token_tree_link',
'#token_types' => $token_types,
];
return $this->renderer
->render($build);
}
}