views_plugin_display_attachment.inc 9.3 KB

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