AjaxRenderer.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Drupal\Core\Render\MainContent;
  3. use Drupal\Core\Ajax\AjaxResponse;
  4. use Drupal\Core\Ajax\AlertCommand;
  5. use Drupal\Core\Ajax\InsertCommand;
  6. use Drupal\Core\Ajax\PrependCommand;
  7. use Drupal\Core\Render\ElementInfoManagerInterface;
  8. use Drupal\Core\Render\RendererInterface;
  9. use Drupal\Core\Routing\RouteMatchInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. /**
  12. * Default main content renderer for Ajax requests.
  13. */
  14. class AjaxRenderer implements MainContentRendererInterface {
  15. /**
  16. * The element info manager.
  17. *
  18. * @var \Drupal\Core\Render\ElementInfoManagerInterface
  19. */
  20. protected $elementInfoManager;
  21. /**
  22. * The renderer.
  23. *
  24. * @var \Drupal\Core\Render\RendererInterface
  25. */
  26. protected $renderer;
  27. /**
  28. * Constructs a new AjaxRenderer instance.
  29. *
  30. * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info_manager
  31. * The element info manager.
  32. * @param \Drupal\Core\Render\RendererInterface $renderer
  33. * The renderer.
  34. */
  35. public function __construct(ElementInfoManagerInterface $element_info_manager, RendererInterface $renderer = NULL) {
  36. $this->elementInfoManager = $element_info_manager;
  37. if ($renderer === NULL) {
  38. @trigger_error('The renderer service must be passed to ' . __METHOD__ . ' and will be required before Drupal 9.0.0. See https://www.drupal.org/node/3009400', E_USER_DEPRECATED);
  39. $renderer = \Drupal::service('renderer');
  40. }
  41. $this->renderer = $renderer;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match) {
  47. $response = new AjaxResponse();
  48. if (isset($main_content['#type']) && ($main_content['#type'] == 'ajax')) {
  49. // Complex Ajax callbacks can return a result that contains an error
  50. // message or a specific set of commands to send to the browser.
  51. $main_content += $this->elementInfoManager->getInfo('ajax');
  52. $error = $main_content['#error'];
  53. if (!empty($error)) {
  54. // Fall back to some default message otherwise use the specific one.
  55. if (!is_string($error)) {
  56. $error = 'An error occurred while handling the request: The server received invalid input.';
  57. }
  58. $response->addCommand(new AlertCommand($error));
  59. }
  60. }
  61. $html = $this->renderer->renderRoot($main_content);
  62. $response->setAttachments($main_content['#attached']);
  63. // The selector for the insert command is NULL as the new content will
  64. // replace the element making the Ajax call. The default 'replaceWith'
  65. // behavior can be changed with #ajax['method'].
  66. $response->addCommand(new InsertCommand(NULL, $html));
  67. $status_messages = ['#type' => 'status_messages'];
  68. $output = $this->renderer->renderRoot($status_messages);
  69. if (!empty($output)) {
  70. $response->addCommand(new PrependCommand(NULL, $output));
  71. }
  72. return $response;
  73. }
  74. /**
  75. * Wraps \Drupal\Core\Render\RendererInterface::renderRoot().
  76. *
  77. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
  78. * $this->renderer->renderRoot() instead.
  79. *
  80. * @see https://www.drupal.org/node/2912696
  81. */
  82. protected function drupalRenderRoot(&$elements) {
  83. @trigger_error('\Drupal\Core\Render\MainContent\AjaxRenderer::drupalRenderRoot() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use $this->renderer->renderRoot() instead. See https://www.drupal.org/node/2912696', E_USER_DEPRECATED);
  84. return $this->renderer->renderRoot($elements);
  85. }
  86. }