You are here

public function BundlePluginUninstallValidator::validate in Entity API 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

src/BundlePlugin/BundlePluginUninstallValidator.php, line 40

Class

BundlePluginUninstallValidator
Prevents uninstalling modules with bundle plugins in case of found data.

Namespace

Drupal\entity\BundlePlugin

Code

public function validate($module) {
  $reasons = [];
  foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {

    /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
    $bundle_handler = $this->entityTypeManager
      ->getHandler($entity_type
      ->id(), 'bundle_plugin');
    $bundles = $bundle_handler
      ->getBundleInfo();

    // We find all bundles which have to be removed due to the uninstallation.
    $bundles_filtered_by_module = array_filter($bundles, function ($bundle_info) use ($module) {
      return $module === $bundle_info['provider'];
    });
    if (!empty($bundles_filtered_by_module)) {
      $bundle_keys_with_content = array_filter(array_keys($bundles_filtered_by_module), function ($bundle) use ($entity_type) {
        $result = $this->entityTypeManager
          ->getStorage($entity_type
          ->id())
          ->getQuery()
          ->condition($entity_type
          ->getKey('bundle'), $bundle)
          ->range(0, 1)
          ->execute();
        return !empty($result);
      });
      $bundles_with_content = array_intersect_key($bundles_filtered_by_module, array_flip($bundle_keys_with_content));
      foreach ($bundles_with_content as $bundle) {
        $reasons[] = $this
          ->t('There is data for the bundle @bundle on the entity type @entity_type. Please remove all content before uninstalling the module.', [
          '@bundle' => $bundle['label'],
          '@entity_type' => $entity_type
            ->getLabel(),
        ]);
      }
    }
  }
  return $reasons;
}