You are here

class Token in Doubleclick for Publishers (DFP) 8

A DFP token service to wrap core's service.

Hierarchy

Expanded class hierarchy of Token

1 string reference to 'Token'
dfp.services.yml in ./dfp.services.yml
dfp.services.yml
1 service uses Token
dfp.token in ./dfp.services.yml
Drupal\dfp\Token

File

src/Token.php, line 19
Contains \Drupal\dfp\Token.

Namespace

Drupal\dfp
View source
class Token implements TokenInterface {

  /**
   * Drupal core's token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $coreToken;

  /**
   * The route match service.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Token constructor.
   *
   * @param \Drupal\Core\Utility\Token $core_token
   *   Drupal core's token service.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match service.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
   */
  public function __construct(CoreToken $core_token, RouteMatchInterface $route_match, AccountInterface $account) {
    $this->coreToken = $core_token;
    $this->routeMatch = $route_match;
    $this->account = $account;
  }

  /**
   * {@inheritdoc}
   */
  public function replace($text, TagView $tag = NULL, array $options = [], BubbleableMetadata $bubbleable_metadata = NULL) {
    $data = [
      'user' => $this->account,
    ];
    if ($tag) {
      $data['dfp_tag'] = $tag;
    }

    // Determine other data from the RouteMatch object.
    $node = $this->routeMatch
      ->getParameter('node');
    if ($node) {
      $data['node'] = $node;
    }
    $term = $this->routeMatch
      ->getParameter('taxonomy_term');
    if ($term) {
      $data['term'] = $term;
    }
    return $this->coreToken
      ->replace($text, $data, $options, $bubbleable_metadata);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Token::$coreToken protected property Drupal core's token service.
Token::$routeMatch protected property The route match service.
Token::replace public function Replaces all tokens in a given string with appropriate values. Overrides TokenInterface::replace
Token::__construct public function Token constructor.