Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Form / FormAjaxResponseBuilder.php
1 <?php
2
3 namespace Drupal\Core\Form;
4
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\UpdateBuildIdCommand;
7 use Drupal\Core\Render\MainContent\MainContentRendererInterface;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\HttpKernel\Exception\HttpException;
11
12 /**
13  * Builds an AJAX form response.
14  *
15  * Given the current request, a form render array, its form state, and any AJAX
16  * commands to apply to the form, build a response object.
17  */
18 class FormAjaxResponseBuilder implements FormAjaxResponseBuilderInterface {
19
20   /**
21    * The main content to AJAX Response renderer.
22    *
23    * @var \Drupal\Core\Render\MainContent\MainContentRendererInterface
24    */
25   protected $ajaxRenderer;
26
27   /**
28    * The current route match.
29    *
30    * @var \Drupal\Core\Routing\RouteMatchInterface
31    */
32   protected $routeMatch;
33
34   /**
35    * Constructs a new FormAjaxResponseBuilder.
36    *
37    * @param \Drupal\Core\Render\MainContent\MainContentRendererInterface $ajax_renderer
38    *   The ajax renderer.
39    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
40    *   The current route match.
41    */
42   public function __construct(MainContentRendererInterface $ajax_renderer, RouteMatchInterface $route_match) {
43     $this->ajaxRenderer = $ajax_renderer;
44     $this->routeMatch = $route_match;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function buildResponse(Request $request, array $form, FormStateInterface $form_state, array $commands) {
51     // If the form build ID has changed, issue an Ajax command to update it.
52     if (isset($form['#build_id_old']) && $form['#build_id_old'] !== $form['#build_id']) {
53       $commands[] = new UpdateBuildIdCommand($form['#build_id_old'], $form['#build_id']);
54     }
55
56     // We need to return the part of the form (or some other content) that needs
57     // to be re-rendered so the browser can update the page with changed
58     // content. It is up to the #ajax['callback'] function of the element (may
59     // or may not be a button) that triggered the Ajax request to determine what
60     // needs to be rendered.
61     $callback = NULL;
62     if (($triggering_element = $form_state->getTriggeringElement()) && isset($triggering_element['#ajax']['callback'])) {
63       $callback = $triggering_element['#ajax']['callback'];
64     }
65     $callback = $form_state->prepareCallback($callback);
66     if (empty($callback) || !is_callable($callback)) {
67       throw new HttpException(500, 'The specified #ajax callback is empty or not callable.');
68     }
69     $result = call_user_func_array($callback, [&$form, &$form_state, $request]);
70
71     // If the callback is an #ajax callback, the result is a render array, and
72     // we need to turn it into an AJAX response, so that we can add any commands
73     // we got earlier; typically the UpdateBuildIdCommand when handling an AJAX
74     // submit from a cached page.
75     if ($result instanceof AjaxResponse) {
76       $response = $result;
77     }
78     else {
79       // At this point we know callback returned a render element. If the
80       // element is part of the group (#group is set on it) it won't be rendered
81       // unless we remove #group from it. This is caused by
82       // \Drupal\Core\Render\Element\RenderElement::preRenderGroup(), which
83       // prevents all members of groups from being rendered directly.
84       if (!empty($result['#group'])) {
85         unset($result['#group']);
86       }
87
88       /** @var \Drupal\Core\Ajax\AjaxResponse $response */
89       $response = $this->ajaxRenderer->renderResponse($result, $request, $this->routeMatch);
90     }
91
92     foreach ($commands as $command) {
93       $response->addCommand($command, TRUE);
94     }
95     return $response;
96   }
97
98 }