You are here

public static function FormAlter::ajaxFormEntityCallback in Ajax form entity 8

Ajax callback to handle special ajax form entity magic.

Parameters

array $form: Nested array of form elements that comprise the form.

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

Return value

array|\Drupal\Core\Ajax\AjaxResponse

File

src/Form/FormAlter.php, line 78

Class

FormAlter
Class FormAlter.

Namespace

Drupal\ajax_form_entity\Form

Code

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

  // If errors, returns the form with errors and messages.
  if ($form_state
    ->hasAnyErrors()) {
    return $form;
  }
  else {
    $user_inputs = $form_state
      ->getUserInput();
    if (empty($user_inputs['entity'])) {
      return $form;
    }

    /* @var $entity \Drupal\Core\Entity\Entity */
    $entity = $user_inputs['entity'];
    $entity_type = $entity
      ->getEntityTypeId();
    $configurations = $form_state
      ->getValue('ajax_form_entity');
    $response = new AjaxResponse();

    // Special case of node page : handle title.
    // @todo : reload the full block with title.
    // @todo : what if in Views but display full ?
    if ($entity_type == 'node' && $configurations['view_mode'] == 'full') {

      /* @var $entity \Drupal\Node\Entity\Node */
      $title = $entity
        ->getTitle();
      $entity
        ->setTitle('');
      $replace = '<h1>' . $title . '</h1>';
      $response
        ->addCommand(new ReplaceCommand('h1', $replace));
    }

    // Get messages even if not shown.
    $status_messages = [
      '#type' => 'status_messages',
    ];
    $message = [
      '#markup' => \Drupal::service('renderer')
        ->renderRoot($status_messages),
    ];

    // Remove old messages.
    $response
      ->addCommand(new RemoveCommand('.messages'));

    // Send the content back (append, prepend of send in a custom div).
    if ($configurations['send_content']) {
      if ($configurations['show_message']) {
        $output['message'] = $message;
      }

      /* @var $render_view_mode \Drupal\Core\Render\Markup */
      $output['entity']['#markup'] = render(\Drupal::entityTypeManager()
        ->getViewBuilder($entity_type)
        ->view($entity, $configurations['view_mode']));
      if ($configurations['selector_type'] === 'prepend') {
        $response
          ->addCommand(new BeforeCommand($configurations['content_selector'], $output));
      }
      elseif ($configurations['selector_type'] === 'append') {
        $response
          ->addCommand(new AfterCommand($configurations['content_selector'], $output));
      }
      elseif ($configurations['content_selector']) {
        $response
          ->addCommand(new ReplaceCommand($configurations['content_selector'], $output));
      }
    }
    elseif ($configurations['show_message']) {
      $response
        ->addCommand(new ReplaceCommand($configurations['content_selector'], $message));
    }

    // Reload (or remove) the form.
    if ($configurations['reload']) {
      $response
        ->addCommand(new ReplaceCommand($configurations['form_selector'], $form));
    }
    else {
      $response
        ->addCommand(new RemoveCommand($configurations['form_selector']));
    }

    // Case of popin.
    $response
      ->addCommand(new RemoveCommand('.ui-dialog'));
    $response
      ->addCommand(new RemoveCommand('.ui-widget-overlay'));
    return $response;
  }
}