You are here

protected function LibraryDiscoveryParser::setOverrideValue in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php \Drupal\Core\Asset\LibraryDiscoveryParser::setOverrideValue()
  2. 9 core/lib/Drupal/Core/Asset/LibraryDiscoveryParser.php \Drupal\Core\Asset\LibraryDiscoveryParser::setOverrideValue()

Overrides the specified library asset.

Parameters

array $library: The containing library definition.

array $sub_key: An array containing the sub-keys specifying the library asset, e.g. ['js'] or ['css', 'component'].

array $overrides: Specifies the overrides, this is an array where the key is the asset to be overridden while the value is overriding asset.

string $theme_path: The theme or base theme.

File

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

Class

LibraryDiscoveryParser
Parses library files to get extension data.

Namespace

Drupal\Core\Asset

Code

protected function setOverrideValue(array &$library, array $sub_key, array $overrides, $theme_path) {
  foreach ($overrides as $original => $replacement) {

    // Get the attributes of the asset to be overridden. If the key does
    // not exist, then throw an exception.
    $key_exists = NULL;
    $parents = array_merge($sub_key, [
      $original,
    ]);

    // Save the attributes of the library asset to be overridden.
    $attributes = NestedArray::getValue($library, $parents, $key_exists);
    if ($key_exists) {

      // Remove asset to be overridden.
      NestedArray::unsetValue($library, $parents);

      // No need to replace if FALSE is specified, since that is a removal.
      if ($replacement) {

        // Ensure the replacement path is relative to drupal root.
        $replacement = $this
          ->resolveThemeAssetPath($theme_path, $replacement);
        $new_parents = array_merge($sub_key, [
          $replacement,
        ]);

        // Replace with an override if specified.
        NestedArray::setValue($library, $new_parents, $attributes);
      }
    }
  }
}