3 namespace Drupal\metatag;
5 use Drupal\Core\Utility\Token;
8 * Token handling service. Uses core token service or contributed Token.
15 * @var \Drupal\Core\Utility\Token
20 * Constructs a new MetatagToken object.
22 * @param \Drupal\Core\Utility\Token $token
25 public function __construct(Token $token) {
26 $this->token = $token;
30 * Wrapper for the Token module's string parsing.
32 * @param string $string
33 * The string to parse.
35 * Arguments for token->replace().
36 * @param array $options
37 * Any additional options necessary.
39 * @return mixed|string
40 * The processed string.
42 public function replace($string, array $data = [], array $options = []) {
43 // Set default requirements for metatag unless options specify otherwise.
44 $options = $options + [
48 $replaced = $this->token->replace($string, $data, $options);
50 // Ensure that there are no double-slash sequences due to empty token
52 $replaced = preg_replace('/(?<!:)(?<!)\/+\//', '/', $replaced);
58 * Gatekeeper function to direct to either the core or contributed Token.
60 * @param array $token_types
61 * The token types to filter the tokens list by. Defaults to an empty array.
64 * If token module is installed, a popup browser plus a help text. If not
67 public function tokenBrowser(array $token_types = []) {
70 $form['intro_text'] = [
71 '#markup' => '<p>' . t('<strong>Configure the meta tags below.</strong><br /> To view a summary of the individual meta tags and the pattern for a specific configuration, click on its name below. Use tokens to avoid redundant meta data and search engine penalization. For example, a \'keyword\' value of "example" will be shown on all content using this configuration, whereas using the [node:field_keywords] automatically inserts the "keywords" values from the current entity (node, term, etc).') . '</p>',
74 // Normalize taxonomy tokens.
75 if (!empty($token_types)) {
76 $token_types = array_map(function ($value) {
77 return stripos($value, 'taxonomy_') === 0 ? substr($value, strlen('taxonomy_')) : $value;
78 }, (array) $token_types);
82 '#theme' => 'token_tree_link',
83 '#token_types' => $token_types,
84 '#global_types' => TRUE,
85 '#show_nested' => FALSE,