Projet

Général

Profil

Paste
Télécharger (3,19 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_contextual_links.inc @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_contextual_links.
6
 */
7

    
8
/**
9
 * Provides a handler that adds contextual links.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_contextual_links extends views_handler_field {
14
  function option_definition() {
15
    $options = parent::option_definition();
16

    
17
    $options['fields'] = array('default' => array());
18
    $options['destination'] = array('default' => TRUE, 'bool' => TRUE);
19

    
20
    return $options;
21
  }
22

    
23
  function options_form(&$form, &$form_state) {
24
    $all_fields = $this->view->display_handler->get_field_labels();
25
    // Offer to include only those fields that follow this one.
26
    $field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
27
    $form['fields'] = array(
28
      '#type' => 'checkboxes',
29
      '#title' => t('Fields'),
30
      '#description' => t('Fields to be included as contextual links.'),
31
      '#options' => $field_options,
32
      '#default_value' => $this->options['fields'],
33
    );
34
    $form['destination'] = array(
35
      '#type' => 'checkbox',
36
      '#title' => t('Include destination'),
37
      '#description' => t('Include a "destination" parameter in the link to return the user to the original view upon completing the contextual action.'),
38
      '#default_value' => $this->options['destination'],
39
    );
40
  }
41

    
42
  function pre_render(&$values) {
43
    // Add a row plugin css class for the contextual link.
44
    $class = 'contextual-links-region';
45
    if (!empty($this->view->style_plugin->options['row_class'])) {
46
      $this->view->style_plugin->options['row_class'] .= " $class";
47
    }
48
    else {
49
      $this->view->style_plugin->options['row_class'] = $class;
50
    }
51
  }
52

    
53
  /**
54
   * Render the contextual fields.
55
   */
56
  function render($values) {
57
    $links = array();
58
    foreach ($this->options['fields'] as $field) {
59
      if (empty($this->view->style_plugin->rendered_fields[$this->view->row_index][$field])) {
60
        continue;
61
      }
62
      $title = $this->view->field[$field]->last_render_text;
63
      $path = '';
64
      if (!empty($this->view->field[$field]->options['alter']['path'])) {
65
        $path = $this->view->field[$field]->options['alter']['path'];
66
      }
67
      if (!empty($title) && !empty($path)) {
68
        // Make sure that tokens are replaced for this paths as well.
69
        $tokens = $this->get_render_tokens(array());
70
        $path = strip_tags(decode_entities(strtr($path, $tokens)));
71

    
72
        $links[$field] = array(
73
          'href' => $path,
74
          'title' => $title,
75
        );
76
        if (!empty($this->options['destination'])) {
77
          $links[$field]['query'] = drupal_get_destination();
78
        }
79
      }
80
    }
81

    
82
    if (!empty($links)) {
83
      $build = array(
84
        '#prefix' => '<div class="contextual-links-wrapper">',
85
        '#suffix' => '</div>',
86
        '#theme' => 'links__contextual',
87
        '#links' => $links,
88
        '#attributes' => array('class' => array('contextual-links')),
89
        '#attached' => array(
90
          'library' => array(array('contextual', 'contextual-links')),
91
        ),
92
        '#access' => user_access('access contextual links'),
93
      );
94
      return drupal_render($build);
95
    }
96
    else {
97
      return '';
98
    }
99
  }
100

    
101
  function query() { }
102
}