views_plugin_row_rss_fields.inc 6.8 KB

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