You are here

public function LocalStockUninstallValidator::validate in Commerce Stock 8

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

modules/local_storage/src/LocalStockUninstallValidator.php, line 54

Class

LocalStockUninstallValidator
Provides link to delete commerce_stock_local field values.

Namespace

Drupal\commerce_stock_local

Code

public function validate($module) {
  if ($module !== 'commerce_stock_local') {
    return [];
  }
  $entity_types = $this->entityTypeManager
    ->getDefinitions();
  $reasons = [];
  foreach ($entity_types as $entity_type_id => $entity_type) {
    if ($entity_type
      ->entityClassImplements('\\Drupal\\commerce\\PurchasableEntityInterface')) {
      $storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
      if ($storage instanceof SqlContentEntityStorage) {
        foreach ($this->entityFieldManager
          ->getActiveFieldStorageDefinitions($entity_type_id) as $storage_definition) {
          if ($storage_definition
            ->getProvider() == $module && $storage instanceof FieldableEntityStorageInterface && $storage
            ->countFieldData($storage_definition, TRUE)) {
            $reasons[] = $this
              ->t('<a href=":url">Remove field values</a>: @field-name on entity type @entity_type.', [
              '@field-name' => $storage_definition
                ->getName(),
              '@entity_type' => $entity_type
                ->getLabel(),
              ':url' => Url::fromRoute('commerce_stock_local.prepare_module_uninstall')
                ->toString(),
            ]);
          }
        }
      }
    }
  }
  return $reasons;
}