views_plugin_display_attachment.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_display_attachment.
  5. */
  6. /**
  7. * The plugin that handles an attachment display.
  8. *
  9. * Attachment displays are secondary displays that are 'attached' to a primary
  10. * display. Effectively they are a simple way to get multiple views within
  11. * the same view. They can share some information.
  12. *
  13. * @ingroup views_display_plugins
  14. */
  15. class views_plugin_display_attachment extends views_plugin_display {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function option_definition () {
  20. $options = parent::option_definition();
  21. $options['show_title'] = array('default' => FALSE, 'bool' => TRUE);
  22. $options['show_title_empty'] = array('default' => FALSE, 'bool' => TRUE);
  23. $options['displays'] = array('default' => array());
  24. $options['attachment_position'] = array('default' => 'before');
  25. $options['inherit_arguments'] = array('default' => TRUE, 'bool' => TRUE);
  26. $options['inherit_exposed_filters'] = array('default' => FALSE, 'bool' => TRUE);
  27. $options['inherit_pager'] = array('default' => FALSE, 'bool' => TRUE);
  28. $options['render_pager'] = array('default' => FALSE, 'bool' => TRUE);
  29. return $options;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function execute() {
  35. return $this->view->render($this->display->id);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attachment_positions($position = NULL) {
  41. $positions = array(
  42. 'before' => t('Before'),
  43. 'after' => t('After'),
  44. 'both' => t('Both'),
  45. );
  46. if ($position) {
  47. return $positions[$position];
  48. }
  49. return $positions;
  50. }
  51. /**
  52. * Provide the summary for attachment options in the views UI.
  53. *
  54. * This output is returned as an array.
  55. */
  56. public function options_summary(&$categories, &$options) {
  57. // It is very important to call the parent function here.
  58. parent::options_summary($categories, $options);
  59. $categories['attachment'] = array(
  60. 'title' => t('Attachment settings'),
  61. 'column' => 'second',
  62. 'build' => array(
  63. '#weight' => -10,
  64. ),
  65. );
  66. $displays = array_filter($this->get_option('displays'));
  67. if (count($displays) > 1) {
  68. $attach_to = t('Multiple displays');
  69. }
  70. elseif (count($displays) == 1) {
  71. $display = array_shift($displays);
  72. if (!empty($this->view->display[$display])) {
  73. $attach_to = check_plain($this->view->display[$display]->display_title);
  74. }
  75. }
  76. if (!isset($attach_to)) {
  77. $attach_to = t('Not defined');
  78. }
  79. $options['show_title'] = array(
  80. 'category' => 'title',
  81. 'title' => t('Show title'),
  82. 'value' => $this->get_option('show_title') ? t('Yes') : t('No'),
  83. );
  84. $options['show_title_empty'] = array(
  85. 'category' => 'title',
  86. 'title' => t('Show title even if view has no results'),
  87. 'value' => $this->get_option('show_title_empty') ? t('Yes') : t('No'),
  88. );
  89. $options['displays'] = array(
  90. 'category' => 'attachment',
  91. 'title' => t('Attach to'),
  92. 'value' => $attach_to,
  93. );
  94. $options['attachment_position'] = array(
  95. 'category' => 'attachment',
  96. 'title' => t('Attachment position'),
  97. 'value' => $this->attachment_positions($this->get_option('attachment_position')),
  98. );
  99. $options['inherit_arguments'] = array(
  100. 'category' => 'attachment',
  101. 'title' => t('Inherit contextual filters'),
  102. 'value' => $this->get_option('inherit_arguments') ? t('Yes') : t('No'),
  103. );
  104. $options['inherit_exposed_filters'] = array(
  105. 'category' => 'attachment',
  106. 'title' => t('Inherit exposed filters'),
  107. 'value' => $this->get_option('inherit_exposed_filters') ? t('Yes') : t('No'),
  108. );
  109. $options['inherit_pager'] = array(
  110. 'category' => 'pager',
  111. 'title' => t('Inherit pager'),
  112. 'value' => $this->get_option('inherit_pager') ? t('Yes') : t('No'),
  113. );
  114. $options['render_pager'] = array(
  115. 'category' => 'pager',
  116. 'title' => t('Render pager'),
  117. 'value' => $this->get_option('render_pager') ? t('Yes') : t('No'),
  118. );
  119. }
  120. /**
  121. * Provide the default form for setting options.
  122. */
  123. public function options_form(&$form, &$form_state) {
  124. // It is very important to call the parent function here.
  125. parent::options_form($form, $form_state);
  126. switch ($form_state['section']) {
  127. case 'show_title':
  128. $form['#title'] .= t('Title');
  129. $form['show_title'] = array(
  130. '#type' => 'checkbox',
  131. '#title' => t('Show title'),
  132. '#description' => t('Do you want to show the title of the attachment?'),
  133. '#default_value' => $this->get_option('show_title'),
  134. );
  135. break;
  136. case 'show_title_empty':
  137. $form['#title'] .= t('Title');
  138. $form['show_title_empty'] = array(
  139. '#type' => 'checkbox',
  140. '#title' => t('Show title for empty view'),
  141. '#description' => t('Do you want to show the title of the attachment even if the view has no results?'),
  142. '#default_value' => $this->get_option('show_title_empty'),
  143. );
  144. break;
  145. case 'inherit_arguments':
  146. $form['#title'] .= t('Inherit contextual filters');
  147. $form['inherit_arguments'] = array(
  148. '#type' => 'checkbox',
  149. '#title' => t('Inherit'),
  150. '#description' => t('Should this display inherit its contextual filter values from the parent display to which it is attached?'),
  151. '#default_value' => $this->get_option('inherit_arguments'),
  152. );
  153. break;
  154. case 'inherit_exposed_filters':
  155. $form['#title'] .= t('Inherit exposed filters');
  156. $form['inherit_exposed_filters'] = array(
  157. '#type' => 'checkbox',
  158. '#title' => t('Inherit'),
  159. '#description' => t('Should this display inherit its exposed filter values from the parent display to which it is attached?'),
  160. '#default_value' => $this->get_option('inherit_exposed_filters'),
  161. );
  162. break;
  163. case 'inherit_pager':
  164. $form['#title'] .= t('Inherit pager');
  165. $form['inherit_pager'] = array(
  166. '#type' => 'checkbox',
  167. '#title' => t('Inherit'),
  168. '#description' => t('Should this display inherit its paging values from the parent display to which it is attached?'),
  169. '#default_value' => $this->get_option('inherit_pager'),
  170. );
  171. break;
  172. case 'render_pager':
  173. $form['#title'] .= t('Render pager');
  174. $form['render_pager'] = array(
  175. '#type' => 'checkbox',
  176. '#title' => t('Render'),
  177. '#description' => t('Should this display render the pager values? This is only meaningful if inheriting a pager.'),
  178. '#default_value' => $this->get_option('render_pager'),
  179. );
  180. break;
  181. case 'attachment_position':
  182. $form['#title'] .= t('Position');
  183. $form['attachment_position'] = array(
  184. '#type' => 'radios',
  185. '#description' => t('Attach before or after the parent display?'),
  186. '#options' => $this->attachment_positions(),
  187. '#default_value' => $this->get_option('attachment_position'),
  188. );
  189. break;
  190. case 'displays':
  191. $form['#title'] .= t('Attach to');
  192. $displays = array();
  193. foreach ($this->view->display as $display_id => $display) {
  194. if (!empty($display->handler) && $display->handler->accept_attachments()) {
  195. $displays[$display_id] = $display->display_title;
  196. }
  197. }
  198. $form['displays'] = array(
  199. '#type' => 'checkboxes',
  200. '#description' => t('Select which display or displays this should attach to.'),
  201. '#options' => $displays,
  202. '#default_value' => $this->get_option('displays'),
  203. );
  204. break;
  205. }
  206. }
  207. /**
  208. * Perform any necessary changes to the form values prior to storage.
  209. * There is no need for this function to actually store the data.
  210. */
  211. public function options_submit(&$form, &$form_state) {
  212. // It is very important to call the parent function here.
  213. parent::options_submit($form, $form_state);
  214. switch ($form_state['section']) {
  215. case 'show_title':
  216. case 'show_title_empty':
  217. case 'inherit_arguments':
  218. case 'inherit_pager':
  219. case 'render_pager':
  220. case 'inherit_exposed_filters':
  221. case 'attachment_position':
  222. case 'displays':
  223. $this->set_option($form_state['section'], $form_state['values'][$form_state['section']]);
  224. break;
  225. }
  226. }
  227. /**
  228. * Attach to another view.
  229. */
  230. public function attach_to($display_id) {
  231. $displays = $this->get_option('displays');
  232. if (empty($displays[$display_id])) {
  233. return;
  234. }
  235. if (!$this->access()) {
  236. return;
  237. }
  238. // Get a fresh view because our current one has a lot of stuff on it
  239. // because it's already been executed.
  240. $view = $this->view->clone_view();
  241. $view->original_args = $view->args;
  242. $args = $this->get_option('inherit_arguments') ? $this->view->args : array();
  243. $view->set_arguments($args);
  244. $exposed_input = $this->get_option('inherit_exposed_filters') && isset($this->view->exposed_input) ? $this->view->exposed_input : array();
  245. $view->set_exposed_input($exposed_input);
  246. $view->set_display($this->display->id);
  247. if ($this->get_option('inherit_pager')) {
  248. $view->display_handler->use_pager = $this->view->display[$display_id]->handler->use_pager();
  249. $view->display_handler->set_option('pager', $this->view->display[$display_id]->handler->get_option('pager'));
  250. }
  251. $attachment_output = $view->execute_display($this->display->id, $args);
  252. $attachment = '';
  253. if ($view->display_handler->get_option('show_title') && $view->display_handler->get_option('title')) {
  254. if ($view->display_handler->get_option('show_title_empty') || !empty($view->result)) {
  255. $attachment .= theme('html_tag', array(
  256. 'element' => array(
  257. '#tag' => 'h2',
  258. '#value' => $view->display_handler->get_option('title'),
  259. ),
  260. ));
  261. }
  262. }
  263. $attachment .= $attachment_output;
  264. switch ($this->get_option('attachment_position')) {
  265. case 'before':
  266. $this->view->attachment_before .= $attachment;
  267. break;
  268. case 'after':
  269. $this->view->attachment_after .= $attachment;
  270. break;
  271. case 'both':
  272. $this->view->attachment_before .= $attachment;
  273. $this->view->attachment_after .= $attachment;
  274. break;
  275. }
  276. $view->destroy();
  277. }
  278. /**
  279. * Attachment displays only use exposed widgets if they are set to inherit
  280. * the exposed filter settings of their parent display.
  281. */
  282. public function uses_exposed() {
  283. if (!empty($this->options['inherit_exposed_filters']) && parent::uses_exposed()) {
  284. return TRUE;
  285. }
  286. return FALSE;
  287. }
  288. /**
  289. * If an attachment is set to inherit the exposed filter settings from its
  290. * parent display, then don't render and display a second set of exposed
  291. * filter widgets.
  292. */
  293. public function displays_exposed() {
  294. return $this->options['inherit_exposed_filters'] ? FALSE : TRUE;
  295. }
  296. /**
  297. * {@inheritdoc}
  298. */
  299. public function use_pager() {
  300. return !empty($this->use_pager);
  301. }
  302. /**
  303. * {@inheritdoc}
  304. */
  305. public function render_pager() {
  306. return !empty($this->use_pager) && $this->get_option('render_pager');
  307. }
  308. }