Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
CRAP
55.56% covered (warning)
55.56%
5 / 9
RequiredModuleUninstallValidator
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
7.19
55.56% covered (warning)
55.56%
5 / 9
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 validate
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 getModuleInfoByModule
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 2
<?php
/**
 * @file
 * Contains \Drupal\Core\Extension\RequiredModuleUninstallValidator.
 */
namespace Drupal\Core\Extension;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
/**
 * Ensures that required modules cannot be uninstalled.
 */
class RequiredModuleUninstallValidator implements ModuleUninstallValidatorInterface {
  use StringTranslationTrait;
  /**
   * Constructs a new RequiredModuleUninstallValidator.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(TranslationInterface $string_translation) {
    $this->stringTranslation = $string_translation;
  }
  /**
   * {@inheritdoc}
   */
  public function validate($module) {
    $reasons = [];
    $module_info = $this->getModuleInfoByModule($module);
    if (!empty($module_info['required'])) {
      $reasons[] = $this->t('The @module module is required', ['@module' => $module_info['name']]);
    }
    return $reasons;
  }
  /**
   * Returns the module info for a specific module.
   *
   * @param string $module
   *   The name of the module.
   *
   * @return array
   *   The module info, or NULL if that module does not exist.
   */
  protected function getModuleInfoByModule($module) {
    $modules = system_rebuild_module_data();
    return isset($modules[$module]->info) ? $modules[$module]->info : [];
  }
}