Projet

Général

Profil

Paste
Télécharger (2,51 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / node / views_handler_field_node.inc @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the basic 'node' field handler.
6
 */
7

    
8
/**
9
 * Field handler to provide simple renderer that allows linking to a node.
10
 * Definition terms:
11
 * - link_to_node default: Should this field have the checkbox "link to node" enabled by default.
12
 *
13
 * @ingroup views_field_handlers
14
 */
15
class views_handler_field_node extends views_handler_field {
16

    
17
  function init(&$view, &$options) {
18
    parent::init($view, $options);
19
    // Don't add the additional fields to groupby
20
    if (!empty($this->options['link_to_node'])) {
21
      $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
22
      if (module_exists('translation')) {
23
        $this->additional_fields['language'] = array('table' => 'node', 'field' => 'language');
24
      }
25
    }
26
  }
27

    
28
  function option_definition() {
29
    $options = parent::option_definition();
30
    $options['link_to_node'] = array('default' => isset($this->definition['link_to_node default']) ? $this->definition['link_to_node default'] : FALSE, 'bool' => TRUE);
31
    return $options;
32
  }
33

    
34
  /**
35
   * Provide link to node option
36
   */
37
  function options_form(&$form, &$form_state) {
38
    $form['link_to_node'] = array(
39
      '#title' => t('Link this field to the original piece of content'),
40
      '#description' => t("Enable to override this field's links."),
41
      '#type' => 'checkbox',
42
      '#default_value' => !empty($this->options['link_to_node']),
43
    );
44

    
45
    parent::options_form($form, $form_state);
46
  }
47

    
48
  /**
49
   * Render whatever the data is as a link to the node.
50
   *
51
   * Data should be made XSS safe prior to calling this function.
52
   */
53
  function render_link($data, $values) {
54
    if (!empty($this->options['link_to_node']) && !empty($this->additional_fields['nid'])) {
55
      if ($data !== NULL && $data !== '') {
56
        $this->options['alter']['make_link'] = TRUE;
57
        $this->options['alter']['path'] = "node/" . $this->get_value($values, 'nid');
58
        if (isset($this->aliases['language'])) {
59
          $languages = language_list();
60
          $language = $this->get_value($values, 'language');
61
          if (isset($languages[$language])) {
62
            $this->options['alter']['language'] = $languages[$language];
63
          }
64
          else {
65
            unset($this->options['alter']['language']);
66
          }
67
        }
68
      }
69
      else {
70
        $this->options['alter']['make_link'] = FALSE;
71
      }
72
    }
73
    return $data;
74
  }
75

    
76
  function render($values) {
77
    $value = $this->get_value($values);
78
    return $this->render_link($this->sanitize_value($value), $values);
79
  }
80
}