views_plugin_display_feed.inc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_display_feed.
  5. */
  6. /**
  7. * The plugin that handles a feed, such as RSS or atom.
  8. *
  9. * For the most part, feeds are page displays but with some subtle differences.
  10. *
  11. * @ingroup views_display_plugins
  12. */
  13. class views_plugin_display_feed extends views_plugin_display_page {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function init(&$view, &$display, $options = NULL) {
  18. parent::init($view, $display, $options);
  19. // Set the default row style. Ideally this would be part of the option
  20. // definition, but in this case it's dependent on the view's base table,
  21. // which we don't know until init().
  22. $row_plugins = views_fetch_plugin_names('row', $this->get_style_type(), array($view->base_table));
  23. $default_row_plugin = key($row_plugins);
  24. if ($this->options['row_plugin'] == '') {
  25. $this->options['row_plugin'] = $default_row_plugin;
  26. }
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function uses_breadcrumb() {
  32. return FALSE;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function get_style_type() {
  38. return 'feed';
  39. }
  40. /**
  41. * Feeds do not go through the normal page theming mechanism. Instead, they
  42. * go through their own little theme function and then return NULL so that
  43. * Drupal believes that the page has already rendered itself...which it has.
  44. */
  45. public function execute() {
  46. $output = $this->view->render();
  47. if (!empty($this->view->build_info['denied'])) {
  48. return MENU_ACCESS_DENIED;
  49. }
  50. if (empty($output)) {
  51. return MENU_NOT_FOUND;
  52. }
  53. print $output;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function preview() {
  59. if (!empty($this->view->live_preview)) {
  60. return '<pre>' . check_plain($this->view->render()) . '</pre>';
  61. }
  62. return $this->view->render();
  63. }
  64. /**
  65. * Instead of going through the standard views_view.tpl.php, delegate this
  66. * to the style handler.
  67. */
  68. public function render() {
  69. return $this->view->style_plugin->render($this->view->result);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function defaultable_sections($section = NULL) {
  75. if (in_array($section, array('style_options', 'style_plugin', 'row_options', 'row_plugin',))) {
  76. return FALSE;
  77. }
  78. $sections = parent::defaultable_sections($section);
  79. // Tell views our sitename_title option belongs in the title section.
  80. if ($section == 'title') {
  81. $sections[] = 'sitename_title';
  82. }
  83. elseif (!$section) {
  84. $sections['title'][] = 'sitename_title';
  85. }
  86. return $sections;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function option_definition() {
  92. $options = parent::option_definition();
  93. $options['displays'] = array('default' => array());
  94. // Overrides for standard stuff.
  95. $options['style_plugin']['default'] = 'rss';
  96. $options['style_options']['default'] = array('description' => '');
  97. $options['sitename_title']['default'] = FALSE;
  98. $options['row_plugin']['default'] = '';
  99. $options['defaults']['default']['style_plugin'] = FALSE;
  100. $options['defaults']['default']['style_options'] = FALSE;
  101. $options['defaults']['default']['row_plugin'] = FALSE;
  102. $options['defaults']['default']['row_options'] = FALSE;
  103. return $options;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function options_summary(&$categories, &$options) {
  109. // It is very important to call the parent function here.
  110. parent::options_summary($categories, $options);
  111. // Since we're childing off the 'page' type, we'll still *call* our
  112. // category 'page' but let's override it so it says feed settings.
  113. $categories['page'] = array(
  114. 'title' => t('Feed settings'),
  115. 'column' => 'second',
  116. 'build' => array(
  117. '#weight' => -10,
  118. ),
  119. );
  120. if ($this->get_option('sitename_title')) {
  121. $options['title']['value'] = t('Using the site name');
  122. }
  123. // I don't think we want to give feeds menus directly.
  124. unset($options['menu']);
  125. $displays = array_filter($this->get_option('displays'));
  126. if (count($displays) > 1) {
  127. $attach_to = t('Multiple displays');
  128. }
  129. elseif (count($displays) == 1) {
  130. $display = array_shift($displays);
  131. if (!empty($this->view->display[$display])) {
  132. $attach_to = check_plain($this->view->display[$display]->display_title);
  133. }
  134. }
  135. if (!isset($attach_to)) {
  136. $attach_to = t('None');
  137. }
  138. $options['displays'] = array(
  139. 'category' => 'page',
  140. 'title' => t('Attach to'),
  141. 'value' => $attach_to,
  142. );
  143. }
  144. /**
  145. * Provide the default form for setting options.
  146. */
  147. public function options_form(&$form, &$form_state) {
  148. // It is very important to call the parent function here.
  149. parent::options_form($form, $form_state);
  150. switch ($form_state['section']) {
  151. case 'title':
  152. $title = $form['title'];
  153. // A little juggling to move the 'title' field beyond our checkbox.
  154. unset($form['title']);
  155. $form['sitename_title'] = array(
  156. '#type' => 'checkbox',
  157. '#title' => t('Use the site name for the title'),
  158. '#default_value' => $this->get_option('sitename_title'),
  159. );
  160. $form['title'] = $title;
  161. $form['title']['#dependency'] = array('edit-sitename-title' => array(FALSE));
  162. break;
  163. case 'displays':
  164. $form['#title'] .= t('Attach to');
  165. $displays = array();
  166. foreach ($this->view->display as $display_id => $display) {
  167. if (!empty($display->handler) && $display->handler->accept_attachments()) {
  168. $displays[$display_id] = $display->display_title;
  169. }
  170. }
  171. $form['displays'] = array(
  172. '#type' => 'checkboxes',
  173. '#description' => t('The feed icon will be available only to the selected displays.'),
  174. '#options' => $displays,
  175. '#default_value' => $this->get_option('displays'),
  176. );
  177. break;
  178. case 'path':
  179. $form['path']['#description'] = t('This view will be displayed by visiting this path on your site. It is recommended that the path be something like "path/%/%/feed" or "path/%/%/rss.xml", putting one % in the path for each contextual filter you have defined in the view.');
  180. break;
  181. }
  182. }
  183. /**
  184. * Perform any necessary changes to the form values prior to storage.
  185. * There is no need for this function to actually store the data.
  186. */
  187. public function options_submit(&$form, &$form_state) {
  188. // It is very important to call the parent function here.
  189. parent::options_submit($form, $form_state);
  190. switch ($form_state['section']) {
  191. case 'title':
  192. $this->set_option('sitename_title', $form_state['values']['sitename_title']);
  193. break;
  194. case 'displays':
  195. $this->set_option($form_state['section'], $form_state['values'][$form_state['section']]);
  196. break;
  197. }
  198. }
  199. /**
  200. * Attach to another view.
  201. */
  202. public function attach_to($display_id) {
  203. $displays = $this->get_option('displays');
  204. if (empty($displays[$display_id])) {
  205. return;
  206. }
  207. // Defer to the feed style; it may put in meta information, and/or
  208. // attach a feed icon.
  209. $plugin = $this->get_plugin();
  210. if ($plugin) {
  211. $clone = $this->view->clone_view();
  212. $clone->set_display($this->display->id);
  213. $clone->build_title();
  214. $plugin->attach_to($display_id, $this->get_path(), $clone->get_title());
  215. // Clean up
  216. $clone->destroy();
  217. unset($clone);
  218. }
  219. }
  220. /**
  221. * {@inheritdoc}
  222. */
  223. public function uses_link_display() {
  224. return TRUE;
  225. }
  226. }