Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ComponentPluginManager.php

function 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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal