TokenBase.php in Display Suite 8.4
File
src/Plugin/DsField/TokenBase.php
View source
<?php
namespace Drupal\ds\Plugin\DsField;
use Drupal\Core\Utility\Token;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManager;
abstract class TokenBase extends DsFieldBase {
protected $token;
protected $languageManager;
public function __construct($configuration, $plugin_id, $plugin_definition, Token $token_service, LanguageManager $language_manager) {
$this->token = $token_service;
$this->languageManager = $language_manager;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
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('language_manager'));
}
public function build() {
$content = $this
->content();
$format = $this
->format();
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
$value = $this->token
->replace($content, [
$this
->getEntityTypeId() => $this
->entity(),
], [
'langcode' => $langcode,
'clear' => TRUE,
]);
if ($value === '') {
return [];
}
return [
'#type' => 'processed_text',
'#text' => $value,
'#format' => $format,
'#filter_types_to_skip' => [],
'#langcode' => $langcode,
];
}
protected function format() {
return 'plain_text';
}
protected function content() {
return '';
}
}
Classes
Name |
Description |
TokenBase |
The base plugin to create DS code fields. |