You are here

public function ViewsEmbedDialog::buildSelectDisplay in Views entity embed 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/ViewsEmbedDialog.php \Drupal\views_entity_embed\Form\ViewsEmbedDialog::buildSelectDisplay()

Form constructor for the view Select display.

Return value

array The form structure.

1 call to ViewsEmbedDialog::buildSelectDisplay()
ViewsEmbedDialog::buildForm in src/Form/ViewsEmbedDialog.php
Form constructor.

File

src/Form/ViewsEmbedDialog.php, line 216

Class

ViewsEmbedDialog
Provides a form to embed URLs.

Namespace

Drupal\views_entity_embed\Form

Code

public function buildSelectDisplay(array &$form, FormStateInterface $form_state, $embed_button) {
  $view = $form_state
    ->get('view');
  $view_title = !empty($view
    ->getTitle()) ? $view
    ->getTitle() : $view
    ->id();
  $view_element = $form_state
    ->get('view_element');
  $form['#title'] = $this
    ->t('Select dispay for  @view', [
    '@view' => $view_title,
  ]);
  $displays_options = $this
    ->getViewDisplays($view, $embed_button);
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['back'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Replace selected view'),
    // No regular submit-handler. This form only works via JavaScript.
    '#submit' => [],
    '#attributes' => [
      // @TODO to be fix.
      'disabled' => 'disabled',
    ],
  ];
  $form['actions']['save_modal'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Next'),
    '#button_type' => 'primary',
    // No regular submit-handler. This form only works via JavaScript.
    '#submit' => [],
    '#ajax' => [
      'callback' => '::submitSelectDisplay',
      'event' => 'click',
    ],
    '#attributes' => [
      'class' => [
        'js-button-next',
      ],
    ],
  ];
  if (empty($displays_options)) {
    $form['select_display_msg'] = [
      '#type' => '#markup',
      '#markup' => t('There is no display available for this View.'),
      '#weight' => -10,
    ];

    // Add disabled options for this case.
    $form['actions']['save_modal']['#attributes']['disabled'] = 'disabled';

    // Unset Ajax.
    unset($form['actions']['save_modal']['#ajax']);
    unset($form['actions']['save_modal']['#attributes']['class']['js-button-next']);
  }
  else {
    $form['select_display'] = [
      '#type' => 'select',
      '#options' => $displays_options,
      '#default_value' => isset($view_element['data-view-display']) ? $view_element['data-view-display'] : 'default',
      '#required' => TRUE,
      '#weight' => -10,
    ];
  }
  return $form;
}