class AssetLibrary in Libraries API 8.3
Provides a class for a single attachable asset library.
Hierarchy
- class \Drupal\libraries\ExternalLibrary\LibraryBase implements DependentLibraryInterface, LibraryInterface, VersionedLibraryInterface uses IdAccessorTrait
- class \Drupal\libraries\ExternalLibrary\Asset\AssetLibrary implements AssetLibraryInterface, LocalLibraryInterface, RemoteLibraryInterface uses LocalLibraryTrait
Expanded class hierarchy of AssetLibrary
1 file declares its use of AssetLibrary
- AssetLibraryType.php in src/
Plugin/ libraries/ Type/ AssetLibraryType.php
File
- src/
ExternalLibrary/ Asset/ AssetLibrary.php, line 18
Namespace
Drupal\libraries\ExternalLibrary\AssetView source
class AssetLibrary extends LibraryBase implements AssetLibraryInterface, LocalLibraryInterface, RemoteLibraryInterface {
use LocalLibraryTrait, RemoteLibraryTrait, LocalRemoteAssetTrait;
/**
* An array containing the CSS assets of the library.
*
* @var array
*/
protected $cssAssets = [];
/**
* An array containing the JavaScript assets of the library.
*
* @var array
*/
protected $jsAssets = [];
/**
* An array of attachable asset library IDs that this library depends on.
*
* @todo Explain the difference to regular dependencies.
*/
protected $attachableDependencies = [];
/**
* 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->cssAssets = $definition['css'];
$this->jsAssets = $definition['js'];
$this->attachableDependencies = $definition['attachable_dependencies'];
}
/**
* {@inheritdoc}
*/
protected static function processDefinition(array &$definition) {
parent::processDefinition($definition);
$definition += [
'remote_url' => '',
'css' => [],
'js' => [],
'attachable_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 getAttachableAssetLibrary(LibraryManagerInterface $library_manager) {
if (!$this
->canBeAttached()) {
throw new LibraryNotInstalledException($this);
}
return [
'version' => $this
->getVersion(),
'css' => $this
->processCssAssets($this->cssAssets),
'js' => $this
->processJsAssets($this->jsAssets),
'dependencies' => $this->attachableDependencies,
];
}
/**
* 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AssetLibrary:: |
protected | property | An array of attachable asset library IDs that this library depends on. | |
AssetLibrary:: |
protected | property | An array containing the CSS assets of the library. | |
AssetLibrary:: |
protected | property | An array containing the JavaScript assets of the library. | |
AssetLibrary:: |
public | function |
Returns a core library array structure for this library. Overrides AssetLibraryInterface:: |
|
AssetLibrary:: |
public | function |
Gets the locator of this library using the locator factory. Overrides LocalLibraryInterface:: |
|
AssetLibrary:: |
protected static | function |
Gets library definition defaults. Overrides LibraryBase:: |
|
AssetLibrary:: |
public | function |
Construct an external library. Overrides LibraryBase:: |
|
DependentLibraryInterface:: |
public | function | Returns the libraries dependencies, if any. | |
IdAccessorTrait:: |
protected | property | The ID. | |
IdAccessorTrait:: |
public | function | Returns the ID. | |
LibraryBase:: |
protected | property | The library type of this library. | |
LibraryBase:: |
public static | function |
Creates an instance of the library from its definition. Overrides LibraryInterface:: |
|
LibraryBase:: |
public | function |
Returns the library type of the library. Overrides LibraryInterface:: |
|
LocalLibraryTrait:: |
protected | property | Whether or not the library is installed. | |
LocalLibraryTrait:: |
protected | property | The local path to the library relative to the app root. | |
LocalLibraryTrait:: |
public | function | Gets the path to the library. | |
LocalLibraryTrait:: |
public | function | Checks whether the library is installed. | |
LocalLibraryTrait:: |
public | function | Sets the library path of the library. | |
LocalLibraryTrait:: |
public | function | Marks the library as uninstalled. | |
RemoteLibraryInterface:: |
public | function | Returns the remote URL of the library. | |
RemoteLibraryInterface:: |
public | function | Checks whether the library has a remote URL. | |
VersionedLibraryInterface:: |
public | function | Gets the version of the library. | |
VersionedLibraryInterface:: |
public | function | Gets the version detector of this library using the detector factory. | |
VersionedLibraryInterface:: |
public | function | Sets the version of the library. |