3 namespace Drupal\libraries\ExternalLibrary\Asset;
6 * A trait for asset libraries that serve local and remote files.
8 * If the library files are available locally, they are served locally.
9 * Otherwise, the remote files are served, assuming a remote URL is specified.
11 * This trait should only be used in classes implementing LocalLibraryInterface
12 * and RemoteLibraryInterface.
14 * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface
15 * @see \Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface
17 trait LocalRemoteAssetTrait {
20 * Checks whether this library can be attached.
23 * TRUE if the library can be attached; FALSE otherwise.
25 * @see \Drupal\libraries\ExternalLibrary\Asset\SingleAssetLibraryTrait::canBeAttached()
27 protected function canBeAttached() {
28 /** @var \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface|\Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface $this */
29 return ($this->isInstalled() || $this->hasRemoteUrl());
33 * Gets the prefix to prepend to file paths.
35 * For local libraries this is the library path, for remote libraries this is
41 protected function getPathPrefix() {
42 /** @var \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface|\Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface $this */
43 if ($this->isInstalled()) {
44 // LocalLibraryInterface::getLocalPath() returns the path relative to the
45 // app root. In order for the core core asset system to register the path
46 // as relative to the app root, a leading slash is required.
47 /** @see \Drupal\Core\Asset\LibraryDiscoveryParser::buildByExtension() */
48 return '/' . $this->getLocalPath();
50 elseif ($this->hasRemoteUrl()) {
51 return $this->getRemoteUrl();
54 // @todo Throw an exception.
59 * Gets the CSS assets attached to this library.
61 * @param array $assets
64 * An array of CSS assets of the library following the core library CSS
65 * structure. The keys of the array must be among the SMACSS categories
66 * 'base', 'layout, 'component', 'state', and 'theme'. The value of each
67 * category is in turn an array where the keys are the file paths of the CSS
68 * files and values are CSS options.
70 * @see https://smacss.com/
72 * @see \Drupal\libraries\ExternalLibrary\Asset\SingleAssetLibraryTrait::getCssAssets()
74 protected function processCssAssets(array $assets) {
75 // @todo Consider somehow caching the processed information.
76 $processed_assets = [];
77 foreach ($assets as $category => $category_assets) {
78 // @todo Somehow consolidate this with getJsAssets().
79 foreach ($category_assets as $filename => $options) {
80 $processed_assets[$category][$this->getPathPrefix() . '/' . $filename] = $options;
83 return $processed_assets;
87 * Gets the JavaScript assets attached to this library.
89 * @param array $assets
92 * An array of JavaScript assets of the library. The keys of the array are
93 * the file paths of the JavaScript files and the values are JavaScript
96 * @see \Drupal\libraries\ExternalLibrary\Asset\SingleAssetLibraryTrait::getJsAssets()
98 protected function processJsAssets(array $assets) {
99 // @todo Consider somehow caching the processed information.
100 $processed_assets = [];
101 // @todo Somehow consolidate this with getCssAssets().
102 foreach ($assets as $filename => $options) {
103 $processed_assets[$this->getPathPrefix() . '/' . $filename] = $options;
105 return $processed_assets;