function InstalledPackagesList::getPackageByDrupalProjectName

Returns the package for a given Drupal project name, if it is installed.

Although it is common for the package name to match the project name (for example, a project name of `token` is likely part of the `drupal/token` package), it's not guaranteed. Therefore, in order to avoid inadvertently reading information about the wrong package, use this method to properly determine which package installs a particular Drupal project.

Parameters

string $project_name: The name of a Drupal project.

Return value

\Drupal\package_manager\InstalledPackage|null The Composer package which installs the project, or NULL if it could not be determined.

File

core/modules/package_manager/src/InstalledPackagesList.php, line 112

Class

InstalledPackagesList
Defines a class to list installed Composer packages.

Namespace

Drupal\package_manager

Code

public function getPackageByDrupalProjectName(string $project_name) : ?InstalledPackage {
    $matching_package = NULL;
    foreach ($this as $package) {
        if ($package->getProjectName() === $project_name) {
            if ($matching_package) {
                throw new \UnexpectedValueException(sprintf("Project '%s' was found in packages '%s' and '%s'.", $project_name, $matching_package->name, $package->name));
            }
            $matching_package = $package;
        }
    }
    return $matching_package;
}

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