3 namespace Drupal\Core\Ajax;
5 use Drupal\Core\Asset\AttachedAssets;
8 * Trait for Ajax commands that render content and attach assets.
12 trait CommandWithAttachedAssetsTrait {
15 * The attached assets for this Ajax command.
17 * @var \Drupal\Core\Asset\AttachedAssets
19 protected $attachedAssets;
22 * Processes the content for output.
24 * If content is a render array, it may contain attached assets to be
27 * @return string|\Drupal\Component\Render\MarkupInterface
28 * HTML rendered content.
30 protected function getRenderedContent() {
31 $this->attachedAssets = new AttachedAssets();
32 if (is_array($this->content)) {
33 $html = \Drupal::service('renderer')->renderRoot($this->content);
34 $this->attachedAssets = AttachedAssets::createFromRenderArray($this->content);
38 return $this->content;
43 * Gets the attached assets.
45 * @return \Drupal\Core\Asset\AttachedAssets|null
46 * The attached assets for this command.
48 public function getAttachedAssets() {
49 return $this->attachedAssets;