function InfoParserDynamic::parse

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Extension/InfoParserDynamic.php \Drupal\Core\Extension\InfoParserDynamic::parse()
  2. 8.9.x core/lib/Drupal/Core/Extension/InfoParserDynamic.php \Drupal\Core\Extension\InfoParserDynamic::parse()
  3. 11.x core/lib/Drupal/Core/Extension/InfoParserDynamic.php \Drupal\Core\Extension\InfoParserDynamic::parse()

Parses Drupal module, theme and profile .info.yml files.

Info files are NOT for placing arbitrary theme and module-specific settings. Use Config::get() and Config::set()->save() for that. Info files are formatted as YAML. If the 'version' key is set to 'VERSION' in any info file, then the value will be substituted with the current version of Drupal core.

Information stored in all .info.yml files:

  • name: The real name of the module for display purposes. (Required)
  • description: A brief description of the module.
  • type: whether it is for a module or theme. (Required)
  • core_version_requirement: Specifies the compatible version or versions of Drupal core. For example, "9.3 || 9.4" means compatibility with Drupal 9.3 and 9.4; ">=9" means compatible with Drupal 9, 10 and later versions, "<=9" means compatible with Drupal 8 and 9. (Required)
  • lifecycle: [experimental|stable|deprecated|obsolete]. A description of the current phase in the lifecycle of the module, theme or profile.

Information stored in a module .info.yml file:

  • dependencies: An array of dependency strings. Each is in the form 'project:module (versions)'; with the following meanings:

    • project: (optional) Project shortname, recommended to ensure uniqueness, if the module is part of a project hosted on drupal.org. If omitted, also omit the : that follows. The project name is currently ignored by Drupal core but is used for automated testing.
    • module: (required) Module shortname within the project.
    • (versions): Version information, consisting of one or more comma-separated operator/value pairs or simply version numbers, which can contain "x" as a wildcard. Examples: (>=8.22, <8.28), (8.x-3.x).
  • package: The name of the package of modules this module belongs to.

See node.info.yml for an example of a module .info.yml file.

Information stored in a theme .info.yml file:

  • screenshot: Path to screenshot relative to the theme's .info.yml file.
  • engine: Theme engine; typically twig.
  • base theme: Name of a base theme, if applicable.
  • regions: Listed regions.
  • features: Features available.
  • stylesheets: Theme stylesheets.
  • scripts: Theme scripts.

See olivero.info.yml for an example of a theme .info.yml file.

For information stored in a profile .info.yml file see install_profile_info().

Parameters

string $filename: The file we are parsing. Accepts file with relative or absolute path.

Return value

array The info array.

Overrides InfoParserInterface::parse

1 call to InfoParserDynamic::parse()
InfoParser::parse in core/lib/Drupal/Core/Extension/InfoParser.php
Parses Drupal module, theme and profile .info.yml files.
1 method overrides InfoParserDynamic::parse()
InfoParser::parse in core/lib/Drupal/Core/Extension/InfoParser.php
Parses Drupal module, theme and profile .info.yml files.

File

core/lib/Drupal/Core/Extension/InfoParserDynamic.php, line 38

Class

InfoParserDynamic
Parses dynamic .info.yml files that might change during the page request.

Namespace

Drupal\Core\Extension

Code

public function parse($filename) {
  if (!file_exists($filename)) {
    throw new InfoParserException("Unable to parse {$filename} as it does not exist");
  }
  try {
    $parsed_info = Yaml::decode(file_get_contents($filename));
  } catch (InvalidDataTypeException $e) {
    throw new InfoParserException("Unable to parse {$filename} " . $e->getMessage());
  }
  $missing_keys = array_diff($this->getRequiredKeys(), array_keys($parsed_info));
  if (!empty($missing_keys)) {
    throw new InfoParserException('Missing required keys (' . implode(', ', $missing_keys) . ') in ' . $filename);
  }
  if (!isset($parsed_info['core_version_requirement'])) {
    if (str_starts_with($filename, 'core/') || str_starts_with($filename, $this->root . '/core/')) {
      // Core extensions do not need to specify core compatibility: they are
      // by definition compatible so a sensible default is used. Core
      // modules are allowed to provide these for testing purposes.
      $parsed_info['core_version_requirement'] = \Drupal::VERSION;
    }
    elseif (isset($parsed_info['package']) && $parsed_info['package'] === 'Testing') {
      // Modules in the testing package are exempt as well. This makes it
      // easier for contrib to use test modules.
      $parsed_info['core_version_requirement'] = \Drupal::VERSION;
    }
    else {
      // Non-core extensions must specify core compatibility.
      throw new InfoParserException("The 'core_version_requirement' key must be present in " . $filename);
    }
  }
  // Determine if the extension is compatible with the current version of
  // Drupal core.
  try {
    $parsed_info['core_incompatible'] = !Semver::satisfies(\Drupal::VERSION, $parsed_info['core_version_requirement']);
  } catch (\UnexpectedValueException $exception) {
    throw new InfoParserException("The 'core_version_requirement' constraint ({$parsed_info['core_version_requirement']}) is not a valid value in {$filename}");
  }
  if (isset($parsed_info['version']) && $parsed_info['version'] === 'VERSION') {
    $parsed_info['version'] = \Drupal::VERSION;
  }
  $parsed_info += [
    ExtensionLifecycle::LIFECYCLE_IDENTIFIER => ExtensionLifecycle::STABLE,
  ];
  $lifecycle = $parsed_info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER];
  if (!ExtensionLifecycle::isValid($lifecycle)) {
    $valid_values = [
      ExtensionLifecycle::EXPERIMENTAL,
      ExtensionLifecycle::STABLE,
      ExtensionLifecycle::DEPRECATED,
      ExtensionLifecycle::OBSOLETE,
    ];
    throw new InfoParserException("'lifecycle: {$lifecycle}' is not valid in {$filename}. Valid values are: '" . implode("', '", $valid_values) . "'.");
  }
  if (in_array($lifecycle, [
    ExtensionLifecycle::DEPRECATED,
    ExtensionLifecycle::OBSOLETE,
  ], TRUE)) {
    if (empty($parsed_info[ExtensionLifecycle::LIFECYCLE_LINK_IDENTIFIER])) {
      throw new InfoParserException(sprintf("Extension %s (%s) has 'lifecycle: %s' but is missing a '%s' entry.", $parsed_info['name'], $filename, $lifecycle, ExtensionLifecycle::LIFECYCLE_LINK_IDENTIFIER));
    }
    if (!filter_var($parsed_info[ExtensionLifecycle::LIFECYCLE_LINK_IDENTIFIER], FILTER_VALIDATE_URL)) {
      throw new InfoParserException(sprintf("Extension %s (%s) has a '%s' entry that is not a valid URL.", $parsed_info['name'], $filename, ExtensionLifecycle::LIFECYCLE_LINK_IDENTIFIER));
    }
  }
  return $parsed_info;
}

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