PageRenderTime 29ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/views/src/Plugin/views/display/Attachment.php

https://gitlab.com/reasonat/test8
PHP | 293 lines | 207 code | 36 blank | 50 comment | 16 complexity | 6c90ffe9cc4a4e94b54791c998e210ff MD5 | raw file
  1. <?php
  2. namespace Drupal\views\Plugin\views\display;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\views\ViewExecutable;
  5. /**
  6. * The plugin that handles an attachment display.
  7. *
  8. * Attachment displays are secondary displays that are 'attached' to a primary
  9. * display. Effectively they are a simple way to get multiple views within
  10. * the same view. They can share some information.
  11. *
  12. * @ingroup views_display_plugins
  13. *
  14. * @ViewsDisplay(
  15. * id = "attachment",
  16. * title = @Translation("Attachment"),
  17. * help = @Translation("Attachments added to other displays to achieve multiple views in the same view."),
  18. * theme = "views_view",
  19. * contextual_links_locations = {""}
  20. * )
  21. */
  22. class Attachment extends DisplayPluginBase {
  23. /**
  24. * Whether the display allows the use of a pager or not.
  25. *
  26. * @var bool
  27. */
  28. protected $usesPager = FALSE;
  29. protected function defineOptions() {
  30. $options = parent::defineOptions();
  31. $options['displays'] = array('default' => array());
  32. $options['attachment_position'] = array('default' => 'before');
  33. $options['inherit_arguments'] = array('default' => TRUE);
  34. $options['inherit_exposed_filters'] = array('default' => FALSE);
  35. $options['inherit_pager'] = array('default' => FALSE);
  36. $options['render_pager'] = array('default' => FALSE);
  37. return $options;
  38. }
  39. public function execute() {
  40. return $this->view->render($this->display['id']);
  41. }
  42. public function attachmentPositions($position = NULL) {
  43. $positions = array(
  44. 'before' => $this->t('Before'),
  45. 'after' => $this->t('After'),
  46. 'both' => $this->t('Both'),
  47. );
  48. if ($position) {
  49. return $positions[$position];
  50. }
  51. return $positions;
  52. }
  53. /**
  54. * Provide the summary for attachment options in the views UI.
  55. *
  56. * This output is returned as an array.
  57. */
  58. public function optionsSummary(&$categories, &$options) {
  59. // It is very important to call the parent function here:
  60. parent::optionsSummary($categories, $options);
  61. $categories['attachment'] = array(
  62. 'title' => $this->t('Attachment settings'),
  63. 'column' => 'second',
  64. 'build' => array(
  65. '#weight' => -10,
  66. ),
  67. );
  68. $displays = array_filter($this->getOption('displays'));
  69. if (count($displays) > 1) {
  70. $attach_to = $this->t('Multiple displays');
  71. }
  72. elseif (count($displays) == 1) {
  73. $display = array_shift($displays);
  74. if ($display = $this->view->storage->getDisplay($display)) {
  75. $attach_to = $display['display_title'];
  76. }
  77. }
  78. if (!isset($attach_to)) {
  79. $attach_to = $this->t('Not defined');
  80. }
  81. $options['displays'] = array(
  82. 'category' => 'attachment',
  83. 'title' => $this->t('Attach to'),
  84. 'value' => $attach_to,
  85. );
  86. $options['attachment_position'] = array(
  87. 'category' => 'attachment',
  88. 'title' => $this->t('Attachment position'),
  89. 'value' => $this->attachmentPositions($this->getOption('attachment_position')),
  90. );
  91. $options['inherit_arguments'] = array(
  92. 'category' => 'attachment',
  93. 'title' => $this->t('Inherit contextual filters'),
  94. 'value' => $this->getOption('inherit_arguments') ? $this->t('Yes') : $this->t('No'),
  95. );
  96. $options['inherit_exposed_filters'] = array(
  97. 'category' => 'attachment',
  98. 'title' => $this->t('Inherit exposed filters'),
  99. 'value' => $this->getOption('inherit_exposed_filters') ? $this->t('Yes') : $this->t('No'),
  100. );
  101. $options['inherit_pager'] = array(
  102. 'category' => 'pager',
  103. 'title' => $this->t('Inherit pager'),
  104. 'value' => $this->getOption('inherit_pager') ? $this->t('Yes') : $this->t('No'),
  105. );
  106. $options['render_pager'] = array(
  107. 'category' => 'pager',
  108. 'title' => $this->t('Render pager'),
  109. 'value' => $this->getOption('render_pager') ? $this->t('Yes') : $this->t('No'),
  110. );
  111. }
  112. /**
  113. * Provide the default form for setting options.
  114. */
  115. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  116. // It is very important to call the parent function here:
  117. parent::buildOptionsForm($form, $form_state);
  118. switch ($form_state->get('section')) {
  119. case 'inherit_arguments':
  120. $form['#title'] .= $this->t('Inherit contextual filters');
  121. $form['inherit_arguments'] = array(
  122. '#type' => 'checkbox',
  123. '#title' => $this->t('Inherit'),
  124. '#description' => $this->t('Should this display inherit its contextual filter values from the parent display to which it is attached?'),
  125. '#default_value' => $this->getOption('inherit_arguments'),
  126. );
  127. break;
  128. case 'inherit_exposed_filters':
  129. $form['#title'] .= $this->t('Inherit exposed filters');
  130. $form['inherit_exposed_filters'] = array(
  131. '#type' => 'checkbox',
  132. '#title' => $this->t('Inherit'),
  133. '#description' => $this->t('Should this display inherit its exposed filter values from the parent display to which it is attached?'),
  134. '#default_value' => $this->getOption('inherit_exposed_filters'),
  135. );
  136. break;
  137. case 'inherit_pager':
  138. $form['#title'] .= $this->t('Inherit pager');
  139. $form['inherit_pager'] = array(
  140. '#type' => 'checkbox',
  141. '#title' => $this->t('Inherit'),
  142. '#description' => $this->t('Should this display inherit its paging values from the parent display to which it is attached?'),
  143. '#default_value' => $this->getOption('inherit_pager'),
  144. );
  145. break;
  146. case 'render_pager':
  147. $form['#title'] .= $this->t('Render pager');
  148. $form['render_pager'] = array(
  149. '#type' => 'checkbox',
  150. '#title' => $this->t('Render'),
  151. '#description' => $this->t('Should this display render the pager values? This is only meaningful if inheriting a pager.'),
  152. '#default_value' => $this->getOption('render_pager'),
  153. );
  154. break;
  155. case 'attachment_position':
  156. $form['#title'] .= $this->t('Position');
  157. $form['attachment_position'] = array(
  158. '#title' => $this->t('Position'),
  159. '#type' => 'radios',
  160. '#description' => $this->t('Attach before or after the parent display?'),
  161. '#options' => $this->attachmentPositions(),
  162. '#default_value' => $this->getOption('attachment_position'),
  163. );
  164. break;
  165. case 'displays':
  166. $form['#title'] .= $this->t('Attach to');
  167. $displays = array();
  168. foreach ($this->view->storage->get('display') as $display_id => $display) {
  169. if ($this->view->displayHandlers->has($display_id) && $this->view->displayHandlers->get($display_id)->acceptAttachments()) {
  170. $displays[$display_id] = $display['display_title'];
  171. }
  172. }
  173. $form['displays'] = array(
  174. '#title' => $this->t('Displays'),
  175. '#type' => 'checkboxes',
  176. '#description' => $this->t('Select which display or displays this should attach to.'),
  177. '#options' => array_map('\Drupal\Component\Utility\Html::escape', $displays),
  178. '#default_value' => $this->getOption('displays'),
  179. );
  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 submitOptionsForm(&$form, FormStateInterface $form_state) {
  188. // It is very important to call the parent function here:
  189. parent::submitOptionsForm($form, $form_state);
  190. $section = $form_state->get('section');
  191. switch ($section) {
  192. case 'displays':
  193. $form_state->setValue($section, array_filter($form_state->getValue($section)));
  194. case 'inherit_arguments':
  195. case 'inherit_pager':
  196. case 'render_pager':
  197. case 'inherit_exposed_filters':
  198. case 'attachment_position':
  199. $this->setOption($section, $form_state->getValue($section));
  200. break;
  201. }
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function attachTo(ViewExecutable $view, $display_id, array &$build) {
  207. $displays = $this->getOption('displays');
  208. if (empty($displays[$display_id])) {
  209. return;
  210. }
  211. if (!$this->access()) {
  212. return;
  213. }
  214. $args = $this->getOption('inherit_arguments') ? $this->view->args : array();
  215. $view->setArguments($args);
  216. $view->setDisplay($this->display['id']);
  217. if ($this->getOption('inherit_pager')) {
  218. $view->display_handler->usesPager = $this->view->displayHandlers->get($display_id)->usesPager();
  219. $view->display_handler->setOption('pager', $this->view->displayHandlers->get($display_id)->getOption('pager'));
  220. }
  221. $attachment = $view->buildRenderable($this->display['id'], $args);
  222. switch ($this->getOption('attachment_position')) {
  223. case 'before':
  224. $this->view->attachment_before[] = $attachment;
  225. break;
  226. case 'after':
  227. $this->view->attachment_after[] = $attachment;
  228. break;
  229. case 'both':
  230. $this->view->attachment_before[] = $attachment;
  231. $this->view->attachment_after[] = $attachment;
  232. break;
  233. }
  234. }
  235. /**
  236. * Attachment displays only use exposed widgets if
  237. * they are set to inherit the exposed filter settings
  238. * of their parent display.
  239. */
  240. public function usesExposed() {
  241. if (!empty($this->options['inherit_exposed_filters']) && parent::usesExposed()) {
  242. return TRUE;
  243. }
  244. return FALSE;
  245. }
  246. /**
  247. * If an attachment is set to inherit the exposed filter
  248. * settings from its parent display, then don't render and
  249. * display a second set of exposed filter widgets.
  250. */
  251. public function displaysExposed() {
  252. return $this->options['inherit_exposed_filters'] ? FALSE : TRUE;
  253. }
  254. public function renderPager() {
  255. return $this->usesPager() && $this->getOption('render_pager');
  256. }
  257. }