Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / modules / node / views_plugin_row_node_rss.inc @ 5d12d676

1
<?php
2

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

    
8
/**
9
 * Plugin which performs a node_view on the resulting object
10
 * and formats it as an RSS item.
11
 */
12
class views_plugin_row_node_rss extends views_plugin_row {
13

    
14
  /**
15
   * Basic properties that let the row style follow relationships.
16
   */
17
  public $base_table = 'node';
18

    
19
  /**
20
   *
21
   */
22
  public $base_field = 'nid';
23

    
24
  /**
25
   * Stores the nodes loaded with pre_render.
26
   */
27
  public $nodes = array();
28

    
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function option_definition() {
33
    $options = parent::option_definition();
34

    
35
    $options['item_length'] = array('default' => 'default');
36
    $options['links'] = array('default' => FALSE, 'bool' => TRUE);
37

    
38
    return $options;
39
  }
40

    
41
  /**
42
   * Override init function to convert fulltext view-mode to full.
43
   */
44
  public function init(&$view, &$display, $options = NULL) {
45
    parent::init($view, $display, $options);
46

    
47
    if ($this->options['item_length'] == 'fulltext') {
48
      $this->options['item_length'] = 'full';
49
    }
50
  }
51

    
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public function options_form(&$form, &$form_state) {
56
    parent::options_form($form, $form_state);
57

    
58
    $form['item_length'] = array(
59
      '#type' => 'select',
60
      '#title' => t('Display type'),
61
      '#options' => $this->options_form_summary_options(),
62
      '#default_value' => $this->options['item_length'],
63
    );
64
    $form['links'] = array(
65
      '#type' => 'checkbox',
66
      '#title' => t('Display links'),
67
      '#default_value' => $this->options['links'],
68
    );
69
  }
70

    
71
  /**
72
   * Return the main options, which are shown in the summary title.
73
   */
74
  public function options_form_summary_options() {
75
    $entity_info = entity_get_info('node');
76
    $options = array();
77
    if (!empty($entity_info['view modes'])) {
78
      foreach ($entity_info['view modes'] as $mode => $settings) {
79
        $options[$mode] = $settings['label'];
80
      }
81
    }
82
    $options['title'] = t('Title only');
83
    $options['default'] = t('Use site default RSS settings');
84
    return $options;
85
  }
86

    
87
  /**
88
   * {@inheritdoc}
89
   */
90
  public function summary_title() {
91
    $options = $this->options_form_summary_options();
92
    return check_plain($options[$this->options['item_length']]);
93
  }
94

    
95
  /**
96
   * {@inheritdoc}
97
   */
98
  public function pre_render($values) {
99
    $nids = array();
100
    foreach ($values as $row) {
101
      $nids[] = $row->{$this->field_alias};
102
    }
103
    if (!empty($nids)) {
104
      $this->nodes = node_load_multiple($nids);
105
    }
106
  }
107

    
108
  /**
109
   * {@inheritdoc}
110
   */
111
  public function render($row) {
112
    // For the most part; This code is taken from node_feed() in node.module.
113
    global $base_url;
114

    
115
    $nid = $row->{$this->field_alias};
116
    if (!is_numeric($nid)) {
117
      return;
118
    }
119

    
120
    $display_mode = $this->options['item_length'];
121
    if ($display_mode == 'default') {
122
      $display_mode = variable_get('feed_item_length', 'teaser');
123
    }
124

    
125
    // Load the specified node.
126
    $node = $this->nodes[$nid];
127
    if (empty($node)) {
128
      return;
129
    }
130

    
131
    $item_text = '';
132

    
133
    $uri = entity_uri('node', $node);
134
    $node->link = url($uri['path'], $uri['options'] + array('absolute' => TRUE));
135
    $node->rss_namespaces = array();
136
    $node->rss_elements = array(
137
      array(
138
        'key' => 'pubDate',
139
        'value' => gmdate('r', $node->created),
140
      ),
141
      array(
142
        'key' => 'dc:creator',
143
        'value' => format_username($node),
144
      ),
145
      array(
146
        'key' => 'guid',
147
        'value' => $node->nid . ' at ' . $base_url,
148
        'attributes' => array('isPermaLink' => 'false'),
149
      ),
150
    );
151

    
152
    // The node gets built and modules add to or modify $node->rss_elements
153
    // and $node->rss_namespaces.
154
    $build_mode = $display_mode;
155

    
156
    $build = node_view($node, $build_mode);
157
    unset($build['#theme']);
158

    
159
    if (!empty($node->rss_namespaces)) {
160
      $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $node->rss_namespaces);
161
    }
162
    elseif (function_exists('rdf_get_namespaces')) {
163
      // Merge RDF namespaces in the XML namespaces in case they are used
164
      // further in the RSS content.
165
      $xml_rdf_namespaces = array();
166
      foreach (rdf_get_namespaces() as $prefix => $uri) {
167
        $xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
168
      }
169
      $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
170
    }
171

    
172
    // Hide the links if desired.
173
    if (!$this->options['links']) {
174
      hide($build['links']);
175
    }
176

    
177
    if ($display_mode != 'title') {
178
      // We render node contents and force links to be last.
179
      $build['links']['#weight'] = 1000;
180
      $item_text .= drupal_render($build);
181
    }
182

    
183
    $item = new stdClass();
184
    $item->description = $item_text;
185
    $item->title = $node->title;
186
    $item->link = $node->link;
187
    $item->elements = $node->rss_elements;
188
    $item->nid = $node->nid;
189

    
190
    return theme($this->theme_functions(), array(
191
      'view' => $this->view,
192
      'options' => $this->options,
193
      'row' => $item,
194
    ));
195
  }
196

    
197
}