View source
<?php
namespace Drupal\views\Plugin\views\area;
use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\PathPluginBase;
class DisplayLink extends AreaPluginBase {
protected function defineOptions() {
$options = parent::defineOptions();
$options['display_id'] = [
'default' => NULL,
];
$options['label'] = [
'default' => NULL,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$allowed_displays = [];
$displays = $this->view->storage
->get('display');
foreach ($displays as $display_id => $display) {
if (!$this
->isPathBasedDisplay($display_id)) {
unset($displays[$display_id]);
continue;
}
$allowed_displays[$display_id] = $display['display_title'];
}
$form['description'] = [
[
'#markup' => $this
->t('To make sure the results are the same when switching to the other display, it is recommended to make sure the display:'),
],
[
'#theme' => 'item_list',
'#items' => [
$this
->t('Has a path.'),
$this
->t('Has the same filter criteria.'),
$this
->t('Has the same sort criteria.'),
$this
->t('Has the same pager settings.'),
$this
->t('Has the same contextual filters.'),
],
],
];
if (!$allowed_displays) {
$form['empty_message'] = [
'#markup' => '<p><em>' . $this
->t('There are no path-based displays available.') . '</em></p>',
];
}
else {
$form['display_id'] = [
'#title' => $this
->t('Display'),
'#type' => 'select',
'#options' => $allowed_displays,
'#default_value' => $this->options['display_id'],
'#required' => TRUE,
];
$form['label'] = [
'#title' => $this
->t('Label'),
'#description' => $this
->t('The text of the link.'),
'#type' => 'textfield',
'#default_value' => $this->options['label'],
'#required' => TRUE,
];
}
}
public function validate() {
$errors = parent::validate();
if ($this->displayHandler
->isDefaultDisplay() && !\Drupal::config('views.settings')
->get('ui.show.default_display')) {
return $errors;
}
$linked_display_id = !empty($this->options['display_id']) ? $this->options['display_id'] : NULL;
if (!$linked_display_id) {
$errors[] = $this
->t('%current_display: The link in the %area area has no configured display.', [
'%current_display' => $this->displayHandler->display['display_title'],
'%area' => $this->areaType,
]);
return $errors;
}
if (!$this->view->displayHandlers
->get($linked_display_id)) {
$errors[] = $this
->t('%current_display: The link in the %area area points to the %linked_display display which no longer exists.', [
'%current_display' => $this->displayHandler->display['display_title'],
'%area' => $this->areaType,
'%linked_display' => $this->options['display_id'],
]);
return $errors;
}
if (!$this
->isPathBasedDisplay($linked_display_id)) {
$errors[] = $this
->t('%current_display: The link in the %area area points to the %linked_display display which does not have a path.', [
'%current_display' => $this->displayHandler->display['display_title'],
'%area' => $this->areaType,
'%linked_display' => $this->view->displayHandlers
->get($linked_display_id)->display['display_title'],
]);
return $errors;
}
$unequal_options = [
'filters' => t('Filter criteria'),
'sorts' => t('Sort criteria'),
'pager' => t('Pager'),
'arguments' => t('Contextual filters'),
];
foreach (array_keys($unequal_options) as $option) {
if ($this
->hasEqualOptions($linked_display_id, $option)) {
unset($unequal_options[$option]);
}
}
if ($unequal_options) {
$warning = $this
->t('%current_display: The link in the %area area points to the %linked_display display which uses different settings than the %current_display display for: %unequal_options. To make sure users see the exact same result when clicking the link, please check that the settings are the same.', [
'%current_display' => $this->displayHandler->display['display_title'],
'%area' => $this->areaType,
'%linked_display' => $this->view->displayHandlers
->get($linked_display_id)->display['display_title'],
'%unequal_options' => implode(', ', $unequal_options),
]);
$this
->messenger()
->addWarning($warning);
}
return $errors;
}
public function render($empty = FALSE) {
if ($empty && empty($this->options['empty']) || empty($this->options['display_id'])) {
return [];
}
if (!$this
->isPathBasedDisplay($this->options['display_id'])) {
return [];
}
$query = $this->view
->getExposedInput();
if ($current_page = $this->view
->getCurrentPage()) {
$query['page'] = $current_page;
}
foreach ([
'view_name',
'view_display_id',
'view_args',
'view_path',
'view_dom_id',
'pager_element',
'view_base_path',
AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER,
FormBuilderInterface::AJAX_FORM_REQUEST,
MainContentViewSubscriber::WRAPPER_FORMAT,
] as $key) {
unset($query[$key]);
}
$classes = [
'views-display-link',
'views-display-link-' . $this->options['display_id'],
];
if ($this->options['display_id'] === $this->view->current_display) {
$classes[] = 'is-active';
}
return [
'#type' => 'link',
'#title' => $this->options['label'],
'#url' => $this->view
->getUrl($this->view->args, $this->options['display_id'])
->setOptions([
'query' => $query,
]),
'#options' => [
'view' => $this->view,
'target_display_id' => $this->options['display_id'],
'attributes' => [
'class' => $classes,
],
],
];
}
protected function isPathBasedDisplay($display_id) {
$loaded_display = $this->view->displayHandlers
->get($display_id);
return $loaded_display instanceof PathPluginBase;
}
protected function hasEqualOptions($display_id, $option) {
$loaded_display = $this->view->displayHandlers
->get($display_id);
return $loaded_display
->getOption($option) === $this->displayHandler
->getOption($option);
}
}