ModuleDependencyMessageTrait.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Drupal\system;
  3. use Drupal\Core\Extension\Dependency;
  4. /**
  5. * Messages for missing or incompatible dependencies on modules.
  6. *
  7. * @internal The trait simply helps core classes that display user messages
  8. * regarding missing or incompatible module dependencies share exact same
  9. * wording and markup.
  10. */
  11. trait ModuleDependencyMessageTrait {
  12. /**
  13. * Provides messages for missing modules or incompatible dependencies.
  14. *
  15. * @param array $modules
  16. * The list of existing modules.
  17. * @param string $dependency
  18. * The module dependency to check.
  19. * @param \Drupal\Core\Extension\Dependency $dependency_object
  20. * Dependency object used for comparing version requirement data.
  21. *
  22. * @return string|null
  23. * NULL if compatible, otherwise a string describing the incompatibility.
  24. */
  25. public function checkDependencyMessage(array $modules, $dependency, Dependency $dependency_object) {
  26. if (!isset($modules[$dependency])) {
  27. return $this->t('@module_name (<span class="admin-missing">missing</span>)', ['@module_name' => $dependency]);
  28. }
  29. else {
  30. $module_name = $modules[$dependency]->info['name'];
  31. // Check if the module is compatible with the installed version of core.
  32. if ($modules[$dependency]->info['core_incompatible']) {
  33. return $this->t('@module_name (<span class="admin-missing">incompatible with</span> this version of Drupal core)', [
  34. '@module_name' => $module_name,
  35. ]);
  36. }
  37. // Check if the module is incompatible with the dependency constraints.
  38. $version = str_replace(\Drupal::CORE_COMPATIBILITY . '-', '', $modules[$dependency]->info['version']);
  39. if (!$dependency_object->isCompatible($version)) {
  40. $constraint_string = $dependency_object->getConstraintString();
  41. return $this->t('@module_name (<span class="admin-missing">incompatible with</span> version @version)', [
  42. '@module_name' => "$module_name ($constraint_string)",
  43. '@version' => $modules[$dependency]->info['version'],
  44. ]);
  45. }
  46. }
  47. }
  48. }