TokenizeAreaPluginBase.php 3.53 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

/**
 * @file
 * Contains \Drupal\views\Plugin\views\area\TokenizeAreaPluginBase.
 */

namespace Drupal\views\Plugin\views\area;

/**
 * Tokenized base class for area handlers.
 *
 * This class provides a method tokenizeValue() to tokenize a given value with
 * the tokens of the first view result and additionally apllies global token
 * replacement to the passed value. The form elements to enable the replacement
 * functionality is automatically added to the buildOptionsForm().
 *
 * @ingroup views_area_handlers
 */
abstract class TokenizeAreaPluginBase extends AreaPluginBase {

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['tokenize'] = array('default' => FALSE, 'bool' => TRUE);
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);

    // Add tokenization form elements.
    $this->tokenForm($form, $form_state);
  }

  /**
   * Adds tokenization form elements.
   */
  public function tokenForm(&$form, &$form_state) {
    $form['tokenize'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use replacement tokens from the first row'),
      '#default_value' => $this->options['tokenize'],
    );

    // Get a list of the available fields and arguments for token replacement.
    $options = array();
    foreach ($this->view->display_handler->getHandlers('field') as $field => $handler) {
      $options[t('Fields')]["[$field]"] = $handler->adminLabel();
    }

    $count = 0; // This lets us prepare the key as we want it printed.
58
    foreach ($this->view->display_handler->getHandlers('argument') as $handler) {
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
      $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->adminLabel()));
      $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->adminLabel()));
    }

    if (!empty($options)) {
      $form['tokens'] = array(
        '#type' => 'details',
        '#title' => t('Replacement patterns'),
        '#open' => TRUE,
        '#id' => 'edit-options-token-help',
        '#states' => array(
          'visible' => array(
            ':input[name="options[tokenize]"]' => array('checked' => TRUE),
          ),
        ),
      );
      $form['tokens']['help'] = array(
        '#markup' => '<p>' . t('The following tokens are available. If you would like to have the characters \'[\' and \']\' use the html entity codes \'%5B\' or  \'%5D\' or they will get replaced with empty space.') . '</p>',
      );
      foreach (array_keys($options) as $type) {
        if (!empty($options[$type])) {
          $items = array();
          foreach ($options[$type] as $key => $value) {
            $items[] = $key . ' == ' . $value;
          }
          $form['tokens']['tokens'] = array(
            '#theme' => 'item_list',
            '#items' => $items,
          );
        }
      }
    }

    $this->globalTokenForm($form, $form_state);
  }

  /**
   * Replaces value with special views tokens and global tokens.
   *
   * @param string $value
   *   The value to eventually tokenize.
   *
   * @return string
   *   Tokenized value if tokenize option is enabled. In any case global tokens
   *   will be replaced.
   */
  public function tokenizeValue($value) {
    if ($this->options['tokenize']) {
      $value = $this->view->style_plugin->tokenizeValue($value, 0);
    }
    // As we add the globalTokenForm() we also should replace the token here.
    return $this->globalTokenReplace($value);
  }

}