3 namespace Drupal\Core\Asset;
6 * The default attached assets collection.
8 class AttachedAssets implements AttachedAssetsInterface {
11 * The (ordered) list of asset libraries attached to the current response.
15 public $libraries = [];
18 * The JavaScript settings attached to the current response.
22 public $settings = [];
25 * The set of asset libraries that the client has already loaded.
29 protected $alreadyLoadedLibraries = [];
34 public static function createFromRenderArray(array $render_array) {
35 if (!isset($render_array['#attached'])) {
36 throw new \LogicException('The render array has not yet been rendered, hence not all attachments have been collected yet.');
39 $assets = new static();
40 if (isset($render_array['#attached']['library'])) {
41 $assets->setLibraries($render_array['#attached']['library']);
43 if (isset($render_array['#attached']['drupalSettings'])) {
44 $assets->setSettings($render_array['#attached']['drupalSettings']);
52 public function setLibraries(array $libraries) {
53 $this->libraries = array_unique($libraries);
60 public function getLibraries() {
61 return $this->libraries;
67 public function setSettings(array $settings) {
68 $this->settings = $settings;
75 public function getSettings() {
76 return $this->settings;
82 public function getAlreadyLoadedLibraries() {
83 return $this->alreadyLoadedLibraries;
89 public function setAlreadyLoadedLibraries(array $libraries) {
90 $this->alreadyLoadedLibraries = $libraries;