Attachment.php 9.56 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\display\Attachment.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\display;
9

10
use Drupal\Component\Utility\String;
11
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\views\ViewExecutable;
13

merlinofchaos's avatar
merlinofchaos committed
14 15 16 17 18 19 20 21
/**
 * The plugin that handles an attachment display.
 *
 * Attachment displays are secondary displays that are 'attached' to a primary
 * display. Effectively they are a simple way to get multiple views within
 * the same view. They can share some information.
 *
 * @ingroup views_display_plugins
22
 *
23
 * @ViewsDisplay(
aspilicious's avatar
aspilicious committed
24
 *   id = "attachment",
25 26 27
 *   title = @Translation("Attachment"),
 *   help = @Translation("Attachments added to other displays to achieve multiple views in the same view."),
 *   theme = "views_view",
28
 *   contextual_links_locations = {""}
29 30
 * )
 */
31
class Attachment extends DisplayPluginBase {
32

33 34 35 36 37 38 39
  /**
   * Whether the display allows the use of a pager or not.
   *
   * @var bool
   */
  protected $usesPager = FALSE;

40 41
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
42 43 44 45 46 47 48 49 50 51 52

    $options['displays'] = array('default' => array());
    $options['attachment_position'] = array('default' => 'before');
    $options['inherit_arguments'] = array('default' => TRUE, 'bool' => TRUE);
    $options['inherit_exposed_filters'] = array('default' => FALSE, 'bool' => TRUE);
    $options['inherit_pager'] = array('default' => FALSE, 'bool' => TRUE);
    $options['render_pager'] = array('default' => FALSE, 'bool' => TRUE);

    return $options;
  }

53
  public function execute() {
54
    return $this->view->render($this->display['id']);
merlinofchaos's avatar
merlinofchaos committed
55 56
  }

57
  public function attachmentPositions($position = NULL) {
merlinofchaos's avatar
merlinofchaos committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    $positions = array(
      'before' => t('Before'),
      'after' => t('After'),
      'both' => t('Both'),
    );

    if ($position) {
      return $positions[$position];
    }

    return $positions;
  }

  /**
   * Provide the summary for attachment options in the views UI.
   *
   * This output is returned as an array.
   */
76
  public function optionsSummary(&$categories, &$options) {
merlinofchaos's avatar
merlinofchaos committed
77
    // It is very important to call the parent function here:
78
    parent::optionsSummary($categories, $options);
merlinofchaos's avatar
merlinofchaos committed
79 80 81 82 83 84 85 86 87

    $categories['attachment'] = array(
      'title' => t('Attachment settings'),
      'column' => 'second',
      'build' => array(
        '#weight' => -10,
      ),
    );

88
    $displays = array_filter($this->getOption('displays'));
merlinofchaos's avatar
merlinofchaos committed
89 90 91 92 93
    if (count($displays) > 1) {
      $attach_to = t('Multiple displays');
    }
    elseif (count($displays) == 1) {
      $display = array_shift($displays);
94
      if ($display = $this->view->storage->getDisplay($display)) {
95
        $attach_to = String::checkPlain($display['display_title']);
merlinofchaos's avatar
merlinofchaos committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
      }
    }

    if (!isset($attach_to)) {
      $attach_to = t('Not defined');
    }

    $options['displays'] = array(
      'category' => 'attachment',
      'title' => t('Attach to'),
      'value' => $attach_to,
    );

    $options['attachment_position'] = array(
      'category' => 'attachment',
      'title' => t('Attachment position'),
112
      'value' => $this->attachmentPositions($this->getOption('attachment_position')),
merlinofchaos's avatar
merlinofchaos committed
113 114 115 116 117
    );

    $options['inherit_arguments'] = array(
      'category' => 'attachment',
      'title' => t('Inherit contextual filters'),
118
      'value' => $this->getOption('inherit_arguments') ? t('Yes') : t('No'),
merlinofchaos's avatar
merlinofchaos committed
119 120 121 122 123
    );

    $options['inherit_exposed_filters'] = array(
      'category' => 'attachment',
      'title' => t('Inherit exposed filters'),
124
      'value' => $this->getOption('inherit_exposed_filters') ? t('Yes') : t('No'),
merlinofchaos's avatar
merlinofchaos committed
125 126 127 128 129
    );

    $options['inherit_pager'] = array(
      'category' => 'pager',
      'title' => t('Inherit pager'),
130
      'value' => $this->getOption('inherit_pager') ? t('Yes') : t('No'),
merlinofchaos's avatar
merlinofchaos committed
131 132 133 134 135
    );

    $options['render_pager'] = array(
      'category' => 'pager',
      'title' => t('Render pager'),
136
      'value' => $this->getOption('render_pager') ? t('Yes') : t('No'),
merlinofchaos's avatar
merlinofchaos committed
137 138 139 140 141 142 143
    );

  }

  /**
   * Provide the default form for setting options.
   */
144
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
merlinofchaos's avatar
merlinofchaos committed
145
    // It is very important to call the parent function here:
146
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
147 148 149 150 151 152 153 154

    switch ($form_state['section']) {
      case 'inherit_arguments':
        $form['#title'] .= t('Inherit contextual filters');
        $form['inherit_arguments'] = array(
          '#type' => 'checkbox',
          '#title' => t('Inherit'),
          '#description' => t('Should this display inherit its contextual filter values from the parent display to which it is attached?'),
155
          '#default_value' => $this->getOption('inherit_arguments'),
merlinofchaos's avatar
merlinofchaos committed
156 157 158 159 160 161 162 163
        );
        break;
      case 'inherit_exposed_filters':
        $form['#title'] .= t('Inherit exposed filters');
        $form['inherit_exposed_filters'] = array(
          '#type' => 'checkbox',
          '#title' => t('Inherit'),
          '#description' => t('Should this display inherit its exposed filter values from the parent display to which it is attached?'),
164
          '#default_value' => $this->getOption('inherit_exposed_filters'),
merlinofchaos's avatar
merlinofchaos committed
165 166 167 168 169 170 171 172
        );
        break;
      case 'inherit_pager':
        $form['#title'] .= t('Inherit pager');
        $form['inherit_pager'] = array(
          '#type' => 'checkbox',
          '#title' => t('Inherit'),
          '#description' => t('Should this display inherit its paging values from the parent display to which it is attached?'),
173
          '#default_value' => $this->getOption('inherit_pager'),
merlinofchaos's avatar
merlinofchaos committed
174 175 176 177 178 179 180 181
        );
        break;
      case 'render_pager':
        $form['#title'] .= t('Render pager');
        $form['render_pager'] = array(
          '#type' => 'checkbox',
          '#title' => t('Render'),
          '#description' => t('Should this display render the pager values? This is only meaningful if inheriting a pager.'),
182
          '#default_value' => $this->getOption('render_pager'),
merlinofchaos's avatar
merlinofchaos committed
183 184 185 186 187
        );
        break;
      case 'attachment_position':
        $form['#title'] .= t('Position');
        $form['attachment_position'] = array(
188
          '#title' => t('Position'),
merlinofchaos's avatar
merlinofchaos committed
189 190
          '#type' => 'radios',
          '#description' => t('Attach before or after the parent display?'),
191 192
          '#options' => $this->attachmentPositions(),
          '#default_value' => $this->getOption('attachment_position'),
merlinofchaos's avatar
merlinofchaos committed
193 194 195 196 197
        );
        break;
      case 'displays':
        $form['#title'] .= t('Attach to');
        $displays = array();
198
        foreach ($this->view->storage->get('display') as $display_id => $display) {
199
          if ($this->view->displayHandlers->has($display_id) && $this->view->displayHandlers->get($display_id)->acceptAttachments()) {
200
            $displays[$display_id] = $display['display_title'];
merlinofchaos's avatar
merlinofchaos committed
201 202 203
          }
        }
        $form['displays'] = array(
204
          '#title' => t('Displays'),
merlinofchaos's avatar
merlinofchaos committed
205 206 207
          '#type' => 'checkboxes',
          '#description' => t('Select which display or displays this should attach to.'),
          '#options' => $displays,
208
          '#default_value' => $this->getOption('displays'),
merlinofchaos's avatar
merlinofchaos committed
209 210 211 212 213 214 215 216 217
        );
        break;
    }
  }

  /**
   * Perform any necessary changes to the form values prior to storage.
   * There is no need for this function to actually store the data.
   */
218
  public function submitOptionsForm(&$form, FormStateInterface $form_state) {
merlinofchaos's avatar
merlinofchaos committed
219
    // It is very important to call the parent function here:
220
    parent::submitOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
221
    switch ($form_state['section']) {
222
      case 'displays':
223
        $form_state->setValue($form_state['section'], array_filter($form_state->getValue($form_state['section'])));
merlinofchaos's avatar
merlinofchaos committed
224 225 226 227 228
      case 'inherit_arguments':
      case 'inherit_pager':
      case 'render_pager':
      case 'inherit_exposed_filters':
      case 'attachment_position':
229
        $this->setOption($form_state['section'], $form_state->getValue($form_state['section']));
merlinofchaos's avatar
merlinofchaos committed
230 231 232 233 234
        break;
    }
  }

  /**
235
   * {@inheritdoc}
merlinofchaos's avatar
merlinofchaos committed
236
   */
237
  public function attachTo(ViewExecutable $view, $display_id, array &$build) {
238
    $displays = $this->getOption('displays');
merlinofchaos's avatar
merlinofchaos committed
239 240 241 242 243 244 245 246 247

    if (empty($displays[$display_id])) {
      return;
    }

    if (!$this->access()) {
      return;
    }

248
    $args = $this->getOption('inherit_arguments') ? $this->view->args : array();
249
    $view->setArguments($args);
250
    $view->setDisplay($this->display['id']);
251
    if ($this->getOption('inherit_pager')) {
252 253
      $view->display_handler->usesPager = $this->view->displayHandlers->get($display_id)->usesPager();
      $view->display_handler->setOption('pager', $this->view->displayHandlers->get($display_id)->getOption('pager'));
merlinofchaos's avatar
merlinofchaos committed
254 255
    }

256
    $attachment = $view->executeDisplay($this->display['id'], $args);
merlinofchaos's avatar
merlinofchaos committed
257

258
    switch ($this->getOption('attachment_position')) {
merlinofchaos's avatar
merlinofchaos committed
259
      case 'before':
260
        $this->view->attachment_before[] = $attachment;
merlinofchaos's avatar
merlinofchaos committed
261 262
        break;
      case 'after':
263
        $this->view->attachment_after[] = $attachment;
merlinofchaos's avatar
merlinofchaos committed
264 265
        break;
      case 'both':
266 267
        $this->view->attachment_before[] = $attachment;
        $this->view->attachment_after[] = $attachment;
merlinofchaos's avatar
merlinofchaos committed
268 269 270 271 272 273 274 275 276 277
        break;
    }

  }

  /**
   * Attachment displays only use exposed widgets if
   * they are set to inherit the exposed filter settings
   * of their parent display.
   */
278 279
  public function usesExposed() {
    if (!empty($this->options['inherit_exposed_filters']) && parent::usesExposed()) {
merlinofchaos's avatar
merlinofchaos committed
280 281 282 283 284 285 286 287 288 289
      return TRUE;
    }
    return FALSE;
  }

  /**
   * If an attachment is set to inherit the exposed filter
   * settings from its parent display, then don't render and
   * display a second set of exposed filter widgets.
   */
290
  public function displaysExposed() {
merlinofchaos's avatar
merlinofchaos committed
291 292 293
    return $this->options['inherit_exposed_filters'] ? FALSE : TRUE;
  }

294 295
  public function renderPager() {
    return $this->usesPager() && $this->getOption('render_pager');
merlinofchaos's avatar
merlinofchaos committed
296
  }
297

merlinofchaos's avatar
merlinofchaos committed
298
}