You are here

class UninstallValidator in Lightning Media 8.4

Same name in this branch
  1. 8.4 modules/lightning_media_slideshow/src/UninstallValidator.php \Drupal\lightning_media_slideshow\UninstallValidator
  2. 8.4 modules/lightning_media_slideshow/src/ProxyClass/UninstallValidator.php \Drupal\lightning_media_slideshow\ProxyClass\UninstallValidator
Same name and namespace in other branches
  1. 8.3 modules/lightning_media_slideshow/src/UninstallValidator.php \Drupal\lightning_media_slideshow\UninstallValidator

Prevents this module from being uninstalled if any slideshow blocks exist.

Hierarchy

Expanded class hierarchy of UninstallValidator

1 string reference to 'UninstallValidator'
lightning_media_slideshow.services.yml in modules/lightning_media_slideshow/lightning_media_slideshow.services.yml
modules/lightning_media_slideshow/lightning_media_slideshow.services.yml
1 service uses UninstallValidator
lightning_media_slideshow.uninstall_validator in modules/lightning_media_slideshow/lightning_media_slideshow.services.yml
\Drupal\lightning_media_slideshow\UninstallValidator

File

modules/lightning_media_slideshow/src/UninstallValidator.php, line 13

Namespace

Drupal\lightning_media_slideshow
View source
class UninstallValidator implements ModuleUninstallValidatorInterface {
  use StringTranslationTrait;

  /**
   * The block content entity storage handler.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $blockContentStorage;

  /**
   * Constructs a new validator.
   *
   * @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->blockContentStorage = $entity_type_manager
      ->getStorage('block_content');
    $this
      ->setStringTranslation($string_translation);
  }

  /**
   * {@inheritdoc}
   */
  public function validate($module) {
    $problems = [];
    if ($module === 'lightning_media_slideshow' && $this
      ->hasSlideshowBlocks()) {
      $problems[] = $this
        ->t('To uninstall Media Slideshow, you must delete all slideshow blocks first.');
    }
    return $problems;
  }

  /**
   * Determines if any slideshow blocks exist.
   *
   * @return bool
   *   TRUE if there are slideshow blocks, FALSE otherwise.
   */
  protected function hasSlideshowBlocks() {
    $count = $this->blockContentStorage
      ->getQuery()
      ->condition('type', 'media_slideshow')
      ->accessCheck(FALSE)
      ->count()
      ->execute();
    return !empty($count);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
UninstallValidator::$blockContentStorage protected property The block content entity storage handler.
UninstallValidator::hasSlideshowBlocks protected function Determines if any slideshow blocks exist.
UninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
UninstallValidator::__construct public function Constructs a new validator.