views_plugin_row_rss_fields.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_row_rss_fields.
  5. */
  6. /**
  7. * Renders an RSS item based on fields.
  8. */
  9. class views_plugin_row_rss_fields extends views_plugin_row {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function option_definition() {
  14. $options = parent::option_definition();
  15. $options['title_field'] = array('default' => '');
  16. $options['link_field'] = array('default' => '');
  17. $options['description_field'] = array('default' => '');
  18. $options['creator_field'] = array('default' => '');
  19. $options['date_field'] = array('default' => '');
  20. $options['guid_field_options']['guid_field'] = array('default' => '');
  21. $options['guid_field_options']['guid_field_is_permalink'] = array('default' => TRUE, 'bool' => TRUE);
  22. return $options;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function options_form(&$form, &$form_state) {
  28. parent::options_form($form, $form_state);
  29. $initial_labels = array('' => t('- None -'));
  30. $view_fields_labels = $this->display->handler->get_field_labels();
  31. $view_fields_labels = array_merge($initial_labels, $view_fields_labels);
  32. $form['title_field'] = array(
  33. '#type' => 'select',
  34. '#title' => t('Title field'),
  35. '#description' => t('The field that is going to be used as the RSS item title for each row.'),
  36. '#options' => $view_fields_labels,
  37. '#default_value' => $this->options['title_field'],
  38. '#required' => TRUE,
  39. );
  40. $form['link_field'] = array(
  41. '#type' => 'select',
  42. '#title' => t('Link field'),
  43. '#description' => t('The field that is going to be used as the RSS item link for each row. This must be a drupal relative path.'),
  44. '#options' => $view_fields_labels,
  45. '#default_value' => $this->options['link_field'],
  46. '#required' => TRUE,
  47. );
  48. $form['description_field'] = array(
  49. '#type' => 'select',
  50. '#title' => t('Description field'),
  51. '#description' => t('The field that is going to be used as the RSS item description for each row.'),
  52. '#options' => $view_fields_labels,
  53. '#default_value' => $this->options['description_field'],
  54. '#required' => TRUE,
  55. );
  56. $form['creator_field'] = array(
  57. '#type' => 'select',
  58. '#title' => t('Creator field'),
  59. '#description' => t('The field that is going to be used as the RSS item creator for each row.'),
  60. '#options' => $view_fields_labels,
  61. '#default_value' => $this->options['creator_field'],
  62. '#required' => TRUE,
  63. );
  64. $form['date_field'] = array(
  65. '#type' => 'select',
  66. '#title' => t('Publication date field'),
  67. '#description' => t('The field that is going to be used as the RSS item pubDate for each row. It needs to be in RFC 2822 format.'),
  68. '#options' => $view_fields_labels,
  69. '#default_value' => $this->options['date_field'],
  70. '#required' => TRUE,
  71. );
  72. $form['guid_field_options'] = array(
  73. '#type' => 'fieldset',
  74. '#title' => t('GUID settings'),
  75. '#collapsible' => FALSE,
  76. '#collapsed' => FALSE,
  77. );
  78. $form['guid_field_options']['guid_field'] = array(
  79. '#type' => 'select',
  80. '#title' => t('GUID field'),
  81. '#description' => t('The globally unique identifier of the RSS item.'),
  82. '#options' => $view_fields_labels,
  83. '#default_value' => $this->options['guid_field_options']['guid_field'],
  84. '#required' => TRUE,
  85. );
  86. $form['guid_field_options']['guid_field_is_permalink'] = array(
  87. '#type' => 'checkbox',
  88. '#title' => t('GUID is permalink'),
  89. '#description' => t('The RSS item GUID is a permalink.'),
  90. '#default_value' => $this->options['guid_field_options']['guid_field_is_permalink'],
  91. );
  92. }
  93. /**
  94. *
  95. */
  96. public function validate() {
  97. $errors = parent::validate();
  98. $required_options = array('title_field', 'link_field', 'description_field', 'creator_field', 'date_field');
  99. foreach ($required_options as $required_option) {
  100. if (empty($this->options[$required_option])) {
  101. $errors[] = t('Row style plugin requires specifying which views fields to use for RSS item.');
  102. break;
  103. }
  104. }
  105. // Once more for guid.
  106. if (empty($this->options['guid_field_options']['guid_field'])) {
  107. $errors[] = t('Row style plugin requires specifying which views fields to use for RSS item.');
  108. }
  109. return $errors;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function render($row) {
  115. static $row_index;
  116. if (!isset($row_index)) {
  117. $row_index = 0;
  118. }
  119. if (function_exists('rdf_get_namespaces')) {
  120. // Merge RDF namespaces in the XML namespaces in case they are used
  121. // further in the RSS content.
  122. $xml_rdf_namespaces = array();
  123. foreach (rdf_get_namespaces() as $prefix => $uri) {
  124. $xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
  125. }
  126. $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
  127. }
  128. // Create the RSS item object.
  129. $item = new stdClass();
  130. $item->title = $this->get_field($row_index, $this->options['title_field']);
  131. $item->link = url($this->get_field($row_index, $this->options['link_field']), array('absolute' => TRUE));
  132. $item->description = $this->get_field($row_index, $this->options['description_field']);
  133. $item->elements = array(
  134. array('key' => 'pubDate', 'value' => $this->get_field($row_index, $this->options['date_field'])),
  135. array(
  136. 'key' => 'dc:creator',
  137. 'value' => $this->get_field($row_index, $this->options['creator_field']),
  138. 'namespace' => array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/'),
  139. ),
  140. );
  141. $guid_is_permalink_string = 'false';
  142. $item_guid = $this->get_field($row_index, $this->options['guid_field_options']['guid_field']);
  143. if ($this->options['guid_field_options']['guid_field_is_permalink']) {
  144. $guid_is_permalink_string = 'true';
  145. $item_guid = url($item_guid, array('absolute' => TRUE));
  146. }
  147. $item->elements[] = array(
  148. 'key' => 'guid',
  149. 'value' => $item_guid,
  150. 'attributes' => array('isPermaLink' => $guid_is_permalink_string),
  151. );
  152. $row_index++;
  153. foreach ($item->elements as $element) {
  154. if (isset($element['namespace'])) {
  155. $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $element['namespace']);
  156. }
  157. }
  158. return theme($this->theme_functions(),
  159. array(
  160. 'view' => $this->view,
  161. 'options' => $this->options,
  162. 'row' => $item,
  163. 'field_alias' => isset($this->field_alias) ? $this->field_alias : '',
  164. ));
  165. }
  166. /**
  167. * Retrieves a views field value from the style plugin.
  168. *
  169. * @param int $index
  170. * The index of the row as expected by views_plugin_style::get_field().
  171. * @param string $field_id
  172. * The ID assigned to the required field in the display.
  173. */
  174. public function get_field($index, $field_id) {
  175. if (empty($this->view->style_plugin) || !is_object($this->view->style_plugin) || empty($field_id)) {
  176. return '';
  177. }
  178. return $this->view->style_plugin->get_field($index, $field_id);
  179. }
  180. }