diff --git a/core/lib/Drupal/Core/Ajax/AjaxResponse.php b/core/lib/Drupal/Core/Ajax/AjaxResponse.php index fcb45af..d85476c 100644 --- a/core/lib/Drupal/Core/Ajax/AjaxResponse.php +++ b/core/lib/Drupal/Core/Ajax/AjaxResponse.php @@ -65,6 +65,9 @@ public function setAttachments(array $attachments) { * The current AjaxResponse. */ public function addCommand(CommandInterface $command, $prepend = FALSE) { + if ($command instanceof ResponseAwareCommandInterface) { + $command->setResponse($this); + } if ($prepend) { array_unshift($this->commands, $command->render()); } diff --git a/core/lib/Drupal/Core/Ajax/InsertCommand.php b/core/lib/Drupal/Core/Ajax/InsertCommand.php index 2a31e3e..3235930 100644 --- a/core/lib/Drupal/Core/Ajax/InsertCommand.php +++ b/core/lib/Drupal/Core/Ajax/InsertCommand.php @@ -21,7 +21,9 @@ * * @ingroup ajax */ -class InsertCommand implements CommandInterface { +class InsertCommand implements CommandInterface, ResponseAwareCommandInterface { + + use ResponseAwareCommandTrait; /** * A CSS selector string. @@ -67,26 +69,6 @@ public function __construct($selector, $content, array $settings = NULL) { } /** - * Processes the content for output. - * - * If content is a render array, it may contain attached assets to be - * processed. - * - * @return string - * HTML rendered content. - */ - protected function getRenderedContent() { - if (is_array($this->content)) { - $html = \Drupal::service('renderer')->render($this->content); - drupal_process_attached($this->content); - return $html; - } - else { - return $this->content; - } - } - - /** * Implements Drupal\Core\Ajax\CommandInterface:render(). */ public function render() { diff --git a/core/lib/Drupal/Core/Ajax/OpenDialogCommand.php b/core/lib/Drupal/Core/Ajax/OpenDialogCommand.php index b4793c5..1b1caf2 100644 --- a/core/lib/Drupal/Core/Ajax/OpenDialogCommand.php +++ b/core/lib/Drupal/Core/Ajax/OpenDialogCommand.php @@ -14,7 +14,9 @@ * * @ingroup ajax */ -class OpenDialogCommand implements CommandInterface { +class OpenDialogCommand implements CommandInterface, ResponseAwareCommandInterface { + + use ResponseAwareCommandTrait; /** * The selector of the dialog. @@ -125,26 +127,6 @@ public function setDialogTitle($title) { } /** - * Processes the content for output. - * - * If content is a render array, it may contain attached assets to be - * processed. - * - * @return string - * HTML rendered content. - */ - protected function getRenderedContent() { - if (is_array($this->content)) { - $html = \Drupal::service('renderer')->render($this->content); - drupal_process_attached($this->content); - return $html; - } - else { - return $this->content; - } - } - - /** * Implements \Drupal\Core\Ajax\CommandInterface:render(). */ public function render() { diff --git a/core/lib/Drupal/Core/Ajax/ResponseAwareCommandInterface.php b/core/lib/Drupal/Core/Ajax/ResponseAwareCommandInterface.php new file mode 100644 index 0000000..a9b17ad --- /dev/null +++ b/core/lib/Drupal/Core/Ajax/ResponseAwareCommandInterface.php @@ -0,0 +1,30 @@ +response = $response; + return $this; + } + + /** + * Processes the content for output. + * + * If content is a render array, it may contain attached assets to be + * processed. + * + * @return string + * HTML rendered content. + */ + protected function getRenderedContent() { + if (is_array($this->content)) { + $html = \Drupal::service('renderer')->render($this->content); + $this->response->setAttachments($this->content['#attached']); + return $html; + } + else { + return $this->content; + } + } +}