Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / views / area / TokenizeAreaPluginBase.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\area;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Tokenized base class for area handlers.
9  *
10  * This class provides a method tokenizeValue() to tokenize a given value with
11  * the tokens of the first view result and additionally applies global token
12  * replacement to the passed value. The form elements to enable the replacement
13  * functionality is automatically added to the buildOptionsForm().
14  *
15  * @ingroup views_area_handlers
16  */
17 abstract class TokenizeAreaPluginBase extends AreaPluginBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function defineOptions() {
23     $options = parent::defineOptions();
24     $options['tokenize'] = ['default' => FALSE];
25     return $options;
26   }
27
28   /**
29    * {@inheritdoc}
30    */
31   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
32     parent::buildOptionsForm($form, $form_state);
33
34     // Add tokenization form elements.
35     $this->tokenForm($form, $form_state);
36   }
37
38   /**
39    * Adds tokenization form elements.
40    */
41   public function tokenForm(&$form, FormStateInterface $form_state) {
42     $form['tokenize'] = [
43       '#type' => 'checkbox',
44       '#title' => $this->t('Use replacement tokens from the first row'),
45       '#default_value' => $this->options['tokenize'],
46     ];
47
48     // Get a list of the available fields and arguments for token replacement.
49     $options = [];
50     $optgroup_arguments = (string) t('Arguments');
51     $optgroup_fields = (string) t('Fields');
52     foreach ($this->view->display_handler->getHandlers('field') as $field => $handler) {
53       $options[$optgroup_fields]["{{ $field }}"] = $handler->adminLabel();
54     }
55
56     foreach ($this->view->display_handler->getHandlers('argument') as $arg => $handler) {
57       $options[$optgroup_arguments]["{{ arguments.$arg }}"] = $this->t('@argument title', ['@argument' => $handler->adminLabel()]);
58       $options[$optgroup_arguments]["{{ raw_arguments.$arg }}"] = $this->t('@argument input', ['@argument' => $handler->adminLabel()]);
59     }
60
61     if (!empty($options)) {
62       $form['tokens'] = [
63         '#type' => 'details',
64         '#title' => $this->t('Replacement patterns'),
65         '#open' => TRUE,
66         '#id' => 'edit-options-token-help',
67         '#states' => [
68           'visible' => [
69             ':input[name="options[tokenize]"]' => ['checked' => TRUE],
70           ],
71         ],
72       ];
73       $form['tokens']['help'] = [
74         '#markup' => '<p>' . $this->t('The following tokens are available. You may use Twig syntax in this field.') . '</p>',
75       ];
76       foreach (array_keys($options) as $type) {
77         if (!empty($options[$type])) {
78           $items = [];
79           foreach ($options[$type] as $key => $value) {
80             $items[] = $key . ' == ' . $value;
81           }
82           $form['tokens'][$type]['tokens'] = [
83             '#theme' => 'item_list',
84             '#items' => $items,
85           ];
86         }
87       }
88     }
89
90     $this->globalTokenForm($form, $form_state);
91   }
92
93   /**
94    * Replaces value with special views tokens and global tokens.
95    *
96    * @param string $value
97    *   The value to eventually tokenize.
98    *
99    * @return string
100    *   Tokenized value if tokenize option is enabled. In any case global tokens
101    *   will be replaced.
102    */
103   public function tokenizeValue($value) {
104     if ($this->options['tokenize']) {
105       $value = $this->view->getStyle()->tokenizeValue($value, 0);
106     }
107     // As we add the globalTokenForm() we also should replace the token here.
108     return $this->globalTokenReplace($value);
109   }
110
111 }