/** * Processes an Ajax form submission. * * @param \Symfony\Component\HttpFoundation\Request $request * The current request object. * * @return mixed * Whatever is returned by the triggering element's #ajax['callback'] * function. One of: * - A render array containing the new or updated content to return to the * browser. This is commonly an element within the rebuilt form. * - A \Drupal\Core\Ajax\AjaxResponse object containing commands for the * browser to process. * * @throws \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface */ public function content(Request $request) { /** @var $ajaxForm \Drupal\system\FileAjaxForm */ $ajaxForm = $this->getForm($request); $form = $ajaxForm->getForm(); $form_state = $ajaxForm->getFormState(); $commands = $ajaxForm->getCommands(); $this->formBuilder->processForm($form['#form_id'], $form, $form_state); // 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. // Since this is the generic menu callback used by many Ajax elements, 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; /** @var $form_state \Drupal\Core\Form\FormStateInterface */ if ($triggering_element = $form_state->getTriggeringElement()) { $callback = $triggering_element['#ajax']['callback']; } $callback = $form_state->prepareCallback($callback); if (empty($callback) || !is_callable($callback)) { throw new HttpException(500, 'The specified #ajax callback is empty or not callable.'); } /** @var \Drupal\Core\Ajax\AjaxResponse $response */ $response = call_user_func_array($callback, [&$form, &$form_state]); foreach ($commands as $command) { $response->addCommand($command, TRUE); } return $response; }
/** * Processes an Ajax form submission. * * @param \Symfony\Component\HttpFoundation\Request $request * The current request object. * * @return mixed * Whatever is returned by the triggering element's #ajax['callback'] * function. One of: * - A render array containing the new or updated content to return to the * browser. This is commonly an element within the rebuilt form. * - A \Drupal\Core\Ajax\AjaxResponse object containing commands for the * browser to process. * * @throws \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface */ public function content(Request $request) { $ajax_form = $this->getForm($request); $form = $ajax_form->getForm(); $form_state = $ajax_form->getFormState(); $commands = $ajax_form->getCommands(); $this->formBuilder->processForm($form['#form_id'], $form, $form_state); return $this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, $commands); }
/** * Processes an Ajax form submission. * * @param \Symfony\Component\HttpFoundation\Request $request * The current request object. * * @return mixed * Whatever is returned by the triggering element's #ajax['callback'] * function. One of: * - A render array containing the new or updated content to return to the * browser. This is commonly an element within the rebuilt form. * - A \Drupal\Core\Ajax\AjaxResponse object containing commands for the * browser to process. * * @throws \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface */ public function content(Request $request) { /** @var $ajaxForm \Drupal\system\FileAjaxForm */ $ajaxForm = $this->getForm($request); $form = $ajaxForm->getForm(); $form_state = $ajaxForm->getFormState(); $commands = $ajaxForm->getCommands(); $this->formBuilder->processForm($form['#form_id'], $form, $form_state); // 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. // Since this is the generic menu callback used by many Ajax elements, 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; /** @var $form_state \Drupal\Core\Form\FormStateInterface */ if ($triggering_element = $form_state->getTriggeringElement()) { $callback = $triggering_element['#ajax']['callback']; } $callback = $form_state->prepareCallback($callback); if (empty($callback) || !is_callable($callback)) { throw new HttpException(500, 'The specified #ajax callback is empty or not callable.'); } $result = call_user_func_array($callback, [&$form, &$form_state]); // If the callback is an #ajax callback, the result is a render array, and // we need to turn it into an AJAX response, so that we can add any commands // we got earlier; typically the UpdateBuildIdCommand when handling an AJAX // submit from a cached page. if ($result instanceof AjaxResponse) { $response = $result; } else { /** @var \Drupal\Core\Ajax\AjaxResponse $response */ $response = $this->ajaxRenderer->renderResponse($result, $request, $this->routeMatch); } foreach ($commands as $command) { $response->addCommand($command, TRUE); } return $response; }