You are here

class DepthUninstallValidator in Taxonomy Term Depth 8.2

Same name in this branch
  1. 8.2 src/DepthUninstallValidator.php \Drupal\taxonomy_term_depth\DepthUninstallValidator
  2. 8.2 src/ProxyClass/DepthUninstallValidator.php \Drupal\taxonomy_term_depth\ProxyClass\DepthUninstallValidator

Prevents uninstallation of modules providing active field storage.

Hierarchy

Expanded class hierarchy of DepthUninstallValidator

1 string reference to 'DepthUninstallValidator'
taxonomy_term_depth.services.yml in ./taxonomy_term_depth.services.yml
taxonomy_term_depth.services.yml
1 service uses DepthUninstallValidator
taxonomy_term_depth.uninstall_validator in ./taxonomy_term_depth.services.yml
Drupal\taxonomy_term_depth\DepthUninstallValidator

File

src/DepthUninstallValidator.php, line 16

Namespace

Drupal\taxonomy_term_depth
View source
class DepthUninstallValidator implements ModuleUninstallValidatorInterface {
  use StringTranslationTrait;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * DepthUninstallValidator constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   */
  public function __construct(EntityTypeManagerInterface $entity_manager, EntityFieldManagerInterface $entity_field_manager, TranslationInterface $string_translation) {
    $this->entityTypeManager = $entity_manager;
    $this->entityFieldManager = $entity_field_manager;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function validate($module_name) {
    $reasons = [];

    // We skip fields provided by the Field module as it implements field
    // purging.
    if ($module_name != 'field') {
      foreach ($this->entityTypeManager
        ->getDefinitions() as $entity_type_id => $entity_type) {

        // We skip entity types defined by the module as there must be no
        // content to be able to uninstall them anyway.
        // See \Drupal\Core\Entity\ContentUninstallValidator.
        if ($entity_type
          ->getProvider() != $module_name && $entity_type
          ->entityClassImplements('\\Drupal\\Core\\Entity\\FieldableEntityInterface')) {
          foreach ($this->entityFieldManager
            ->getFieldStorageDefinitions($entity_type_id) as $storage_definition) {
            if ($storage_definition
              ->getProvider() == $module_name) {
              $storage = $this->entityTypeManager
                ->getStorage($entity_type_id);
              if ($storage instanceof FieldableEntityStorageInterface && $storage
                ->countFieldData($storage_definition, TRUE)) {
                $reasons[] = $this
                  ->t('There is data for the field @field-name on entity type @entity_type. <a href=":url">Delete depth fields data.</a>.', [
                  '@field-name' => $storage_definition
                    ->getName(),
                  '@entity_type' => $entity_type
                    ->getLabel(),
                  ':url' => Url::fromRoute('taxonomy_term_depth.prepare_modules_uninstall')
                    ->toString(),
                ]);
              }
            }
          }
        }
      }
    }
    return $reasons;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DepthUninstallValidator::$entityFieldManager protected property
DepthUninstallValidator::$entityTypeManager protected property
DepthUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
DepthUninstallValidator::__construct public function DepthUninstallValidator constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.