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