Skip to content

Instantly share code, notes, and snippets.

@stborchert
Created February 7, 2017 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stborchert/6ef90032f0926047c896a2f8b686737c to your computer and use it in GitHub Desktop.
Save stborchert/6ef90032f0926047c896a2f8b686737c to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\dc_search\Plugin\views\display;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\Attachment;
use Drupal\views\ViewExecutable;
/**
* The plugin that handles a single-page attachment display.
*
* @ingroup views_display_plugins
* @see \Drupal\views\Plugin\views\display\Attachment
*
* @ViewsDisplay(
* id = "dc_single_page_attachment",
* title = @Translation("Single page attachment"),
* help = @Translation("Attachments added to other displays to achieve multiple views in the same view."),
* theme = "views_view",
* contextual_links_locations = {""}
* )
*/
class SinglePageAttachment extends Attachment {
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['display_on_page'] = ['default' => '0'];
return $options;
}
/**
* Provide the summary for attachment options in the views UI.
*
* This output is returned as an array.
*/
public function optionsSummary(&$categories, &$options) {
// It is very important to call the parent function here:
parent::optionsSummary($categories, $options);
$options['display_on_page'] = array(
'category' => 'attachment',
'title' => $this->t('Display on page'),
'value' => empty($this->getOption('display_on_page')) ? $this->t('every page') : $this->getOption('display_on_page'),
);
}
/**
* Provide the default form for setting options.
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
// It is very important to call the parent function here:
parent::buildOptionsForm($form, $form_state);
switch ($form_state->get('section')) {
case 'display_on_page':
$form['#title'] .= $this->t('Display on selected page');
$form['display_on_page'] = [
'#type' => 'number',
'#title' => $this->t('Page'),
'#description' => $this->t('Enter the page number on which the display should be attached. Leave empty or set to 0 for all pages'),
'#default_value' => $this->getOption('display_on_page'),
'#min' => 0,
];
break;
}
}
/**
* Perform any necessary changes to the form values prior to storage.
* There is no need for this function to actually store the data.
*/
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
// It is very important to call the parent function here:
parent::submitOptionsForm($form, $form_state);
$section = $form_state->get('section');
switch ($section) {
case 'display_on_page':
$this->setOption($section, $form_state->getValue($section));
break;
}
}
/**
* {@inheritdoc}
*/
public function attachTo(ViewExecutable $view, $display_id, array &$build) {
if (!$this->view->displayHandlers->get($display_id)->usesPager() || empty($this->getOption('display_on_page'))) {
// No pager used or attach to every page.
parent::attachTo($view, $display_id, $build);
return;
}
$pager = $this->view->pager;
if ($pager && ($this->getOption('display_on_page') - 1) === $pager->current_page) {
// Attach to current page.
parent::attachTo($view, $display_id, $build);
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment