View source
<?php
namespace Drupal\token_filter\Plugin\CKEditorPlugin;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\editor\Entity\Editor;
use Drupal\token\Token;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TokenBrowser extends CKEditorPluginBase implements ContainerFactoryPluginInterface, CKEditorPluginConfigurableInterface {
protected $csrfTokenService;
protected $tokenService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CsrfTokenGenerator $csrf_token_service, Token $token_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->csrfTokenService = $csrf_token_service;
$this->tokenService = $token_service;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('csrf_token'), $container
->get('token'));
}
public function getButtons($token_types = NULL) {
return [
'tokenbrowser' => [
'id' => 'tokenbrowser',
'label' => t('Token browser'),
'image' => file_create_url($this
->getImage()),
'link' => $this
->getUrl($token_types)
->toString(),
],
];
}
protected function getUrl($token_types = NULL) {
$url = Url::fromRoute('token.tree');
$options['query'] = [
'options' => Json::encode($this
->getQueryOptions($token_types)),
'token' => $this->csrfTokenService
->get($url
->getInternalPath()),
];
$url
->setOptions($options);
return $url;
}
protected function getQueryOptions($token_types = NULL) {
return [
'token_types' => $token_types ?: 'all',
'global_types' => FALSE,
'click_insert' => TRUE,
'show_restricted' => FALSE,
'show_nested' => FALSE,
'recursion_limit' => 3,
];
}
protected function getImage() {
return $this
->getModulePath() . '/js/plugins/tokenbrowser/tokenbrowser.png';
}
public function getFile() {
return $this
->getModulePath() . '/js/plugins/tokenbrowser/plugin.js';
}
protected function getModulePath() {
return drupal_get_path('module', 'token_filter');
}
public function getConfig(Editor $editor) {
$token_types = NULL;
$settings = $editor
->getSettings();
if (isset($settings['plugins']['tokenbrowser'], $settings['plugins']['tokenbrowser']['token_types'])) {
$token_types = $settings['plugins']['tokenbrowser']['token_types'];
}
return [
'TokenBrowser_buttons' => $this
->getButtons($token_types),
'token_types' => $token_types,
];
}
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$config = $this
->getConfig($editor);
$parent_token_types = array_filter($this->tokenService
->getInfo()['types'], function ($v) {
return empty($v['nested']);
});
$parent_token_types = array_map(function ($v) {
return $v['name'];
}, $parent_token_types);
$form['token_types'] = [
'#type' => 'select',
'#title' => $this
->t('Token types'),
'#description' => $this
->t('Optionally restrict the token types to show in the browser. Select none to show all.'),
'#multiple' => TRUE,
'#options' => $parent_token_types,
'#default_value' => $config['token_types'],
];
return $form;
}
}