class ExtraFieldTokenPlugin in Entity Extra Field 8
Same name and namespace in other branches
- 2.0.x src/Plugin/ExtraFieldType/ExtraFieldTokenPlugin.php \Drupal\entity_extra_field\Plugin\ExtraFieldType\ExtraFieldTokenPlugin
 
Define extra field token plugin.
Plugin annotation
@ExtraFieldType(
  id = "token",
  label = @Translation("Token")
)
  Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\entity_extra_field\ExtraFieldTypePluginBase implements ExtraFieldTypePluginInterface uses PluginDependencyTrait
- class \Drupal\entity_extra_field\Plugin\ExtraFieldType\ExtraFieldTokenPlugin
 
 
 - class \Drupal\entity_extra_field\ExtraFieldTypePluginBase implements ExtraFieldTypePluginInterface uses PluginDependencyTrait
 
 - class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
 
Expanded class hierarchy of ExtraFieldTokenPlugin
File
- src/
Plugin/ ExtraFieldType/ ExtraFieldTokenPlugin.php, line 21  
Namespace
Drupal\entity_extra_field\Plugin\ExtraFieldTypeView source
class ExtraFieldTokenPlugin extends ExtraFieldTypePluginBase {
  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'type' => NULL,
      'token' => NULL,
    ] + parent::defaultConfiguration();
  }
  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $type = $this
      ->getPluginFormStateValue('type', $form_state);
    $form['type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Type'),
      '#required' => TRUE,
      '#options' => [
        'textfield' => $this
          ->t('Text Field'),
        'text_format' => $this
          ->t('Text Format'),
      ],
      '#empty_empty' => $this
        ->t('- Select -'),
      '#default_value' => $type,
      '#ajax' => [
        'event' => 'change',
        'method' => 'replace',
      ] + $this
        ->extraFieldPluginAjax(),
    ];
    if (isset($type) && !empty($type)) {
      $configuration = $this
        ->getConfiguration();
      $form['token'] = [
        '#type' => $type,
        '#title' => $this
          ->t('Token Value'),
        '#default_value' => is_array($configuration['token']) ? $configuration['token']['value'] : $configuration['token'],
      ];
      if ($type === 'text_format' && isset($configuration['token']['format'])) {
        $form['token']['#format'] = $configuration['token']['format'];
      }
      if ($this->moduleHandler
        ->moduleExists('token')) {
        $form['token_replacements'] = [
          '#theme' => 'token_tree_link',
          '#token_types' => $this
            ->getEntityTokenTypes($this
            ->getTargetEntityTypeId(), $this
            ->getTargetEntityTypeBundle()
            ->id()),
        ];
      }
    }
    return $form;
  }
  /**
   * {@inheritdoc}
   */
  public function build(EntityInterface $entity, EntityDisplayInterface $display) {
    $build = [];
    $token_value = $this
      ->getProcessedTokenValue($entity);
    switch ($this
      ->getTokenTextType()) {
      case 'textfield':
        $build = [
          '#plain_text' => $token_value,
        ];
        break;
      case 'text_format':
        $build = [
          '#type' => 'processed_text',
          '#text' => $token_value,
          '#format' => $this
            ->getTokenTextFormat(),
        ];
        break;
    }
    return $build;
  }
  /**
   * Get token text type.
   *
   * @return string
   *   The token text type.
   */
  protected function getTokenTextType() {
    $configuration = $this
      ->getConfiguration();
    return $configuration['type'];
  }
  /**
   * Get token text format.
   *
   * @return string
   *   The token text format.
   */
  protected function getTokenTextFormat() {
    $configuration = $this
      ->getConfiguration();
    return isset($configuration['token']['format']) ? $configuration['token']['format'] : NULL;
  }
  /**
   * Get processed token value token.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *
   * @return string
   *   The process token value.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function getProcessedTokenValue(ContentEntityInterface $entity) {
    $configuration = $this
      ->getConfiguration();
    $token_value = is_array($configuration['token']) ? $configuration['token']['value'] : $configuration['token'];
    return $this
      ->processEntityToken($token_value, $entity);
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            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 | |
| 
            DependencyTrait:: | 
                  protected | property | The object's dependencies. | |
| 
            DependencyTrait:: | 
                  protected | function | Adds multiple dependencies. | |
| 
            DependencyTrait:: | 
                  protected | function | Adds a dependency. | |
| 
            ExtraFieldTokenPlugin:: | 
                  public | function | 
            Build the render array of the extra field type contents. Overrides ExtraFieldTypePluginInterface:: | 
                  |
| 
            ExtraFieldTokenPlugin:: | 
                  public | function | 
            Form constructor. Overrides ExtraFieldTypePluginBase:: | 
                  |
| 
            ExtraFieldTokenPlugin:: | 
                  public | function | 
            Gets default configuration for this plugin. Overrides ExtraFieldTypePluginBase:: | 
                  |
| 
            ExtraFieldTokenPlugin:: | 
                  protected | function | Get processed token value token. | |
| 
            ExtraFieldTokenPlugin:: | 
                  protected | function | Get token text format. | |
| 
            ExtraFieldTokenPlugin:: | 
                  protected | function | Get token text type. | |
| 
            ExtraFieldTypePluginBase:: | 
                  protected | property | ||
| 
            ExtraFieldTypePluginBase:: | 
                  protected | property | ||
| 
            ExtraFieldTypePluginBase:: | 
                  protected | property | ||
| 
            ExtraFieldTypePluginBase:: | 
                  protected | property | ||
| 
            ExtraFieldTypePluginBase:: | 
                  protected | property | ||
| 
            ExtraFieldTypePluginBase:: | 
                  public | function | 
            Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: | 
                  2 | 
| 
            ExtraFieldTypePluginBase:: | 
                  public static | function | 
            Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: | 
                  1 | 
| 
            ExtraFieldTypePluginBase:: | 
                  protected | function | Get extra field plugin ajax properties. | |
| 
            ExtraFieldTypePluginBase:: | 
                  public | function | Get extra field plugin ajax. | |
| 
            ExtraFieldTypePluginBase:: | 
                  public | function | 
            Gets this plugin's configuration. Overrides ConfigurableInterface:: | 
                  |
| 
            ExtraFieldTypePluginBase:: | 
                  protected | function | Get entity field reference types. | |
| 
            ExtraFieldTypePluginBase:: | 
                  protected | function | Get entity token data. | |
| 
            ExtraFieldTypePluginBase:: | 
                  protected | function | Get entity token types. | |
| 
            ExtraFieldTypePluginBase:: | 
                  protected | function | Get plugin form state value. | |
| 
            ExtraFieldTypePluginBase:: | 
                  protected | function | Get target entity type bundle. | |
| 
            ExtraFieldTypePluginBase:: | 
                  protected | function | Get target entity type definition. | |
| 
            ExtraFieldTypePluginBase:: | 
                  protected | function | Get target entity type identifier. | |
| 
            ExtraFieldTypePluginBase:: | 
                  public | function | 
            Display the extra field plugin label. Overrides ExtraFieldTypePluginInterface:: | 
                  |
| 
            ExtraFieldTypePluginBase:: | 
                  protected | function | Process the entity token text. | |
| 
            ExtraFieldTypePluginBase:: | 
                  public | function | 
            Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: | 
                  |
| 
            ExtraFieldTypePluginBase:: | 
                  public | function | 
            Form submission handler. Overrides PluginFormInterface:: | 
                  1 | 
| 
            ExtraFieldTypePluginBase:: | 
                  public | function | 
            Form validation handler. Overrides PluginFormInterface:: | 
                  1 | 
| 
            ExtraFieldTypePluginBase:: | 
                  public | function | 
            Extra field type view constructor. Overrides PluginBase:: | 
                  1 | 
| 
            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. | |
| 
            PluginDependencyTrait:: | 
                  protected | function | Calculates and adds dependencies of a specific plugin instance. | 1 | 
| 
            PluginDependencyTrait:: | 
                  protected | function | Calculates and returns dependencies of a specific plugin instance. | |
| 
            PluginDependencyTrait:: | 
                  protected | function | Wraps the module handler. | 1 | 
| 
            PluginDependencyTrait:: | 
                  protected | function | Wraps the theme handler. | 1 | 
| 
            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. |