views_plugin_display_attachment.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * @file
  4. * Contains the attachment display plugin.
  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. function option_definition () {
  17. $options = parent::option_definition();
  18. $options['displays'] = array('default' => array());
  19. $options['attachment_position'] = array('default' => 'before');
  20. $options['inherit_arguments'] = array('default' => TRUE, 'bool' => TRUE);
  21. $options['inherit_exposed_filters'] = array('default' => FALSE, 'bool' => TRUE);
  22. $options['inherit_pager'] = array('default' => FALSE, 'bool' => TRUE);
  23. $options['render_pager'] = array('default' => FALSE, 'bool' => TRUE);
  24. return $options;
  25. }
  26. function execute() {
  27. return $this->view->render($this->display->id);
  28. }
  29. function attachment_positions($position = NULL) {
  30. $positions = array(
  31. 'before' => t('Before'),
  32. 'after' => t('After'),
  33. 'both' => t('Both'),
  34. );
  35. if ($position) {
  36. return $positions[$position];
  37. }
  38. return $positions;
  39. }
  40. /**
  41. * Provide the summary for attachment options in the views UI.
  42. *
  43. * This output is returned as an array.
  44. */
  45. function options_summary(&$categories, &$options) {
  46. // It is very important to call the parent function here:
  47. parent::options_summary($categories, $options);
  48. $categories['attachment'] = array(
  49. 'title' => t('Attachment settings'),
  50. 'column' => 'second',
  51. 'build' => array(
  52. '#weight' => -10,
  53. ),
  54. );
  55. $displays = array_filter($this->get_option('displays'));
  56. if (count($displays) > 1) {
  57. $attach_to = t('Multiple displays');
  58. }
  59. elseif (count($displays) == 1) {
  60. $display = array_shift($displays);
  61. if (!empty($this->view->display[$display])) {
  62. $attach_to = check_plain($this->view->display[$display]->display_title);
  63. }
  64. }
  65. if (!isset($attach_to)) {
  66. $attach_to = t('Not defined');
  67. }
  68. $options['displays'] = array(
  69. 'category' => 'attachment',
  70. 'title' => t('Attach to'),
  71. 'value' => $attach_to,
  72. );
  73. $options['attachment_position'] = array(
  74. 'category' => 'attachment',
  75. 'title' => t('Attachment position'),
  76. 'value' => $this->attachment_positions($this->get_option('attachment_position')),
  77. );
  78. $options['inherit_arguments'] = array(
  79. 'category' => 'attachment',
  80. 'title' => t('Inherit contextual filters'),
  81. 'value' => $this->get_option('inherit_arguments') ? t('Yes') : t('No'),
  82. );
  83. $options['inherit_exposed_filters'] = array(
  84. 'category' => 'attachment',
  85. 'title' => t('Inherit exposed filters'),
  86. 'value' => $this->get_option('inherit_exposed_filters') ? t('Yes') : t('No'),
  87. );
  88. $options['inherit_pager'] = array(
  89. 'category' => 'pager',
  90. 'title' => t('Inherit pager'),
  91. 'value' => $this->get_option('inherit_pager') ? t('Yes') : t('No'),
  92. );
  93. $options['render_pager'] = array(
  94. 'category' => 'pager',
  95. 'title' => t('Render pager'),
  96. 'value' => $this->get_option('render_pager') ? t('Yes') : t('No'),
  97. );
  98. }
  99. /**
  100. * Provide the default form for setting options.
  101. */
  102. function options_form(&$form, &$form_state) {
  103. // It is very important to call the parent function here:
  104. parent::options_form($form, $form_state);
  105. switch ($form_state['section']) {
  106. case 'inherit_arguments':
  107. $form['#title'] .= t('Inherit contextual filters');
  108. $form['inherit_arguments'] = array(
  109. '#type' => 'checkbox',
  110. '#title' => t('Inherit'),
  111. '#description' => t('Should this display inherit its contextual filter values from the parent display to which it is attached?'),
  112. '#default_value' => $this->get_option('inherit_arguments'),
  113. );
  114. break;
  115. case 'inherit_exposed_filters':
  116. $form['#title'] .= t('Inherit exposed filters');
  117. $form['inherit_exposed_filters'] = array(
  118. '#type' => 'checkbox',
  119. '#title' => t('Inherit'),
  120. '#description' => t('Should this display inherit its exposed filter values from the parent display to which it is attached?'),
  121. '#default_value' => $this->get_option('inherit_exposed_filters'),
  122. );
  123. break;
  124. case 'inherit_pager':
  125. $form['#title'] .= t('Inherit pager');
  126. $form['inherit_pager'] = array(
  127. '#type' => 'checkbox',
  128. '#title' => t('Inherit'),
  129. '#description' => t('Should this display inherit its paging values from the parent display to which it is attached?'),
  130. '#default_value' => $this->get_option('inherit_pager'),
  131. );
  132. break;
  133. case 'render_pager':
  134. $form['#title'] .= t('Render pager');
  135. $form['render_pager'] = array(
  136. '#type' => 'checkbox',
  137. '#title' => t('Render'),
  138. '#description' => t('Should this display render the pager values? This is only meaningful if inheriting a pager.'),
  139. '#default_value' => $this->get_option('render_pager'),
  140. );
  141. break;
  142. case 'attachment_position':
  143. $form['#title'] .= t('Position');
  144. $form['attachment_position'] = array(
  145. '#type' => 'radios',
  146. '#description' => t('Attach before or after the parent display?'),
  147. '#options' => $this->attachment_positions(),
  148. '#default_value' => $this->get_option('attachment_position'),
  149. );
  150. break;
  151. case 'displays':
  152. $form['#title'] .= t('Attach to');
  153. $displays = array();
  154. foreach ($this->view->display as $display_id => $display) {
  155. if (!empty($display->handler) && $display->handler->accept_attachments()) {
  156. $displays[$display_id] = $display->display_title;
  157. }
  158. }
  159. $form['displays'] = array(
  160. '#type' => 'checkboxes',
  161. '#description' => t('Select which display or displays this should attach to.'),
  162. '#options' => $displays,
  163. '#default_value' => $this->get_option('displays'),
  164. );
  165. break;
  166. }
  167. }
  168. /**
  169. * Perform any necessary changes to the form values prior to storage.
  170. * There is no need for this function to actually store the data.
  171. */
  172. function options_submit(&$form, &$form_state) {
  173. // It is very important to call the parent function here:
  174. parent::options_submit($form, $form_state);
  175. switch ($form_state['section']) {
  176. case 'inherit_arguments':
  177. case 'inherit_pager':
  178. case 'render_pager':
  179. case 'inherit_exposed_filters':
  180. case 'attachment_position':
  181. case 'displays':
  182. $this->set_option($form_state['section'], $form_state['values'][$form_state['section']]);
  183. break;
  184. }
  185. }
  186. /**
  187. * Attach to another view.
  188. */
  189. function attach_to($display_id) {
  190. $displays = $this->get_option('displays');
  191. if (empty($displays[$display_id])) {
  192. return;
  193. }
  194. if (!$this->access()) {
  195. return;
  196. }
  197. // Get a fresh view because our current one has a lot of stuff on it because it's
  198. // already been executed.
  199. $view = $this->view->clone_view();
  200. $view->original_args = $view->args;
  201. $args = $this->get_option('inherit_arguments') ? $this->view->args : array();
  202. $view->set_arguments($args);
  203. $exposed_input = $this->get_option('inherit_exposed_filters') ? $this->view->exposed_input : array();
  204. $view->set_exposed_input($exposed_input);
  205. $view->set_display($this->display->id);
  206. if ($this->get_option('inherit_pager')) {
  207. $view->display_handler->use_pager = $this->view->display[$display_id]->handler->use_pager();
  208. $view->display_handler->set_option('pager', $this->view->display[$display_id]->handler->get_option('pager'));
  209. }
  210. $attachment = $view->execute_display($this->display->id, $args);
  211. switch ($this->get_option('attachment_position')) {
  212. case 'before':
  213. $this->view->attachment_before .= $attachment;
  214. break;
  215. case 'after':
  216. $this->view->attachment_after .= $attachment;
  217. break;
  218. case 'both':
  219. $this->view->attachment_before .= $attachment;
  220. $this->view->attachment_after .= $attachment;
  221. break;
  222. }
  223. $view->destroy();
  224. }
  225. /**
  226. * Attachment displays only use exposed widgets if
  227. * they are set to inherit the exposed filter settings
  228. * of their parent display.
  229. */
  230. function uses_exposed() {
  231. if (!empty($this->options['inherit_exposed_filters']) && parent::uses_exposed()) {
  232. return TRUE;
  233. }
  234. return FALSE;
  235. }
  236. /**
  237. * If an attachment is set to inherit the exposed filter
  238. * settings from its parent display, then don't render and
  239. * display a second set of exposed filter widgets.
  240. */
  241. function displays_exposed() {
  242. return $this->options['inherit_exposed_filters'] ? FALSE : TRUE;
  243. }
  244. function use_pager() {
  245. return !empty($this->use_pager);
  246. }
  247. function render_pager() {
  248. return !empty($this->use_pager) && $this->get_option('render_pager');
  249. }
  250. }