Further modules included.
[yaffs-website] / web / modules / contrib / linkit / src / MatcherTokensTrait.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\MatcherTokensTrait.
6  */
7
8 namespace Drupal\linkit;
9
10 /**
11  * Provides friendly methods for matchers using tokens.
12  */
13 trait MatcherTokensTrait {
14
15   /**
16    * Inserts a form element with a list of available tokens.
17    *
18    * @param $form
19    *   The form array to append the token list to.
20    * @param array $types
21    *   An array of token types to use.
22    */
23   public function insertTokenList(&$form, array $types = array()) {
24     if (\Drupal::moduleHandler()->moduleExists('token')) {
25       // Add the token tree UI.
26       $form['token_tree'] = array(
27         '#theme' => 'token_tree_link',
28         '#token_types' => $types,
29         '#dialog' => TRUE,
30         '#weight' => -90,
31       );
32     }
33     else {
34       $token_items = array();
35       foreach ($this->getAvailableTokens($types) as $type => $tokens) {
36         foreach ($tokens as $name => $info) {
37           $token_description = !empty($info['description']) ? $info['description'] : '';
38           $token_items[$type . ':' . $name] = "[$type:$name]" . ' - ' . $info['name'] . ': ' . $token_description;
39         }
40       }
41
42       if (count($token_items)) {
43         $form['tokens'] = array(
44           '#type' => 'details',
45           '#title' => t('Available tokens'),
46           '#weight' => -90,
47         );
48
49         $form['tokens']['list'] = array(
50           '#theme' => 'item_list',
51           '#items' => $token_items,
52         );
53       }
54     }
55   }
56
57   /**
58    * Gets all available tokens.
59    *
60    * @param array $types
61    *   An array of token types to use.
62    * @return array
63    *   An array with available tokens
64    */
65   public function getAvailableTokens(array $types = array()) {
66     $info = \Drupal::token()->getInfo();
67     $available = array_intersect_key($info['tokens'], array_flip($types));
68     return $available;
69   }
70
71 }