Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / modules / comment / views_handler_field_comment.inc @ 5d12d676

1
<?php
2

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

    
8
/**
9
 * Field handler to allow linking to a comment.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_comment extends views_handler_field {
14

    
15
  /**
16
   * Override init function to provide generic option to link to comment.
17
   */
18
  public function init(&$view, &$options) {
19
    parent::init($view, $options);
20
    if (!empty($this->options['link_to_comment'])) {
21
      $this->additional_fields['cid'] = 'cid';
22
      $this->additional_fields['nid'] = 'nid';
23
    }
24
  }
25

    
26
  /**
27
   * {@inheritdoc}
28
   */
29
  public function option_definition() {
30
    $options = parent::option_definition();
31
    $options['link_to_comment'] = array('default' => TRUE, 'bool' => TRUE);
32
    $options['link_to_node'] = array('default' => FALSE, 'bool' => TRUE);
33

    
34
    return $options;
35
  }
36

    
37
  /**
38
   * Provide link-to-comment option.
39
   */
40
  public function options_form(&$form, &$form_state) {
41
    $form['link_to_comment'] = array(
42
      '#title' => t('Link this field to its comment'),
43
      '#description' => t("Enable to override this field's links."),
44
      '#type' => 'checkbox',
45
      '#default_value' => $this->options['link_to_comment'],
46
    );
47
    $form['link_to_node'] = array(
48
      '#title' => t('Link field to the node if there is no comment.'),
49
      '#type' => 'checkbox',
50
      '#default_value' => $this->options['link_to_node'],
51
    );
52
    parent::options_form($form, $form_state);
53
  }
54

    
55
  /**
56
   * {@inheritdoc}
57
   */
58
  public function render_link($data, $values) {
59
    if (!empty($this->options['link_to_comment'])) {
60
      $this->options['alter']['make_link'] = TRUE;
61
      $nid = $this->get_value($values, 'nid');
62
      $cid = $this->get_value($values, 'cid');
63
      if (!empty($cid)) {
64
        $this->options['alter']['path'] = "comment/" . $cid;
65
        $this->options['alter']['fragment'] = "comment-" . $cid;
66
      }
67
      // If there is no comment link to the node.
68
      elseif ($this->options['link_to_node']) {
69
        $this->options['alter']['path'] = "node/" . $nid;
70
      }
71
    }
72

    
73
    return $data;
74
  }
75

    
76
  /**
77
   * {@inheritdoc}
78
   */
79
  public function render($values) {
80
    $value = $this->get_value($values);
81
    return $this->render_link($this->sanitize_value($value), $values);
82
  }
83

    
84
}