You are here

public function DepthUninstallValidator::validate in Taxonomy Term Depth 8.2

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

Determines the reasons a module can not be uninstalled.

Parameters

string $module: A module name.

Return value

string[] An array of reasons the module can not be uninstalled, empty if it can. Each reason should not end with any punctuation since multiple reasons can be displayed together.

Overrides ModuleUninstallValidatorInterface::validate

See also

template_preprocess_system_modules_uninstall()

File

src/DepthUninstallValidator.php, line 50

Class

DepthUninstallValidator
Prevents uninstallation of modules providing active field storage.

Namespace

Drupal\taxonomy_term_depth

Code

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;
}