You are here

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

Expanded class hierarchy of TokenBrowser

File

src/Plugin/CKEditorPlugin/TokenBrowser.php, line 28

Namespace

Drupal\token_filter\Plugin\CKEditorPlugin
View 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

Namesort descending Modifiers Type Description Overrides
CKEditorPluginBase::getDependencies public function Returns a list of plugins this plugin requires. Overrides CKEditorPluginInterface::getDependencies 1
CKEditorPluginBase::getLibraries public function Returns a list of libraries this plugin requires. Overrides CKEditorPluginInterface::getLibraries 4
CKEditorPluginBase::isInternal public function Indicates if this plugin is part of the optimized CKEditor build. Overrides CKEditorPluginInterface::isInternal 4
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TokenBrowser::$csrfTokenService protected property The CSRF token manager service.
TokenBrowser::$tokenService protected property
TokenBrowser::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
TokenBrowser::getButtons 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::getButtons
TokenBrowser::getConfig public function Returns the additions to CKEDITOR.config for a specific CKEditor instance. Overrides CKEditorPluginInterface::getConfig
TokenBrowser::getFile public function Make sure that the path to the plugin.js matches the file structure of the CKEditor plugin you are implementing. Overrides CKEditorPluginInterface::getFile
TokenBrowser::getImage protected function Fetches the path to the image.
TokenBrowser::getModulePath protected function Fetches the path to this module.
TokenBrowser::getQueryOptions protected function Fetches the list of query options.
TokenBrowser::getUrl protected function Fetches the URL.
TokenBrowser::settingsForm public function Returns a settings form to configure this CKEditor plugin. Overrides CKEditorPluginConfigurableInterface::settingsForm
TokenBrowser::__construct public function Overrides PluginBase::__construct