class BookUninstallValidator

Same name in this branch
  1. 10 core/modules/book/src/ProxyClass/BookUninstallValidator.php \Drupal\book\ProxyClass\BookUninstallValidator
Same name in other branches
  1. 9 core/modules/book/src/ProxyClass/BookUninstallValidator.php \Drupal\book\ProxyClass\BookUninstallValidator
  2. 9 core/modules/book/src/BookUninstallValidator.php \Drupal\book\BookUninstallValidator
  3. 8.9.x core/modules/book/src/ProxyClass/BookUninstallValidator.php \Drupal\book\ProxyClass\BookUninstallValidator
  4. 8.9.x core/modules/book/src/BookUninstallValidator.php \Drupal\book\BookUninstallValidator
  5. 11.x core/modules/book/src/ProxyClass/BookUninstallValidator.php \Drupal\book\ProxyClass\BookUninstallValidator
  6. 11.x core/modules/book/src/BookUninstallValidator.php \Drupal\book\BookUninstallValidator

Prevents book module from being uninstalled under certain conditions.

These conditions are when any book nodes exist or there are any book outline stored.

Hierarchy

Expanded class hierarchy of BookUninstallValidator

1 string reference to 'BookUninstallValidator'
book.services.yml in core/modules/book/book.services.yml
core/modules/book/book.services.yml
1 service uses BookUninstallValidator
book.uninstall_validator in core/modules/book/book.services.yml
Drupal\book\BookUninstallValidator

File

core/modules/book/src/BookUninstallValidator.php, line 16

Namespace

Drupal\book
View source
class BookUninstallValidator implements ModuleUninstallValidatorInterface {
    use StringTranslationTrait;
    
    /**
     * The book outline storage.
     *
     * @var \Drupal\book\BookOutlineStorageInterface
     */
    protected $bookOutlineStorage;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Constructs a new BookUninstallValidator.
     *
     * @param \Drupal\book\BookOutlineStorageInterface $book_outline_storage
     *   The book outline storage.
     * @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(BookOutlineStorageInterface $book_outline_storage, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
        $this->bookOutlineStorage = $book_outline_storage;
        $this->entityTypeManager = $entity_type_manager;
        $this->stringTranslation = $string_translation;
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate($module) {
        $reasons = [];
        if ($module == 'book') {
            if ($this->hasBookOutlines()) {
                $reasons[] = $this->t('To uninstall Book, delete all content that is part of a book');
            }
            else {
                // The book node type is provided by the Book module. Prevent uninstall
                // if there are any nodes of that type.
                if ($this->hasBookNodes()) {
                    $reasons[] = $this->t('To uninstall Book, delete all content that has the Book content type');
                }
            }
        }
        return $reasons;
    }
    
    /**
     * Checks if there are any books in an outline.
     *
     * @return bool
     *   TRUE if there are books, FALSE if not.
     */
    protected function hasBookOutlines() {
        return $this->bookOutlineStorage
            ->hasBooks();
    }
    
    /**
     * Determines if there is any book nodes or not.
     *
     * @return bool
     *   TRUE if there are book nodes, FALSE otherwise.
     */
    protected function hasBookNodes() {
        $nodes = $this->entityTypeManager
            ->getStorage('node')
            ->getQuery()
            ->condition('type', 'book')
            ->accessCheck(FALSE)
            ->range(0, 1)
            ->execute();
        return !empty($nodes);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
BookUninstallValidator::$bookOutlineStorage protected property The book outline storage.
BookUninstallValidator::$entityTypeManager protected property The entity type manager.
BookUninstallValidator::hasBookNodes protected function Determines if there is any book nodes or not.
BookUninstallValidator::hasBookOutlines protected function Checks if there are any books in an outline.
BookUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
BookUninstallValidator::__construct public function Constructs a new BookUninstallValidator.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.