summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/Extension/ModuleDependencyMessageTrait.php
blob: 6fef0d9a6a7c47ecfdef14eb9a6888436d87f238 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

namespace Drupal\Core\Extension;

/**
 * Messages for missing or incompatible dependencies on modules.
 *
 * @internal The trait simply helps core classes that display user messages
 *   regarding missing or incompatible module dependencies share exact same
 *   wording and markup.
 */
trait ModuleDependencyMessageTrait {

  /**
   * Provides messages for missing modules or incompatible dependencies.
   *
   * @param array $modules
   *   The list of existing modules.
   * @param string $dependency
   *   The module dependency to check.
   * @param \Drupal\Core\Extension\Dependency $dependency_object
   *   Dependency object used for comparing version requirement data.
   *
   * @return string|null
   *   NULL if compatible, otherwise a string describing the incompatibility.
   */
  public function checkDependencyMessage(array $modules, $dependency, Dependency $dependency_object) {
    if (!isset($modules[$dependency])) {
      return $this->t('@module_name (<span class="admin-missing">missing</span>)', ['@module_name' => $dependency]);
    }
    else {
      $module_name = $modules[$dependency]->info['name'];

      // Check if the module is compatible with the installed version of core.
      if ($modules[$dependency]->info['core_incompatible']) {
        return $this->t('@module_name (<span class="admin-missing">incompatible with</span> this version of Drupal core)', [
          '@module_name' => $module_name,
        ]);
      }

      // Check if the module is incompatible with the dependency constraints.
      // Remove CORE_COMPATIBILITY- only from the start of the string.
      $version = preg_replace('/^(' . \Drupal::CORE_COMPATIBILITY . '\-)/', '', $modules[$dependency]->info['version'] ?? '');
      if (!$dependency_object->isCompatible($version)) {
        $constraint_string = $dependency_object->getConstraintString();
        return $this->t('@module_name (<span class="admin-missing">incompatible with</span> version @version)', [
          '@module_name' => "$module_name ($constraint_string)",
          '@version' => $modules[$dependency]->info['version'] ?? '* ? *',
        ]);
      }
    }

    return NULL;
  }

}