class TokenBrowser in Token Filter 8
Defines the "tokenbrowser" plugin.
NOTE: The plugin ID ('id' key) corresponds to the CKEditor plugin name. It is the first argument of the CKEDITOR.plugins.add() function in the plugin.js file.
Plugin annotation
@CKEditorPlugin(
id = "tokenbrowser",
label = @Translation("Token browser")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\ckeditor\CKEditorPluginBase implements CKEditorPluginButtonsInterface, CKEditorPluginInterface
- class \Drupal\token_filter\Plugin\CKEditorPlugin\TokenBrowser implements CKEditorPluginConfigurableInterface, ContainerFactoryPluginInterface
- class \Drupal\ckeditor\CKEditorPluginBase implements CKEditorPluginButtonsInterface, CKEditorPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TokenBrowser
File
- src/
Plugin/ CKEditorPlugin/ TokenBrowser.php, line 28
Namespace
Drupal\token_filter\Plugin\CKEditorPluginView source
class TokenBrowser extends CKEditorPluginBase implements ContainerFactoryPluginInterface, CKEditorPluginConfigurableInterface {
/**
* The CSRF token manager service.
*
* @var Drupal\Core\Access\CsrfTokenGenerator
*/
protected $csrfTokenService;
protected $tokenService;
/**
* {@inheritdoc}
*
* @param Drupal\Core\Access\CsrfTokenGenerator $csrf_token_service
*/
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'));
}
/**
* {@inheritdoc}
*
* NOTE: The keys of the returned array corresponds to the CKEditor button
* names. They are the first argument of the editor.ui.addButton() or
* editor.ui.addRichCombo() functions in the plugin.js file.
*/
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(),
],
];
}
/**
* Fetches the URL.
*
* @return Drupal\Core\Url
* The URL.
*
* @see TokenTreeController::outputTree().
*/
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;
}
/**
* Fetches the list of query options.
*
* @return array
* The list of query options.
*
* @see TreeBuilderInterface::buildRenderable() for option definitions.
*/
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,
];
}
/**
* Fetches the path to the image.
*
* Make sure that the path to the image matches the file structure of the
* CKEditor plugin you are implementing.
*
* @return string
* The string representation of the path to the image.
*/
protected function getImage() {
return $this
->getModulePath() . '/js/plugins/tokenbrowser/tokenbrowser.png';
}
/**
* {@inheritdoc}
*
* Make sure that the path to the plugin.js matches the file structure of the
* CKEditor plugin you are implementing.
*/
public function getFile() {
return $this
->getModulePath() . '/js/plugins/tokenbrowser/plugin.js';
}
/**
* Fetches the path to this module.
*
* @return string
* The string representation of the module's path.
*/
protected function getModulePath() {
return drupal_get_path('module', 'token_filter');
}
/**
* {@inheritdoc}
*/
public function getConfig(Editor $editor) {
// Get settings.
$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,
];
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
// Get config.
$config = $this
->getConfig($editor);
// Get parent token type names, keyed by machine name.
$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);
// Add multiselect for token types to show in browser.
$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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CKEditorPluginBase:: |
public | function |
Returns a list of plugins this plugin requires. Overrides CKEditorPluginInterface:: |
1 |
CKEditorPluginBase:: |
public | function |
Returns a list of libraries this plugin requires. Overrides CKEditorPluginInterface:: |
4 |
CKEditorPluginBase:: |
public | function |
Indicates if this plugin is part of the optimized CKEditor build. Overrides CKEditorPluginInterface:: |
4 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TokenBrowser:: |
protected | property | The CSRF token manager service. | |
TokenBrowser:: |
protected | property | ||
TokenBrowser:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
TokenBrowser:: |
public | function |
NOTE: The keys of the returned array corresponds to the CKEditor button
names. They are the first argument of the editor.ui.addButton() or
editor.ui.addRichCombo() functions in the plugin.js file. Overrides CKEditorPluginButtonsInterface:: |
|
TokenBrowser:: |
public | function |
Returns the additions to CKEDITOR.config for a specific CKEditor instance. Overrides CKEditorPluginInterface:: |
|
TokenBrowser:: |
public | function |
Make sure that the path to the plugin.js matches the file structure of the
CKEditor plugin you are implementing. Overrides CKEditorPluginInterface:: |
|
TokenBrowser:: |
protected | function | Fetches the path to the image. | |
TokenBrowser:: |
protected | function | Fetches the path to this module. | |
TokenBrowser:: |
protected | function | Fetches the list of query options. | |
TokenBrowser:: |
protected | function | Fetches the URL. | |
TokenBrowser:: |
public | function |
Returns a settings form to configure this CKEditor plugin. Overrides CKEditorPluginConfigurableInterface:: |
|
TokenBrowser:: |
public | function |
Overrides PluginBase:: |