You are here

public function AnalysisFormHandler::analysisSubmitAjax in Real-time SEO for Drupal 8.2

Ajax Callback for returning entity preview to seo library.

Parameters

array $form: The complete form array.

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

Return value

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

File

src/Form/AnalysisFormHandler.php, line 70

Class

AnalysisFormHandler
Base class for yoast_seo_preview form handlers.

Namespace

Drupal\yoast_seo\Form

Code

public function analysisSubmitAjax(array &$form, FormStateInterface $form_state) {

  // Prevent firing accidental submissions from entity builder callbacks.
  $form_state
    ->setTemporaryValue('entity_validated', FALSE);
  $preview_entity = $form_state
    ->getFormObject()
    ->buildEntity($form, $form_state);
  $preview_entity->in_preview = TRUE;
  $entity_data = $this->entityAnalyser
    ->createEntityPreview($preview_entity);

  // The current value of the alias field, if any,
  // takes precedence over the entity url.
  $user_input = $form_state
    ->getUserInput();
  if (!empty($user_input['path'][0]['alias'])) {
    $entity_data['url'] = $user_input['path'][0]['alias'];
  }

  // Any form errors were displayed when our form with the analysis was
  // rendered. Any new messages are from form validation. We don't want to
  // leak those to the user because they'll get them during normal submission
  // so we clear them here.
  $this->messenger
    ->deleteAll();
  $response = new AjaxResponse();
  $response
    ->addCommand(new InvokeCommand('body', 'trigger', [
    'updateSeoData',
    $entity_data,
  ]));
  return $response;
}