function ViewsFormHelperTrait::standardDisplayDropdown

Same name and namespace in other branches
  1. main core/modules/views/src/ViewsFormHelperTrait.php \Drupal\views\ViewsFormHelperTrait::standardDisplayDropdown()

Adds an element to select either the default or the current display.

Parameters

array $form: The form render array to be altered.

\Drupal\Core\Form\FormStateInterface $formState: The current state of the form.

string $section: The section to which the display dropdown belongs.

5 calls to ViewsFormHelperTrait::standardDisplayDropdown()
AddHandler::buildForm in core/modules/views_ui/src/Form/Ajax/AddHandler.php
Form constructor.
ConfigHandler::buildForm in core/modules/views_ui/src/Form/Ajax/ConfigHandler.php
Form constructor.
DisplayPluginBase::buildOptionsForm in core/modules/views/src/Plugin/views/display/DisplayPluginBase.php
Provide a form to edit options for this plugin.
Rearrange::buildForm in core/modules/views_ui/src/Form/Ajax/Rearrange.php
Form constructor.
RearrangeFilter::buildForm in core/modules/views_ui/src/Form/Ajax/RearrangeFilter.php
Form constructor.

File

core/modules/views/src/ViewsFormHelperTrait.php, line 29

Class

ViewsFormHelperTrait
Provides reusable code to be shared by Views forms.

Namespace

Drupal\views

Code

protected function standardDisplayDropdown(array &$form, FormStateInterface $formState, string $section) : void {
  $view = $formState->get('view');
  $displayId = $formState->get('display_id');
  $executable = $view->getExecutable();
  $displays = $executable->displayHandlers;
  $currentDisplay = $executable->display_handler;
  // @todo Move this to a separate function if it's needed on any forms that
  // don't have the display dropdown.
  $form['override'] = [
    '#prefix' => '<div class="views-override clearfix form--inline views-offset-top" data-drupal-views-offset="top">',
    '#suffix' => '</div>',
    '#weight' => -1000,
    '#tree' => TRUE,
  ];
  // Add the "2 of 3" progress indicator.
  if ($formProgress = $view->getFormProgress()) {
    $arguments = $form['#title']->getArguments() + [
      '@current' => $formProgress['current'],
      '@total' => $formProgress['total'],
    ];
    $form['#title'] = $this->t('Configure @type @current of @total: @item', $arguments);
  }
  // The dropdown should not be added when any of the following is true:
  // - this is the default display.
  // - there is no default shown and just one additional display (mostly
  //   page), and the current display is defaulted.
  if ($currentDisplay->isDefaultDisplay() || $currentDisplay->isDefaulted($section) && !$this->getConfigFactory()
    ->get('views.settings')
    ->get('ui.show.default_display') && count($displays) <= 2) {
    return;
  }
  // Determine whether any other displays have overrides for this section.
  $sectionOverrides = FALSE;
  $sectionDefaulted = $currentDisplay->isDefaulted($section);
  foreach ($displays as $id => $display) {
    if ($id === 'default' || $id === $displayId) {
      continue;
    }
    if ($display && !$display->isDefaulted($section)) {
      $sectionOverrides = TRUE;
    }
  }
  $displayDropdown['default'] = $sectionOverrides ? $this->t('All displays (except overridden)') : $this->t('All displays');
  $displayDropdown[$displayId] = $this->t('This @display_type (override)', [
    '@display_type' => $currentDisplay->getPluginId(),
  ]);
  // Only display the revert option if we are in an overridden section.
  if (!$sectionDefaulted) {
    $displayDropdown['default_revert'] = $this->t('Revert to default');
  }
  $form['override']['dropdown'] = [
    '#type' => 'select',
    // @todo Translators may need more context than this.
'#title' => $this->t('For'),
    '#options' => $displayDropdown,
  ];
  if ($currentDisplay->isDefaulted($section)) {
    $form['override']['dropdown']['#default_value'] = 'defaults';
  }
  else {
    $form['override']['dropdown']['#default_value'] = $displayId;
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.