ContextualLinks.php 3.49 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\contextual\Plugin\views\field\ContextualLinks.
6
7
 */

8
namespace Drupal\contextual\Plugin\views\field;
9

dawehner's avatar
dawehner committed
10
use Drupal\Core\Annotation\Plugin;
11
use Drupal\views\Plugin\views\field\FieldPluginBase;
dawehner's avatar
dawehner committed
12

13
14
15
16
/**
 * Provides a handler that adds contextual links.
 *
 * @ingroup views_field_handlers
17
18
 *
 * @Plugin(
19
20
 *   id = "contextual_links",
 *   module = "contextual"
dawehner's avatar
dawehner committed
21
22
 * )
 */
23
class ContextualLinks extends FieldPluginBase {
24

25
26
  protected function defineOptions() {
    $options = parent::defineOptions();
27
28
29
30
31
32
33

    $options['fields'] = array('default' => array());
    $options['destination'] = array('default' => 1);

    return $options;
  }

34
  public function buildOptionsForm(&$form, &$form_state) {
35
    $all_fields = $this->view->display_handler->getFieldLabels();
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    // Offer to include only those fields that follow this one.
    $field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
    $form['fields'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Fields'),
      '#description' => t('Fields to be included as contextual links.'),
      '#options' => $field_options,
      '#default_value' => $this->options['fields'],
    );
    $form['destination'] = array(
      '#type' => 'select',
      '#title' => t('Include destination'),
      '#description' => t('Include a "destination" parameter in the link to return the user to the original view upon completing the contextual action.'),
      '#options' => array(
        '0' => t('No'),
        '1' => t('Yes'),
      ),
      '#default_value' => $this->options['destination'],
    );
  }

  function pre_render(&$values) {
    // Add a row plugin css class for the contextual link.
59
    $class = 'contextual-region';
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    if (!empty($this->view->style_plugin->options['row_class'])) {
      $this->view->style_plugin->options['row_class'] .= " $class";
    }
    else {
      $this->view->style_plugin->options['row_class'] = $class;
    }
  }

  /**
   * Render the contextual fields.
   */
  function render($values) {
    $links = array();
    foreach ($this->options['fields'] as $field) {
74
75
      $rendered_field = $this->view->style_plugin->get_field($this->view->row_index, $field);
      if (empty($rendered_field)) {
76
77
78
79
80
81
82
        continue;
      }
      $title = $this->view->field[$field]->last_render_text;
      $path = '';
      if (!empty($this->view->field[$field]->options['alter']['path'])) {
        $path = $this->view->field[$field]->options['alter']['path'];
      }
83
      if (!empty($title) && !empty($path)) {
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        // Make sure that tokens are replaced for this paths as well.
        $tokens = $this->get_render_tokens(array());
        $path = strip_tags(decode_entities(strtr($path, $tokens)));

        $links[$field] = array(
          'href' => $path,
          'title' => $title,
        );
        if (!empty($this->options['destination'])) {
          $links[$field]['query'] = drupal_get_destination();
        }
      }
    }

    if (!empty($links)) {
      $build = array(
100
        '#prefix' => '<div class="contextual">',
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        '#suffix' => '</div>',
        '#theme' => 'links__contextual',
        '#links' => $links,
        '#attributes' => array('class' => array('contextual-links')),
        '#attached' => array(
          'library' => array(array('contextual', 'contextual-links')),
        ),
        '#access' => user_access('access contextual links'),
      );
      return drupal_render($build);
    }
    else {
      return '';
    }
  }

117
  public function query() { }
118

119
}