Projet

Général

Profil

Paste
Télécharger (4,75 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_links.inc @ 4003efde

1
<?php
2

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

    
8
/**
9
 * A abstract handler which provides a collection of links.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_links extends views_handler_field {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function option_definition() {
19
    $options = parent::option_definition();
20

    
21
    $options['fields'] = array('default' => array());
22
    $options['check_access'] = array('default' => FALSE);
23
    $options['destination'] = array('default' => TRUE, 'bool' => TRUE);
24

    
25
    return $options;
26
  }
27

    
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public function options_form(&$form, &$form_state) {
32
    parent::options_form($form, $form_state);
33

    
34
    $all_fields = $this->view->display_handler->get_field_labels();
35
    // Offer to include only those fields that follow this one.
36
    $field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
37
    $form['fields'] = array(
38
      '#type' => 'checkboxes',
39
      '#title' => t('Fields'),
40
      '#description' => t('Fields to be included as links.'),
41
      '#options' => $field_options,
42
      '#default_value' => $this->options['fields'],
43
      '#required' => TRUE,
44
    );
45

    
46
    $form['check_access'] = array(
47
      '#type' => 'checkbox',
48
      '#title' => t('Evaluate router path for access'),
49
      '#default_value' => $this->options['check_access'],
50
      '#description' => t('Will check if the path exists and is accessible for the current user. Might be useful, might be slow.'),
51
    );
52

    
53
    $form['destination'] = array(
54
      '#type' => 'checkbox',
55
      '#title' => t('Include destination'),
56
      '#description' => t('Include a "destination" parameter in the link to return the user to the original view upon completing the link action.'),
57
      '#default_value' => $this->options['destination'],
58
    );
59
  }
60

    
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function options_submit(&$form, &$form_state) {
65
    // Remove unselected options.
66
    $form_state['values']['options']['fields'] = array_filter($form_state['values']['options']['fields']);
67
    parent::options_submit($form, $form_state);
68
  }
69

    
70
  /**
71
   * Return the list of links of this field.
72
   *
73
   * @return array
74
   *   The links which are used by the render function.
75
   */
76
  public function get_links() {
77
    $links = array();
78
    foreach ($this->options['fields'] as $field) {
79
      if (empty($this->view->field[$field]->last_render_text)) {
80
        continue;
81
      }
82

    
83
      $title = $this->view->field[$field]->last_render_text;
84
      // Use the alter settings for the link field source not this links field.
85
      $alter = $this->view->field[$field]->options['alter'];
86
      $url = array('query' => array());
87

    
88
      // Copy code from views_handler_field::render_as_link().
89
      $path = $alter['path'];
90
      if (!empty($path) && $path != '<front>') {
91
        // Leave path alone on <front> as strip_tags() would remove this.
92
        // Replace tokens and strip any HTML tags in the path.
93
        $tokens = $this->get_render_tokens(array());
94
        $path = strip_tags(decode_entities(strtr($path, $tokens)));
95

    
96
        if (!empty($alter['path_case']) && $alter['path_case'] != 'none') {
97
          $path = $this->case_transform($path, $alter['path_case']);
98
        }
99

    
100
        if (!empty($alter['replace_spaces'])) {
101
          $path = str_replace(' ', '-', $path);
102
        }
103

    
104
        $url = drupal_parse_url($path);
105
        if (empty($url)) {
106
          // Seriously malformed URLs may return FALSE or empty arrays.
107
          continue;
108
        }
109
        $path = $url['path'];
110

    
111
        // Check router menu item access for the current user.
112
        if ($this->options['check_access']) {
113
          $menu_item = menu_get_item($path);
114
          if (!$menu_item || empty($menu_item['access'])) {
115
            continue;
116
          }
117
        }
118

    
119
        if (!empty($this->options['destination']) && empty($alter['external'])) {
120
          // Override any destination argument included in URL.
121
          $url['query'] = array_merge($url['query'], drupal_get_destination());
122
        }
123

    
124
        // Omit tweaks of query, fragment, and link_class.
125
        $alt = strtr($alter['alt'], $tokens);
126
        if ($alt && $alt != $title) {
127
          // Set the title attribute only if it improves accessibility.
128
          $url['attributes']['title'] = decode_entities($alt);
129
        }
130

    
131
        if (!empty($alter['rel']) && $rel = strtr($alter['rel'], $tokens)) {
132
          $url['attributes']['rel'] = $rel;
133
        }
134

    
135
        $target = check_plain(trim(strtr($alter['target'], $tokens)));
136
        if (!empty($target)) {
137
          $url['attributes']['target'] = $target;
138
        }
139
      }
140

    
141
      $links[$field] = array(
142
        'href' => $path,
143
        'title' => $title,
144
      ) + $url;
145
    }
146

    
147
    return $links;
148
  }
149

    
150
  /**
151
   * {@inheritdoc}
152
   */
153
  public function query() {
154
  }
155

    
156
}