LibraryDiscoveryParser::parseLibraryInfo

protected LibraryDiscoveryParser::parseLibraryInfo($extension, $path)

Parses a given library file and allows modules and themes to alter it.

This method sets the parsed information onto the library property.

Library information is parsed from *.libraries.yml files; see editor.library.yml for an example. Every library must have at least one js or css entry. Each entry starts with a machine name and defines the following elements:

  • js: A list of JavaScript files to include. Each file is keyed by the file path. An item can have several attributes (like HTML attributes). For example:
  js:
    path/js/file.js: { attributes: { defer: true } }
  

If the file has no special attributes, just use an empty object:

  js:
    path/js/file.js: {}
  

The path of the file is relative to the module or theme directory, unless it starts with a /, in which case it is relative to the Drupal root. If the file path starts with //, it will be treated as a protocol-free, external resource (e.g., //cdn.com/library.js). Full URLs (e.g., http://cdn.com/library.js) as well as URLs that use a valid stream wrapper (e.g., public://path/to/file.js) are also supported.

  • css: A list of categories for which the library provides CSS files. The available categories are:
    • base
    • layout
    • component
    • state
    • theme

    Each category is itself a key for a sub-list of CSS files to include:

  css:
    component:
      css/file.css: {}
  

Just like with JavaScript files, each CSS file is the key of an object that can define specific attributes. The format of the file path is the same as for the JavaScript files.

  • dependencies: A list of libraries this library depends on.
  • version: The library version. The string "VERSION" can be used to mean the current Drupal core version.
  • header: By default, JavaScript files are included in the footer. If the script must be included in the header (along with all its dependencies), set this to true. Defaults to false.
  • minified: If the file is already minified, set this to true to avoid minifying it again. Defaults to false.
  • remote: If the library is a third-party script, this provides the repository URL for reference.
  • license: If the remote property is set, the license information is required. It has 3 properties:
    • name: The human-readable name of the license.
    • url: The URL of the license file/information for the version of the library used.
    • gpl-compatible: A Boolean for whether this library is GPL compatible.

See https://www.drupal.org/node/2274843#define-library for more information.

Parameters

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

string $path: The relative path to the extension.

Return value

array An array of parsed library data.

Throws

\Drupal\Core\Asset\Exception\InvalidLibraryFileException Thrown when a parser exception got thrown.

File

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

Class

LibraryDiscoveryParser
Parses library files to get extension data.

Namespace

Drupal\Core\Asset

Code

protected function parseLibraryInfo($extension, $path) {
  $libraries = [];

  $library_file = $path . '/' . $extension . '.libraries.yml';
  if (file_exists($this->root . '/' . $library_file)) {
    try {
      $libraries = Yaml::decode(file_get_contents($this->root . '/' . $library_file));
    }
    catch (InvalidDataTypeException $e) {
      // Rethrow a more helpful exception to provide context.
      throw new InvalidLibraryFileException(sprintf('Invalid library definition in %s: %s', $library_file, $e->getMessage()), 0, $e);
    }
  }

  // Allow modules to add dynamic library definitions.
  $hook = 'library_info_build';
  if ($this->moduleHandler->implementsHook($extension, $hook)) {
    $libraries = NestedArray::mergeDeep($libraries, $this->moduleHandler->invoke($extension, $hook));
  }

  // Allow modules to alter the module's registered libraries.
  $this->moduleHandler->alter('library_info', $libraries, $extension);
  $this->themeManager->alter('library_info', $libraries, $extension);

  return $libraries;
}
doc_Drupal
2016-10-29 09:23:11
Comments
Leave a Comment

Please login to continue.