You are here

public function ViewsEmbedDialog::submitSelectArguments in Views entity embed 8

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

Form submission handler for the views selection step.

Return value

\Drupal\Core\Ajax\AjaxResponse The ajax response.

File

src/Form/ViewsEmbedDialog.php, line 431

Class

ViewsEmbedDialog
Provides a form to embed URLs.

Namespace

Drupal\views_entity_embed\Form

Code

public function submitSelectArguments(array &$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $build_arg = $form_state
    ->getValue('build_select_arguments');
  $embed_button = $form_state
    ->get('embed_button');

  // Display errors in form, if any.
  if ($form_state
    ->hasAnyErrors()) {
    unset($form['#prefix'], $form['#suffix']);
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];
    $response
      ->addCommand(new HtmlCommand('#views-entity-embed-dialog-form', $form));
  }
  else {
    $view_element = $form_state
      ->get('view_element');

    // Serialize entity embed settings to JSON string.
    $view_element['data-view-arguments'] = Json::encode($build_arg);
    $view_element['data-embed-button'] = $embed_button
      ->id();

    // Filter out empty attributes.
    $view_element = array_filter($view_element, function ($value) {
      return (bool) Unicode::strlen((string) $value);
    });

    // Allow other modules to alter the values before
    // getting submitted to the WYSIWYG.
    $response
      ->addCommand(new EditorDialogSave([
      'attributes' => $view_element,
    ]));
    $response
      ->addCommand(new CloseModalDialogCommand());
  }
  return $response;
}