You are here

class MultipleAssetLibrary in Libraries API 8.3

Provides a class for a library with multiple attachable asset libraries.

Hierarchy

Expanded class hierarchy of MultipleAssetLibrary

1 file declares its use of MultipleAssetLibrary
MultipleAssetLibraryType.php in src/Plugin/libraries/Type/MultipleAssetLibraryType.php

File

src/ExternalLibrary/Asset/MultipleAssetLibrary.php, line 20

Namespace

Drupal\libraries\ExternalLibrary\Asset
View source
class MultipleAssetLibrary extends LibraryBase implements MultipleAssetLibraryInterface, VersionedLibraryInterface, DependentLibraryInterface, LocalLibraryInterface, RemoteLibraryInterface {
  use LocalLibraryTrait, RemoteLibraryTrait, LocalRemoteAssetTrait;

  /**
   * An array of attachable asset libraries.
   */
  protected $libraries = [];

  /**
   * Construct an external library.
   *
   * @param string $id
   *   The library ID.
   * @param array $definition
   *   The library definition array.
   * @param \Drupal\libraries\ExternalLibrary\Type\LibraryTypeInterface $library_type
   *   The library type of the library.
   */
  public function __construct($id, array $definition, LibraryTypeInterface $library_type) {
    parent::__construct($id, $definition, $library_type);
    $this->remoteUrl = $definition['remote_url'];
    $this->libraries = $definition['libraries'];
  }

  /**
   * {@inheritdoc}
   */
  protected static function processDefinition(array &$definition) {
    parent::processDefinition($definition);
    $definition += [
      'remote_url' => '',
      'libraries' => [],
    ];
    foreach ($definition['libraries'] as &$library) {
      $library += [
        'css' => [],
        'js' => [],
        'dependencies' => [],
      ];
    }
  }

  /**
   * Returns a core library array structure for this library.
   *
   * @param \Drupal\libraries\ExternalLibrary\LibraryManagerInterface $library_manager
   *   The library manager that can be used to fetch dependencies.
   *
   * @return array
   *
   * @see \Drupal\libraries\ExternalLibrary\Asset\getAttachableAssetLibraries::getAttachableAssetLibraries()
   *
   * @throws \Drupal\libraries\ExternalLibrary\Exception\InvalidLibraryDependencyException
   * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException
   * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryTypeNotFoundException
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   *
   * @todo Document the return value.
   */
  public function getAttachableAssetLibraries(LibraryManagerInterface $library_manager) {
    if (!$this
      ->canBeAttached()) {
      throw new LibraryNotInstalledException($this);
    }
    $attachable_libraries = [];
    foreach ($this->libraries as $attachable_library_id => $attachable_library) {
      $attachable_libraries[$attachable_library_id] = [
        'version' => $this
          ->getVersion(),
        'css' => $this
          ->processCssAssets($attachable_library['css']),
        'js' => $this
          ->processJsAssets($attachable_library['js']),
        'dependencies' => $attachable_library['dependencies'],
      ];
    }
    return $attachable_libraries;
  }

  /**
   * Gets the locator of this library using the locator factory.
   *
   * @param \Drupal\Component\Plugin\Factory\FactoryInterface $locator_factory
   *
   * @return \Drupal\libraries\ExternalLibrary\Local\LocatorInterface
   *
   * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::getLocator()
   */
  public function getLocator(FactoryInterface $locator_factory) {

    // @todo Consider consolidating the stream wrappers used here. For now we
    // allow asset libs to live almost anywhere.
    return $locator_factory
      ->createInstance('chain')
      ->addLocator($locator_factory
      ->createInstance('uri', [
      'uri' => 'asset://',
    ]))
      ->addLocator($locator_factory
      ->createInstance('uri', [
      'uri' => 'php-file://',
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependentLibraryInterface::getDependencies public function Returns the libraries dependencies, if any.
IdAccessorTrait::$id protected property The ID.
IdAccessorTrait::getId public function Returns the ID.
LibraryBase::$type protected property The library type of this library.
LibraryBase::create public static function Creates an instance of the library from its definition. Overrides LibraryInterface::create
LibraryBase::getType public function Returns the library type of the library. Overrides LibraryInterface::getType
LocalLibraryTrait::$installed protected property Whether or not the library is installed.
LocalLibraryTrait::$localPath protected property The local path to the library relative to the app root.
LocalLibraryTrait::getLocalPath public function Gets the path to the library.
LocalLibraryTrait::isInstalled public function Checks whether the library is installed.
LocalLibraryTrait::setLocalPath public function Sets the library path of the library.
LocalLibraryTrait::setUninstalled public function Marks the library as uninstalled.
MultipleAssetLibrary::$libraries protected property An array of attachable asset libraries.
MultipleAssetLibrary::getAttachableAssetLibraries public function Returns a core library array structure for this library. Overrides MultipleAssetLibraryInterface::getAttachableAssetLibraries
MultipleAssetLibrary::getLocator public function Gets the locator of this library using the locator factory. Overrides LocalLibraryInterface::getLocator
MultipleAssetLibrary::processDefinition protected static function Gets library definition defaults. Overrides LibraryBase::processDefinition
MultipleAssetLibrary::__construct public function Construct an external library. Overrides LibraryBase::__construct
MultipleAssetLibraryInterface::SEPARATOR constant Separates the library machine name from its component name.
RemoteLibraryInterface::getRemoteUrl public function Returns the remote URL of the library.
RemoteLibraryInterface::hasRemoteUrl public function Checks whether the library has a remote URL.
VersionedLibraryInterface::getVersion public function Gets the version of the library.
VersionedLibraryInterface::getVersionDetector public function Gets the version detector of this library using the detector factory.
VersionedLibraryInterface::setVersion public function Sets the version of the library.