BookUninstallValidator.php in Drupal 9
File
core/modules/book/src/BookUninstallValidator.php
View source
<?php
namespace Drupal\book;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
class BookUninstallValidator implements ModuleUninstallValidatorInterface {
use StringTranslationTrait;
protected $bookOutlineStorage;
protected $entityTypeManager;
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;
}
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 {
if ($this
->hasBookNodes()) {
$reasons[] = $this
->t('To uninstall Book, delete all content that has the Book content type');
}
}
}
return $reasons;
}
protected function hasBookOutlines() {
return $this->bookOutlineStorage
->hasBooks();
}
protected function hasBookNodes() {
$nodes = $this->entityTypeManager
->getStorage('node')
->getQuery()
->condition('type', 'book')
->accessCheck(FALSE)
->range(0, 1)
->execute();
return !empty($nodes);
}
}
Classes
Name |
Description |
BookUninstallValidator |
Prevents book module from being uninstalled whilst any book nodes exist or
there are any book outline stored. |