function LibraryDiscoveryParser::applyLibrariesOverride

Same name in other branches
  1. 9 core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php \Drupal\Core\Asset\LibraryDiscoveryParser::applyLibrariesOverride()
  2. 10 core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php \Drupal\Core\Asset\LibraryDiscoveryParser::applyLibrariesOverride()
  3. 11.x core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php \Drupal\Core\Asset\LibraryDiscoveryParser::applyLibrariesOverride()

Apply libraries overrides specified for the current active theme.

Parameters

array $libraries: The libraries definitions.

string $extension: The extension in which these libraries are defined.

Return value

array The modified libraries definitions.

1 call to LibraryDiscoveryParser::applyLibrariesOverride()
LibraryDiscoveryParser::buildByExtension in core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php
Parses and builds up all the libraries information of an extension.

File

core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php, line 380

Class

LibraryDiscoveryParser
Parses library files to get extension data.

Namespace

Drupal\Core\Asset

Code

protected function applyLibrariesOverride($libraries, $extension) {
    $active_theme = $this->themeManager
        ->getActiveTheme();
    // ActiveTheme::getLibrariesOverride() returns libraries-overrides for the
    // current theme as well as all its base themes.
    $all_libraries_overrides = $active_theme->getLibrariesOverride();
    foreach ($all_libraries_overrides as $theme_path => $libraries_overrides) {
        foreach ($libraries as $library_name => $library) {
            // Process libraries overrides.
            if (isset($libraries_overrides["{$extension}/{$library_name}"])) {
                // Active theme defines an override for this library.
                $override_definition = $libraries_overrides["{$extension}/{$library_name}"];
                if (is_string($override_definition) || $override_definition === FALSE) {
                    // A string or boolean definition implies an override (or removal)
                    // for the whole library. Use the override key to specify that this
                    // library will be overridden when it is called.
                    // @see \Drupal\Core\Asset\LibraryDiscovery::getLibraryByName()
                    if ($override_definition) {
                        $libraries[$library_name]['override'] = $override_definition;
                    }
                    else {
                        $libraries[$library_name]['override'] = FALSE;
                    }
                }
                elseif (is_array($override_definition)) {
                    // An array definition implies an override for an asset within this
                    // library.
                    foreach ($override_definition as $sub_key => $value) {
                        // Throw an exception if the asset is not properly specified.
                        if (!is_array($value)) {
                            throw new InvalidLibrariesOverrideSpecificationException(sprintf('Library asset %s is not correctly specified. It should be in the form "extension/library_name/sub_key/path/to/asset.js".', "{$extension}/{$library_name}/{$sub_key}"));
                        }
                        if ($sub_key === 'drupalSettings') {
                            // drupalSettings may not be overridden.
                            throw new InvalidLibrariesOverrideSpecificationException(sprintf('drupalSettings may not be overridden in libraries-override. Trying to override %s. Use hook_library_info_alter() instead.', "{$extension}/{$library_name}/{$sub_key}"));
                        }
                        elseif ($sub_key === 'css') {
                            // SMACSS category should be incorporated into the asset name.
                            foreach ($value as $category => $overrides) {
                                $this->setOverrideValue($libraries[$library_name], [
                                    $sub_key,
                                    $category,
                                ], $overrides, $theme_path);
                            }
                        }
                        else {
                            $this->setOverrideValue($libraries[$library_name], [
                                $sub_key,
                            ], $value, $theme_path);
                        }
                    }
                }
            }
        }
    }
    return $libraries;
}

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