3 namespace Drupal\metatag;
5 use Drupal\Core\Utility\Token;
6 use Drupal\Core\Render\BubbleableMetadata;
9 * Token handling service. Uses core token service or contributed Token.
16 * @var \Drupal\Core\Utility\Token
21 * Constructs a new MetatagToken object.
23 * @param \Drupal\Core\Utility\Token $token
26 public function __construct(Token $token) {
27 $this->token = $token;
31 * Wrapper for the Token module's string parsing.
33 * @param string $string
34 * The string to parse.
36 * Arguments for token->replace().
37 * @param array $options
38 * Any additional options necessary.
39 * @param \Drupal\Core\Render\BubbleableMetadata|null $bubbleable_metadata
40 * (optional) An object to which static::generate() and the hooks and
41 * functions that it invokes will add their required bubbleable metadata.
43 * @return mixed|string
44 * The processed string.
46 public function replace($string, array $data = [], array $options = [], BubbleableMetadata $bubbleable_metadata = NULL) {
47 // Set default requirements for metatag unless options specify otherwise.
48 $options = $options + [
52 $replaced = $this->token->replace($string, $data, $options, $bubbleable_metadata);
54 // Ensure that there are no double-slash sequences due to empty token
56 $replaced = preg_replace('/(?<!:)(?<!)\/+\//', '/', $replaced);
62 * Gatekeeper function to direct to either the core or contributed Token.
64 * @param array $token_types
65 * The token types to filter the tokens list by. Defaults to an empty array.
68 * If token module is installed, a popup browser plus a help text. If not
71 public function tokenBrowser(array $token_types = []) {
74 $form['intro_text'] = [
75 '#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>',
78 // Normalize taxonomy tokens.
79 if (!empty($token_types)) {
80 $token_types = array_map(function ($value) {
81 return stripos($value, 'taxonomy_') === 0 ? substr($value, strlen('taxonomy_')) : $value;
82 }, (array) $token_types);
86 '#theme' => 'token_tree_link',
87 '#token_types' => $token_types,
88 '#global_types' => TRUE,
89 '#show_nested' => FALSE,