ViewFormBase.php in Drupal 9
File
core/modules/views_ui/src/ViewFormBase.php
View source
<?php
namespace Drupal\views_ui;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
abstract class ViewFormBase extends EntityForm {
protected $displayID;
public function init(FormStateInterface $form_state) {
parent::init($form_state);
$form_state
->loadInclude('views_ui', 'inc', 'admin');
$form_state
->set('view', $this->entity);
}
public function buildForm(array $form, FormStateInterface $form_state, $display_id = NULL) {
if (isset($display_id) && $form_state
->has('display_id') && $display_id !== $form_state
->get('display_id')) {
throw new \InvalidArgumentException('Mismatch between $form_state->get(\'display_id\') and $display_id.');
}
$this->displayID = $form_state
->has('display_id') ? $form_state
->get('display_id') : $display_id;
return parent::buildForm($form, $form_state);
}
protected function prepareEntity() {
if ($tabs = $this
->getDisplayTabs($this->entity)) {
if (empty($this->displayID)) {
uasort($tabs, 'Drupal\\Component\\Utility\\SortArray::sortByWeightProperty');
foreach ($tabs as $id => $tab) {
if (!isset($tab['#access']) || $tab['#access']) {
$this->displayID = $id;
break;
}
}
}
if ($this->displayID && !isset($tabs[$this->displayID])) {
throw new NotFoundHttpException();
}
elseif ($this->displayID && (isset($tabs[$this->displayID]['#access']) && !$tabs[$this->displayID]['#access'])) {
throw new AccessDeniedHttpException();
}
}
elseif ($this->displayID) {
throw new NotFoundHttpException();
}
}
public function getDisplayTabs(ViewUI $view) {
$executable = $view
->getExecutable();
$executable
->initDisplay();
$display_id = $this->displayID;
$tabs = [];
foreach ($view
->get('display') as $id => $display) {
$display_plugin = $executable->displayHandlers
->get($id);
if (empty($display_plugin)) {
continue;
}
$tabs[$id] = [
'#theme' => 'menu_local_task',
'#weight' => $display['position'],
'#link' => [
'title' => $this
->getDisplayLabel($view, $id),
'localized_options' => [],
'url' => $view
->toUrl('edit-display-form')
->setRouteParameter('display_id', $id),
],
];
if (!empty($display['deleted'])) {
$tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'views-display-deleted-link';
}
if (isset($display['display_options']['enabled']) && !$display['display_options']['enabled']) {
$tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'views-display-disabled-link';
}
}
if (!$this
->isDefaultDisplayShown($view) && $display_id != 'default' && count($tabs) > 1) {
$tabs['default']['#access'] = FALSE;
}
$errors = $executable
->validate();
foreach ($view
->get('display') as $id => $display) {
if (!empty($errors[$id])) {
$tabs[$id]['#access'] = TRUE;
$tabs[$id]['#link']['localized_options']['attributes']['class'][] = 'error';
$tabs[$id]['#link']['localized_options']['attributes']['title'] = $this
->t('This display has one or more validation errors.');
}
}
return $tabs;
}
public function isDefaultDisplayShown(ViewUI $view) {
$advanced_mode = \Drupal::config('views.settings')
->get('ui.show.default_display');
$additional_displays = count($view
->getExecutable()->displayHandlers) == 1;
return $advanced_mode || $additional_displays;
}
public function getDisplayLabel(ViewUI $view, $display_id, $check_changed = TRUE) {
$display = $view
->get('display');
$title = $display_id == 'default' ? $this
->t('Default') : $display[$display_id]['display_title'];
$title = views_ui_truncate($title, 25);
if ($check_changed && !empty($view->changed_display[$display_id])) {
$changed = '*';
$title = $title . $changed;
}
return $title;
}
}