You are here

public function InsertViewDialog::buildForm in Advanced Insert View 8

Same name and namespace in other branches
  1. 2.0.x src/Form/InsertViewDialog.php \Drupal\insert_view_adv\Form\InsertViewDialog::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/InsertViewDialog.php, line 215

Class

InsertViewDialog
Class InsertViewDialog

Namespace

Drupal\insert_view_adv\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) {

  // Add AJAX support.
  $form['#prefix'] = '<div id="insert-view-dialog-form">';
  $form['#suffix'] = '</div>';

  // Ensure relevant dialog libraries are attached.
  $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
  $filter_settings = $filter_format
    ->filters('insert_view_adv')->settings;
  $allowed_views = array_filter($filter_settings['allowed_views']);
  $views_list = Views::getEnabledViews();
  $arguments = [];
  $options = [
    '' => $this
      ->t('-- Choose the view --'),
  ];
  foreach ($views_list as $machine_name => $view) {
    foreach ($view
      ->get('display') as $display) {

      // Get display name with the view label
      $key = $machine_name . '=' . $display['id'];
      if (!empty($allowed_views) && empty($allowed_views[$key])) {
        continue;
      }
      if (empty($options[$machine_name])) {
        $options[$machine_name] = [];
      }
      $options[$machine_name][$key] = $view
        ->label() . ' ' . $display['display_title'];
      if (empty($display['display_options']['arguments']) && $display['id'] != 'default') {
        $master_display = $view
          ->getDisplay('default');
        if (!empty($master_display['display_options']['arguments'])) {
          $display['display_options']['arguments'] = $master_display['display_options']['arguments'];
        }
      }

      // Get arguments.
      if (!empty($display['display_options']['arguments']) && $display['display_options']['arguments']) {
        foreach ($display['display_options']['arguments'] as $field => $item) {
          $arguments[$machine_name . '=' . $display['id']][] = [
            $field => $item,
          ];
        }
      }
    }
  }

  // pass the arguments to form so we have access to arguments in ajax call
  $form['#view_arguments'] = $arguments;

  // check if the widget edit form is called
  $current_view = $this
    ->getUserInput($form_state, 'inserted_view_adv');
  if ($current_view == '') {

    // try to get the value from submitted
    $values = $form_state
      ->getUserInput();
    if (!empty($values['inserted_view_adv'])) {
      $current_view = $values['inserted_view_adv'];
    }
  }

  // select box with the list of views blocks grouped by view
  $form['inserted_view_adv'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('View to insert'),
    '#options' => $options,
    '#required' => TRUE,
    '#default_value' => $current_view,
    '#ajax' => [
      // trigger ajax call on change to get the arguments of the views block
      'callback' => 'Drupal\\insert_view_adv\\Form\\InsertViewDialog::getArguments',
      'event' => 'change',
      'wrapper' => 'arguments',
    ],
  ];

  // Create a settings form from the existing video formatter.
  $form['arguments'] = [];
  if (!empty($filter_settings['hide_argument_input'])) {
    $form['arguments']['#access'] = FALSE;
  }
  $form['arguments']['#type'] = 'fieldset';
  $form['arguments']['#prefix'] = '<div id="arguments">';
  $form['arguments']['#suffix'] = '</div>';
  $form['arguments']['#title'] = $this
    ->t('Arguments');
  $form['arguments']['argument'] = [
    '#tree' => TRUE,
  ];
  if ($current_view) {
    $argument_field = count($form['#view_arguments'][$current_view]);
    $form_state
      ->set('num_args', $argument_field);
  }
  else {
    $argument_field = $form_state
      ->get('num_args');
  }
  if (empty($argument_field)) {
    $form_state
      ->set('num_args', 0);
  }
  for ($i = 0; $i < $argument_field; $i++) {
    $this
      ->renderArgument($form, $form_state, $current_view, $i);
  }

  // if there are no arguments show the message
  if (count($form['arguments']['argument']) == 1) {
    $form['arguments']['argument']['#markup'] = $this
      ->t('No arguments provided');
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['save_modal'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#submit' => [],
    '#ajax' => [
      'callback' => '::ajaxSubmit',
      'event' => 'click',
      'wrapper' => 'insert-view-dialog-form',
    ],
  ];
  return $form;
}