You are here

protected function AjaxPageControllerBase::generateAjaxReplace in Forena Reports 8

Generate the ajax form replacement commands.

Parameters

$section:

$form:

Return value

array

1 call to AjaxPageControllerBase::generateAjaxReplace()
AjaxPageControllerBase::getForm in src/Controller/AjaxPageControllerBase.php
Retrieve a drupal form for inclusion in the app. Will load based on controlllerr's jsMode property as either an ajax command or as an inline form.

File

src/Controller/AjaxPageControllerBase.php, line 401

Class

AjaxPageControllerBase

Namespace

Drupal\forena\Controller

Code

protected function generateAjaxReplace($section, $form) {
  $commands = [];

  // If the form build ID has changed, issue an Ajax command to update it.
  $build = $this->form_state
    ->getCompleteForm();
  if (isset($_POST['form_build_id']) && $_POST['form_build_id'] !== $build['#build_id']) {
    $commands[] = new UpdateBuildIdCommand($_POST['form_build_id'], $build['#build_id']);
  }

  // We need to return the part of the form (or some other content) that needs
  // to be re-rendered so the browser can update the page with changed
  // content. It is up to the #ajax['callback'] function of the element (may
  // or may not be a button) that triggered the Ajax request to determine what
  // needs to be rendered.
  $callback = NULL;
  $wrapper = NULL;
  $ajax_callback = NULL;

  // If we have an ajax callback assume we're doing a drupal style ajax replacment
  if (($triggering_element = $this->form_state
    ->getTriggeringElement()) && isset($triggering_element['#ajax']['callback'])) {
    $callback = $ajax_callback = $triggering_element['#ajax']['callback'];
    $wrapper = $triggering_element['#ajax']['wrapper'];
  }

  // Determine if there is a callback.
  $callback = $this->form_state
    ->prepareCallback($callback);
  if ($callback) {
    if (empty($callback) || !is_callable($callback)) {
      $commands[] = new HtmlCommand('#' . $section, $form);
    }
    $result = call_user_func_array($callback, [
      &$form,
      &$this->form_state,
    ]);

    // At this point we know callback returned a render element. If the
    // element is part of the group (#group is set on it) it won't be rendered
    // unless we remove #group from it. This is caused by
    // \Drupal\Core\Render\Element\RenderElement::preRenderGroup(), which
    // prevents all members of groups from being rendered directly.
    if (is_array($result)) {
      if (!empty($result['#group'])) {
        unset($result['#group']);
      }
      if ($wrapper) {
        $commands[] = new ReplaceCommand('#' . $wrapper, $result);
      }
      else {

        // we don't have a wrapper so assume form section replacement
        $commands[] = new HtmlCommand('#' . $section, $form);
      }
    }
  }
  else {

    // No ajax callback implies we are doing a normal full form replacment.
    $commands[] = new HtmlCommand('#' . $section, $form);
  }
  return $commands;
}