Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / src / MetatagToken.php
1 <?php
2
3 namespace Drupal\metatag;
4
5 use Drupal\Core\Utility\Token;
6 use Drupal\Core\Render\BubbleableMetadata;
7
8 /**
9  * Token handling service. Uses core token service or contributed Token.
10  */
11 class MetatagToken {
12
13   /**
14    * Token service.
15    *
16    * @var \Drupal\Core\Utility\Token
17    */
18   protected $token;
19
20   /**
21    * Constructs a new MetatagToken object.
22    *
23    * @param \Drupal\Core\Utility\Token $token
24    *   Token service.
25    */
26   public function __construct(Token $token) {
27     $this->token = $token;
28   }
29
30   /**
31    * Wrapper for the Token module's string parsing.
32    *
33    * @param string $string
34    *   The string to parse.
35    * @param array $data
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.
42    *
43    * @return mixed|string
44    *   The processed string.
45    */
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 + [
49       'clear' => TRUE,
50     ];
51
52     $replaced = $this->token->replace($string, $data, $options, $bubbleable_metadata);
53
54     // Ensure that there are no double-slash sequences due to empty token
55     // values.
56     $replaced = preg_replace('/(?<!:)(?<!)\/+\//', '/', $replaced);
57
58     return $replaced;
59   }
60
61   /**
62    * Gatekeeper function to direct to either the core or contributed Token.
63    *
64    * @param array $token_types
65    *   The token types to filter the tokens list by. Defaults to an empty array.
66    *
67    * @return array
68    *   If token module is installed, a popup browser plus a help text. If not
69    *   only the help text.
70    */
71   public function tokenBrowser(array $token_types = []) {
72     $form = [];
73
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>',
76     ];
77
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);
83     }
84
85     $form['tokens'] = [
86       '#theme' => 'token_tree_link',
87       '#token_types' => $token_types,
88       '#global_types' => TRUE,
89       '#show_nested' => FALSE,
90     ];
91
92     return $form;
93   }
94
95 }