Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / modules / node / views_plugin_row_node_rss.inc @ 6f57d8c7

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the node RSS row style plugin.
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
  // Basic properties that let the row style follow relationships.
14
  var $base_table = 'node';
15
  var $base_field = 'nid';
16

    
17
  // Stores the nodes loaded with pre_render.
18
  var $nodes = array();
19

    
20
  function option_definition() {
21
    $options = parent::option_definition();
22

    
23
    $options['item_length'] = array('default' => 'default');
24
    $options['links'] = array('default' => FALSE, 'bool' => TRUE);
25

    
26
    return $options;
27
  }
28

    
29
  /**
30
   * Override init function to convert fulltext view-mode to full.
31
   */
32
  function init(&$view, &$display, $options = NULL) {
33
    parent::init($view, $display, $options);
34

    
35
    if ($this->options['item_length'] == 'fulltext') {
36
      $this->options['item_length'] = 'full';
37
    }
38
  }
39

    
40
  function options_form(&$form, &$form_state) {
41
    parent::options_form($form, $form_state);
42

    
43
    $form['item_length'] = array(
44
      '#type' => 'select',
45
      '#title' => t('Display type'),
46
      '#options' => $this->options_form_summary_options(),
47
      '#default_value' => $this->options['item_length'],
48
    );
49
    $form['links'] = array(
50
      '#type' => 'checkbox',
51
      '#title' => t('Display links'),
52
      '#default_value' => $this->options['links'],
53
    );
54
  }
55

    
56
  /**
57
   * Return the main options, which are shown in the summary title.
58
   */
59
  function options_form_summary_options() {
60
    $entity_info = entity_get_info('node');
61
    $options = array();
62
    if (!empty($entity_info['view modes'])) {
63
      foreach ($entity_info['view modes'] as $mode => $settings) {
64
        $options[$mode] = $settings['label'];
65
      }
66
    }
67
    $options['title'] = t('Title only');
68
    $options['default'] = t('Use site default RSS settings');
69
    return $options;
70
  }
71

    
72
  function summary_title() {
73
    $options = $this->options_form_summary_options();
74
    return check_plain($options[$this->options['item_length']]);
75
  }
76

    
77

    
78
  function pre_render($values) {
79
    $nids = array();
80
    foreach ($values as $row) {
81
      $nids[] = $row->{$this->field_alias};
82
    }
83
    if (!empty($nids)) {
84
      $this->nodes = node_load_multiple($nids);
85
    }
86
  }
87

    
88
  function render($row) {
89
    // For the most part, this code is taken from node_feed() in node.module
90
    global $base_url;
91

    
92
    $nid = $row->{$this->field_alias};
93
    if (!is_numeric($nid)) {
94
      return;
95
    }
96

    
97
    $display_mode = $this->options['item_length'];
98
    if ($display_mode == 'default') {
99
      $display_mode = variable_get('feed_item_length', 'teaser');
100
    }
101

    
102
    // Load the specified node:
103
    $node = $this->nodes[$nid];
104
    if (empty($node)) {
105
      return;
106
    }
107

    
108
    $item_text = '';
109

    
110
    $uri = entity_uri('node', $node);
111
    $node->link = url($uri['path'], $uri['options'] + array('absolute' => TRUE));
112
    $node->rss_namespaces = array();
113
    $node->rss_elements = array(
114
      array(
115
        'key' => 'pubDate',
116
        'value' => gmdate('r', $node->created),
117
      ),
118
      array(
119
        'key' => 'dc:creator',
120
        'value' => format_username($node),
121
      ),
122
      array(
123
        'key' => 'guid',
124
        'value' => $node->nid . ' at ' . $base_url,
125
        'attributes' => array('isPermaLink' => 'false'),
126
      ),
127
    );
128

    
129
    // The node gets built and modules add to or modify $node->rss_elements
130
    // and $node->rss_namespaces.
131

    
132
    $build_mode = $display_mode;
133

    
134
    $build = node_view($node, $build_mode);
135
    unset($build['#theme']);
136

    
137
    if (!empty($node->rss_namespaces)) {
138
      $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $node->rss_namespaces);
139
    }
140
    elseif (function_exists('rdf_get_namespaces')) {
141
      // Merge RDF namespaces in the XML namespaces in case they are used
142
      // further in the RSS content.
143
      $xml_rdf_namespaces = array();
144
      foreach (rdf_get_namespaces() as $prefix => $uri) {
145
        $xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
146
      }
147
      $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
148
    }
149

    
150
    // Hide the links if desired.
151
    if (!$this->options['links']) {
152
      hide($build['links']);
153
    }
154

    
155
    if ($display_mode != 'title') {
156
      // We render node contents and force links to be last.
157
      $build['links']['#weight'] = 1000;
158
      $item_text .= drupal_render($build);
159
    }
160

    
161
    $item = new stdClass();
162
    $item->description = $item_text;
163
    $item->title = $node->title;
164
    $item->link = $node->link;
165
    $item->elements = $node->rss_elements;
166
    $item->nid = $node->nid;
167

    
168
    return theme($this->theme_functions(), array(
169
      'view' => $this->view,
170
      'options' => $this->options,
171
      'row' => $item
172
    ));
173
  }
174
}