You are here

public function LibraryDiscoveryParser::buildByExtension in Drupal 10

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

Parses and builds up all the libraries information of an extension.

Parameters

string $extension: The name of the extension that registered a library.

Return value

array All library definitions of the passed extension.

Throws

\Drupal\Core\Asset\Exception\IncompleteLibraryDefinitionException Thrown when a library has no js/css/setting.

\UnexpectedValueException Thrown when a js file defines a positive weight.

File

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

Class

LibraryDiscoveryParser
Parses library files to get extension data.

Namespace

Drupal\Core\Asset

Code

public function buildByExtension($extension) {
  if ($extension === 'core') {
    $path = 'core';
    $extension_type = 'core';
  }
  else {
    if ($this->moduleHandler
      ->moduleExists($extension)) {
      $extension_type = 'module';
    }
    else {
      $extension_type = 'theme';
    }
    $path = $this->extensionPathResolver
      ->getPath($extension_type, $extension);
  }
  $libraries = $this
    ->parseLibraryInfo($extension, $path);
  $libraries = $this
    ->applyLibrariesOverride($libraries, $extension);
  foreach ($libraries as $id => &$library) {
    if (!isset($library['js']) && !isset($library['css']) && !isset($library['drupalSettings']) && !isset($library['dependencies'])) {
      throw new IncompleteLibraryDefinitionException(sprintf("Incomplete library definition for definition '%s' in extension '%s'", $id, $extension));
    }
    $library += [
      'dependencies' => [],
      'js' => [],
      'css' => [],
    ];
    if (isset($library['header']) && !is_bool($library['header'])) {
      throw new \LogicException(sprintf("The 'header' key in the library definition '%s' in extension '%s' is invalid: it must be a boolean.", $id, $extension));
    }
    if (isset($library['version'])) {

      // @todo Retrieve version of a non-core extension.
      if ($library['version'] === 'VERSION') {
        $library['version'] = \Drupal::VERSION;
      }
      elseif (is_string($library['version']) && $library['version'][0] === 'v') {
        $library['version'] = substr($library['version'], 1);
      }
    }

    // If this is a 3rd party library, the license info is required.
    if (isset($library['remote']) && !isset($library['license'])) {
      throw new LibraryDefinitionMissingLicenseException(sprintf("Missing license information in library definition for definition '%s' extension '%s': it has a remote, but no license.", $id, $extension));
    }

    // Assign Drupal's license to libraries that don't have license info.
    if (!isset($library['license'])) {
      $library['license'] = [
        'name' => 'GNU-GPL-2.0-or-later',
        'url' => 'https://www.drupal.org/licensing/faq',
        'gpl-compatible' => TRUE,
      ];
    }
    foreach ([
      'js',
      'css',
    ] as $type) {

      // Prepare (flatten) the SMACSS-categorized definitions.
      // @todo After Asset(ic) changes, retain the definitions as-is and
      //   properly resolve dependencies for all (css) libraries per category,
      //   and only once prior to rendering out an HTML page.
      if ($type == 'css' && !empty($library[$type])) {
        assert(static::validateCssLibrary($library[$type]) < 2, 'CSS files should be specified as key/value pairs, where the values are configuration options. See https://www.drupal.org/node/2274843.');
        assert(static::validateCssLibrary($library[$type]) === 0, 'CSS must be nested under a category. See https://www.drupal.org/node/2274843.');
        foreach ($library[$type] as $category => $files) {
          $category_weight = 'CSS_' . strtoupper($category);
          assert(defined($category_weight), 'Invalid CSS category: ' . $category . '. See https://www.drupal.org/node/2274843.');
          foreach ($files as $source => $options) {
            if (!isset($options['weight'])) {
              $options['weight'] = 0;
            }

            // Apply the corresponding weight defined by CSS_* constants.
            $options['weight'] += constant($category_weight);
            $library[$type][$source] = $options;
          }
          unset($library[$type][$category]);
        }
      }
      foreach ($library[$type] as $source => $options) {
        unset($library[$type][$source]);

        // Allow to omit the options hashmap in YAML declarations.
        if (!is_array($options)) {
          $options = [];
        }
        if ($type == 'js' && isset($options['weight']) && $options['weight'] > 0) {
          throw new \UnexpectedValueException("The {$extension}/{$id} library defines a positive weight for '{$source}'. Only negative weights are allowed (but should be avoided). Instead of a positive weight, specify accurate dependencies for this library.");
        }

        // Unconditionally apply default groups for the defined asset files.
        // The library system is a dependency management system. Each library
        // properly specifies its dependencies instead of relying on a custom
        // processing order.
        if ($type == 'js') {
          $options['group'] = JS_LIBRARY;
        }
        elseif ($type == 'css') {
          $options['group'] = $extension_type == 'theme' ? CSS_AGGREGATE_THEME : CSS_AGGREGATE_DEFAULT;
        }

        // By default, all library assets are files.
        if (!isset($options['type'])) {
          $options['type'] = 'file';
        }
        if ($options['type'] == 'external') {
          $options['data'] = $source;
        }
        else {
          if ($source[0] === '/') {

            // An absolute path maps to DRUPAL_ROOT / base_path().
            if ($source[1] !== '/') {
              $source = substr($source, 1);

              // Non core provided libraries can be in multiple locations.
              if (strpos($source, 'libraries/') === 0) {
                $path_to_source = $this->librariesDirectoryFileFinder
                  ->find(substr($source, 10));
                if ($path_to_source) {
                  $source = $path_to_source;
                }
              }
              $options['data'] = $source;
            }
            else {
              $options['type'] = 'external';
              $options['data'] = $source;
            }
          }
          elseif ($this->streamWrapperManager
            ->isValidUri($source)) {
            $options['data'] = $source;
          }
          elseif ($this
            ->isValidUri($source)) {
            $options['type'] = 'external';
            $options['data'] = $source;
          }
          else {
            $options['data'] = $path . '/' . $source;
          }
        }
        if (!isset($library['version'])) {

          // @todo Get the information from the extension.
          $options['version'] = -1;
        }
        else {
          $options['version'] = $library['version'];
        }

        // Set the 'minified' flag on JS file assets, default to FALSE.
        if ($type == 'js' && $options['type'] == 'file') {
          $options['minified'] = $options['minified'] ?? FALSE;
        }
        $library[$type][] = $options;
      }
    }
  }
  return $libraries;
}