3 namespace Drupal\metatag;
4 use Drupal\Core\Utility\Token;
7 * Token handling service. Uses core token service or contributed Token.
14 * @var \Drupal\Core\Utility\Token
19 * Constructs a new MetatagToken object.
21 * @param \Drupal\Core\Utility\Token $token
24 public function __construct(Token $token) {
25 $this->token = $token;
29 * Wrapper for the Token module's string parsing.
33 * @param array $options
35 * @return mixed|string $string
37 public function replace($string, array $data = [], $options = []) {
38 // Set default requirements for metatag unless options specify otherwise.
39 $options = $options + [
43 $replaced = $this->token->replace($string, $data, $options);
45 // Ensure that there are no double-slash sequences due to empty token
47 $replaced = preg_replace('/(?<!:)\/+\//', '/', $replaced);
53 * Gatekeeper function to direct to either the core or contributed Token.
55 * @param array $token_types
56 * The token types to filter the tokens list by. Defaults to an empty array.
59 * If token module is installed, a popup browser plus a help text. If not
62 public function tokenBrowser(array $token_types = []) {
65 $form['intro_text'] = [
66 '#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>',
69 // Normalize taxonomy tokens.
70 if (!empty($token_types)) {
71 $token_types = array_map(function($value) {
72 return stripos($value, 'taxonomy_') === 0 ? substr($value, strlen('taoxnomy_')) : $value;
73 }, (array) $token_types);
77 '#theme' => 'token_tree_link',
78 '#token_types' => $token_types,
79 '#global_types' => TRUE,
80 '#show_nested' => FALSE,