Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Ajax / CommandWithAttachedAssetsTrait.php
1 <?php
2
3 namespace Drupal\Core\Ajax;
4
5 use Drupal\Core\Asset\AttachedAssets;
6
7 /**
8  * Trait for Ajax commands that render content and attach assets.
9  *
10  * @ingroup ajax
11  */
12 trait CommandWithAttachedAssetsTrait {
13
14   /**
15    * The attached assets for this Ajax command.
16    *
17    * @var \Drupal\Core\Asset\AttachedAssets
18    */
19   protected $attachedAssets;
20
21   /**
22    * Processes the content for output.
23    *
24    * If content is a render array, it may contain attached assets to be
25    * processed.
26    *
27    * @return string|\Drupal\Component\Render\MarkupInterface
28    *   HTML rendered content.
29    */
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);
35       return $html;
36     }
37     else {
38       return $this->content;
39     }
40   }
41
42   /**
43    * Gets the attached assets.
44    *
45    * @return \Drupal\Core\Asset\AttachedAssets|null
46    *   The attached assets for this command.
47    */
48   public function getAttachedAssets() {
49     return $this->attachedAssets;
50   }
51
52 }