PrependCommand example

$html = $this->renderer->renderRoot($main_content);
    $response->setAttachments($main_content['#attached']);

    // The selector for the insert command is NULL as the new content will     // replace the element making the Ajax call. The default 'replaceWith'     // behavior can be changed with #ajax['method'].     $response->addCommand(new InsertCommand(NULL, $html));
    $status_messages = ['#type' => 'status_messages'];
    $output = $this->renderer->renderRoot($status_messages);
    if (!empty($output)) {
      $response->addCommand(new PrependCommand(NULL, $output));
    }
    return $response;
  }

}


/** * Alter the Ajax command data that is sent to the client. * * @param \Drupal\Core\Ajax\CommandInterface[] $data * An array of all the rendered commands that will be sent to the client. */
function hook_ajax_render_alter(array &$data) {
  // Inject any new status messages into the content area.   $status_messages = ['#type' => 'status_messages'];
  $command = new \Drupal\Core\Ajax\PrependCommand('#block-system-main .content', \Drupal::service('renderer')->renderRoot($status_messages));
  $data[] = $command->render();
}

/** * Perform alterations before a form is rendered. * * One popular use of this hook is to add form elements to the node form. When * altering a node form, the node entity can be retrieved by invoking * $form_state->getFormObject()->getEntity(). * * Implementations are responsible for adding cache contexts/tags/max-age as * needed. See https://www.drupal.org/docs/8/api/cache-api/cache-api. * * In addition to hook_form_alter(), which is called for all forms, there are * two more specific form hooks available. The first, * hook_form_BASE_FORM_ID_alter(), allows targeting of a form/forms via a base * form (if one exists). The second, hook_form_FORM_ID_alter(), can be used to * target a specific form directly. * * The call order is as follows: all existing form alter functions are called * for module A, then all for module B, etc., followed by all for any base * theme(s), and finally for the theme itself. The module order is determined * by system weight, then by module name. * * Within each module, form alter hooks are called in the following order: * first, hook_form_alter(); second, hook_form_BASE_FORM_ID_alter(); third, * hook_form_FORM_ID_alter(). So, for each module, the more general hooks are * called first followed by the more specific. * * @param $form * Nested array of form elements that comprise the form. * @param $form_state * The current state of the form. The arguments that * \Drupal::formBuilder()->getForm() was originally called with are available * in the array $form_state->getBuildInfo()['args']. * @param $form_id * A string that is the unique ID of the form, set by * Drupal\Core\Form\FormInterface::getFormId(). * * @see hook_form_BASE_FORM_ID_alter() * @see hook_form_FORM_ID_alter() * * @ingroup form_api */
// Override the display's pager_element with the one actually used.         if (isset($pager_element)) {
          $response->addCommand(new ScrollTopCommand(".js-view-dom-id-$dom_id"));
          $view->displayHandlers->get($display_id)->setOption('pager_element', $pager_element);
        }
        // Reuse the same DOM id so it matches that in drupalSettings.         $view->dom_id = $dom_id;

        $preview = $view->preview($display_id$args);
        $response->addCommand(new ReplaceCommand(".js-view-dom-id-$dom_id", $preview));
        $response->addCommand(new PrependCommand(".js-view-dom-id-$dom_id", ['#type' => 'status_messages']));

        return $response;
      }
      else {
        throw new AccessDeniedHttpException();
      }
    }
    else {
      throw new NotFoundHttpException();
    }
  }

}

  public function onException(ExceptionEvent $event) {
    $exception = $event->getThrowable();
    $request = $event->getRequest();

    // Render a nice error message in case we have a file upload which exceeds     // the configured upload limit.     if ($exception instanceof BrokenPostRequestException && $request->query->has(FormBuilderInterface::AJAX_FORM_REQUEST)) {
      $this->messenger->addError($this->t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', ['@size' => $this->formatSize($exception->getSize())]));
      $response = new AjaxResponse(NULL, 200);
      $status_messages = ['#type' => 'status_messages'];
      $response->addCommand(new PrependCommand(NULL, $status_messages));
      $event->allowCustomResponseCode();
      $event->setResponse($response);
      return;
    }

    // Extract the form AJAX exception (it may have been passed to another     // exception before reaching here).     if ($exception = $this->getFormAjaxException($exception)) {
      $request = $event->getRequest();
      $form = $exception->getForm();
      $form_state = $exception->getFormState();

      
'method' => 'myMethod',
      'args' => ['var1', 'var2'],
    ];

    $this->assertEquals($expected$command->render());
  }

  /** * @covers \Drupal\Core\Ajax\PrependCommand */
  public function testPrependCommand() {
    $command = new PrependCommand('#page-title', '<p>New Text!</p>', ['my-setting' => 'setting']);

    $expected = [
      'command' => 'insert',
      'method' => 'prepend',
      'selector' => '#page-title',
      'data' => '<p>New Text!</p>',
      'settings' => ['my-setting' => 'setting'],
    ];

    $this->assertEquals($expected$command->render());
  }

  
Home | Imprint | This part of the site doesn't use cookies.