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