You are here

class BundlePluginUninstallValidator in Entity API 8

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

Hierarchy

Expanded class hierarchy of BundlePluginUninstallValidator

1 string reference to 'BundlePluginUninstallValidator'
entity.services.yml in ./entity.services.yml
entity.services.yml
1 service uses BundlePluginUninstallValidator
entity.bundle_plugin.uninstall_validator in ./entity.services.yml
Drupal\entity\BundlePlugin\BundlePluginUninstallValidator

File

src/BundlePlugin/BundlePluginUninstallValidator.php, line 13

Namespace

Drupal\entity\BundlePlugin
View source
class BundlePluginUninstallValidator implements ModuleUninstallValidatorInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs the object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BundlePluginUninstallValidator::$entityTypeManager protected property The entity type manager.
BundlePluginUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
BundlePluginUninstallValidator::__construct public function Constructs the object.
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.