You are here

final class TokensReplacementEvent in Hook Event Dispatcher 8

Class TokensProvideEvent.

Hierarchy

Expanded class hierarchy of TokensReplacementEvent

See also

hook_tokens

3 files declare their use of TokensReplacementEvent
ExampleTokenEventSubscriber.php in src/Example/ExampleTokenEventSubscriber.php
hook_event_dispatcher.module in ./hook_event_dispatcher.module
Hook event dispatcher module.
TokenEventTest.php in tests/src/Unit/Token/TokenEventTest.php

File

src/Event/Token/TokensReplacementEvent.php, line 18

Namespace

Drupal\hook_event_dispatcher\Event\Token
View source
final class TokensReplacementEvent extends Event implements EventInterface {

  /**
   * An associative array of replacement values.
   *
   * Keyed by the raw [type:token] strings from the original text.
   * The returned values must be either plain
   * text strings, or an object implementing MarkupInterface if they are
   * HTML-formatted.
   *
   * @var string|\Drupal\Component\Render\MarkupInterface[]
   */
  private $replacementValues = [];

  /**
   * Type.
   *
   * @var string
   */
  private $type;

  /**
   * Tokens.
   *
   * @var array
   */
  private $tokens;

  /**
   * Data.
   *
   * @var array
   */
  private $data;

  /**
   * Options.
   *
   * @var array
   */
  private $options;

  /**
   * Bubbleable meta data.
   *
   * @var \Drupal\Core\Render\BubbleableMetadata
   */
  private $bubbleableMetadata;

  /**
   * Constructor.
   *
   * @param string $type
   *   The machine-readable name of the type (group) of token being replaced,
   *   such as 'node', 'user', or another type defined by a hook_token_info()
   *   implementation.
   * @param array $tokens
   *   An array of tokens to be replaced. The keys are the machine-readable
   *   token names, and the values are the raw [type:token] strings that
   *   appeared in the
   *   original text.
   * @param array $data
   *   An associative array of data objects to be used when generating
   *   replacement values, as supplied in the $data parameter to
   *   \Drupal\Core\Utility\Token::replace().
   * @param array $options
   *   An associative array of options for token replacement; see
   *   \Drupal\Core\Utility\Token::replace() for possible values.
   * @param \Drupal\Core\Render\BubbleableMetadata $bubbleableMetadata
   *   The bubbleable metadata. Prior to invoking this hook,
   *   \Drupal\Core\Utility\Token::generate() collects metadata for all of the
   *   data objects in $data. For any data sources not in $data, but that are
   *   used by the token replacement logic, such as global configuration (e.g.,
   *   'system.site') and related objects (e.g., $node->getOwner()),
   *   implementations of this hook must add the corresponding metadata.
   */
  public function __construct($type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleableMetadata) {
    $this->type = $type;
    $this->tokens = $tokens;
    $this->data = $data;
    $this->options = $options;
    $this->bubbleableMetadata = $bubbleableMetadata;
  }

  /**
   * Getter.
   *
   * The machine-readable name of the type (group) of token being replaced,
   * such as 'node', 'user', or another type defined by a hook_token_info()
   * implementation.
   *
   * @return string
   *   The type.
   */
  public function getType() {
    return $this->type;
  }

  /**
   * Getter.
   *
   * An array of tokens to be replaced. The keys are the machine-readable
   * token names, and the values are the raw [type:token] strings that
   * appeared in the original text.
   *
   * @return array
   *   The tokens.
   */
  public function getTokens() {
    return $this->tokens;
  }

  /**
   * Getter for single data member.
   *
   * @param string $key
   *   The key for the additional token data, like 'node'.
   * @param mixed $default
   *   The default value, if data does not exists.
   *
   * @return mixed
   *   The value.
   */
  public function getData($key, $default = NULL) {
    return isset($this->data[$key]) ? $this->data[$key] : $default;
  }

  /**
   * Getter.
   *
   * An associative array of data objects to be used when generating
   * replacement values, as supplied in the $data parameter to
   * \Drupal\Core\Utility\Token::replace().
   *
   * @return array
   *   The raw data given inside the hook_tokens.
   */
  public function getRawData() {
    return $this->data;
  }

  /**
   * Getter.
   *
   * An associative array of options for token replacement; see
   * \Drupal\Core\Utility\Token::replace() for possible values.
   *
   * @return array
   *   The raw options given inside the hook_tokens.
   */
  public function getOptions() {
    return $this->options;
  }

  /**
   * Getter.
   *
   * The bubbleable metadata. Prior to invoking this hook,
   * \Drupal\Core\Utility\Token::generate() collects metadata for all of the
   * data objects in $data. For any data sources not in $data, but that are
   * used by the token replacement logic, such as global configuration (e.g.,
   * 'system.site') and related objects (e.g., $node->getOwner()),
   * implementations of this hook must add the corresponding metadata.
   * For example:
   *
   * @return \Drupal\Core\Render\BubbleableMetadata
   *   The metadata.
   */
  public function getBubbleableMetadata() {
    return $this->bubbleableMetadata;
  }

  /**
   * An associative array of replacement values.
   *
   * Keyed by the raw [type:token] strings from the original text.
   * The returned values must be either plain
   * text strings, or an object implementing MarkupInterface if they are
   * HTML-formatted.
   *
   * @return string|\Drupal\Component\Render\MarkupInterface[]
   *   The replacement values for the token.
   */
  public function getReplacementValues() {
    return $this->replacementValues;
  }

  /**
   * Set's a replacement value for a token.
   *
   * @param string $type
   *   The token type like 'node'.
   * @param string $token
   *   The name of the token, like 'url'.
   * @param string|\Drupal\Component\Render\MarkupInterface $replacement
   *   The replacement value.
   *
   * @throws \UnexpectedValueException
   */
  public function setReplacementValue($type, $token, $replacement) {
    if (!is_string($type)) {
      throw new UnexpectedValueException('Type should be a string');
    }
    if (!is_string($token)) {
      throw new UnexpectedValueException('Token should be a string');
    }
    if (!$this
      ->forToken($type, $token)) {
      throw new UnexpectedValueException('Requested replacement is not requested');
    }
    if (!is_string($replacement) && !$replacement instanceof MarkupInterface) {
      throw new UnexpectedValueException('Replacement value should be a string or instanceof MarkupInterface');
    }
    $this->replacementValues["[{$type}:{$token}]"] = $replacement;
  }

  /**
   * {@inheritdoc}
   */
  public function getDispatcherType() {
    return HookEventDispatcherInterface::TOKEN_REPLACEMENT;
  }

  /**
   * Check if the event is for the given token.
   *
   * @param string $type
   *   The token type like 'node'.
   * @param string $token
   *   The token type like 'url'.
   *
   * @return bool
   *   TRUE if there is one.
   */
  public function forToken($type, $token) {
    return $this->type === $type && isset($this->tokens[$token]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TokensReplacementEvent::$bubbleableMetadata private property Bubbleable meta data.
TokensReplacementEvent::$data private property Data.
TokensReplacementEvent::$options private property Options.
TokensReplacementEvent::$replacementValues private property An associative array of replacement values.
TokensReplacementEvent::$tokens private property Tokens.
TokensReplacementEvent::$type private property Type.
TokensReplacementEvent::forToken public function Check if the event is for the given token.
TokensReplacementEvent::getBubbleableMetadata public function Getter.
TokensReplacementEvent::getData public function Getter for single data member.
TokensReplacementEvent::getDispatcherType public function Get the dispatcher type. Overrides EventInterface::getDispatcherType
TokensReplacementEvent::getOptions public function Getter.
TokensReplacementEvent::getRawData public function Getter.
TokensReplacementEvent::getReplacementValues public function An associative array of replacement values.
TokensReplacementEvent::getTokens public function Getter.
TokensReplacementEvent::getType public function Getter.
TokensReplacementEvent::setReplacementValue public function Set's a replacement value for a token.
TokensReplacementEvent::__construct public function Constructor.