3 namespace Drupal\libraries\ExternalLibrary\Asset;
5 use Drupal\Component\Plugin\Factory\FactoryInterface;
6 use Drupal\libraries\ExternalLibrary\Dependency\DependentLibraryInterface;
7 use Drupal\libraries\ExternalLibrary\Exception\LibraryNotInstalledException;
8 use Drupal\libraries\ExternalLibrary\LibraryBase;
9 use Drupal\libraries\ExternalLibrary\LibraryManagerInterface;
10 use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface;
11 use Drupal\libraries\ExternalLibrary\Local\LocalLibraryTrait;
12 use Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryInterface;
13 use Drupal\libraries\ExternalLibrary\Remote\RemoteLibraryTrait;
14 use Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface;
15 use Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface;
18 * Provides a class for a library with multiple attachable asset libraries.
20 class MultipleAssetLibrary extends LibraryBase implements
21 MultipleAssetLibraryInterface,
22 VersionedLibraryInterface,
23 DependentLibraryInterface,
24 LocalLibraryInterface,
25 RemoteLibraryInterface
35 * An array of attachable asset libraries.
37 protected $libraries = [];
40 * Construct an external library.
44 * @param array $definition
45 * The library definition array.
46 * @param \Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface $library_type
47 * The library type of the library.
49 public function __construct($id, array $definition, LibraryTypeInterface $library_type) {
50 parent::__construct($id, $definition, $library_type);
51 $this->remoteUrl = $definition['remote_url'];
52 $this->libraries = $definition['libraries'];
58 protected static function processDefinition(array &$definition) {
59 parent::processDefinition($definition);
64 foreach ($definition['libraries'] as &$library) {
74 * Returns a core library array structure for this library.
76 * @param \Drupal\libraries\ExternalLibrary\LibraryManagerInterface $library_manager
77 * The library manager that can be used to fetch dependencies.
81 * @see \Drupal\libraries\ExternalLibrary\Asset\getAttachableAssetLibraries::getAttachableAssetLibraries()
83 * @throws \Drupal\libraries\ExternalLibrary\Exception\InvalidLibraryDependencyException
84 * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException
85 * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryTypeNotFoundException
86 * @throws \Drupal\Component\Plugin\Exception\PluginException
88 * @todo Document the return value.
90 public function getAttachableAssetLibraries(LibraryManagerInterface $library_manager) {
91 if (!$this->canBeAttached()) {
92 throw new LibraryNotInstalledException($this);
94 $attachable_libraries = [];
95 foreach ($this->libraries as $attachable_library_id => $attachable_library) {
96 $attachable_libraries[$attachable_library_id] = [
97 'version' => $this->getVersion(),
98 'css' => $this->processCssAssets($attachable_library['css']),
99 'js' => $this->processJsAssets($attachable_library['js']),
100 'dependencies' => $attachable_library['dependencies'],
103 return $attachable_libraries;
107 * Gets the locator of this library using the locator factory.
109 * @param \Drupal\Component\Plugin\Factory\FactoryInterface $locator_factory
111 * @return \Drupal\libraries\ExternalLibrary\Local\LocatorInterface
113 * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::getLocator()
115 public function getLocator(FactoryInterface $locator_factory) {
116 // @todo Consider consolidating the stream wrappers used here. For now we
117 // allow asset libs to live almost anywhere.
118 return $locator_factory->createInstance('chain')
119 ->addLocator($locator_factory->createInstance('uri', ['uri' => 'asset://']))
120 ->addLocator($locator_factory->createInstance('uri', ['uri' => 'php-file://']));