function ComponentPluginManager::libraryFromDefinition

Same name in this branch
  1. 11.x core/modules/sdc/src/ComponentPluginManager.php \Drupal\sdc\ComponentPluginManager::libraryFromDefinition()
Same name in other branches
  1. 10 core/modules/sdc/src/ComponentPluginManager.php \Drupal\sdc\ComponentPluginManager::libraryFromDefinition()
  2. 10 core/lib/Drupal/Core/Theme/ComponentPluginManager.php \Drupal\Core\Theme\ComponentPluginManager::libraryFromDefinition()

Creates the library declaration array from a component definition.

Parameters

array $definition: The component definition.

Return value

array The library for the Library API.

1 call to ComponentPluginManager::libraryFromDefinition()
ComponentPluginManager::alterDefinition in core/lib/Drupal/Core/Theme/ComponentPluginManager.php
Alters the plugin definition with computed properties.

File

core/lib/Drupal/Core/Theme/ComponentPluginManager.php, line 173

Class

ComponentPluginManager
Defines a plugin manager to deal with components.

Namespace

Drupal\Core\Theme

Code

protected function libraryFromDefinition(array $definition) : array {
    $metadata_path = $definition[YamlDirectoryDiscovery::FILE_KEY];
    $component_directory = $this->fileSystem
        ->dirname($metadata_path);
    // Add the JS and CSS files.
    $library = [];
    $css_file = $this->findAsset($component_directory, $definition['machineName'], 'css', TRUE);
    if ($css_file) {
        $library['css']['component'][$css_file] = [];
    }
    $js_file = $this->findAsset($component_directory, $definition['machineName'], 'js', TRUE);
    if ($js_file) {
        $library['js'][$js_file] = [];
    }
    // We allow component authors to use library overrides to use files relative
    // to the component directory. So we need to fix the paths here.
    if (!empty($definition['libraryOverrides'])) {
        $overrides = $this->translateLibraryPaths($definition['libraryOverrides'], $component_directory);
        // Apply library overrides.
        $library = array_merge($library, $overrides);
        // Ensure that 'core/drupal' is always a dependency. This will ensure that
        // JS behaviors are attached.
        $library['dependencies'][] = 'core/drupal';
        $library['dependencies'] = array_unique($library['dependencies']);
    }
    return $library;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.