annotate core/modules/book/src/BookUninstallValidator.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\book;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 6 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
Chris@0 7 use Drupal\Core\StringTranslation\StringTranslationTrait;
Chris@0 8 use Drupal\Core\StringTranslation\TranslationInterface;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Prevents book module from being uninstalled whilst any book nodes exist or
Chris@0 12 * there are any book outline stored.
Chris@0 13 */
Chris@0 14 class BookUninstallValidator implements ModuleUninstallValidatorInterface {
Chris@0 15
Chris@0 16 use StringTranslationTrait;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * The book outline storage.
Chris@0 20 *
Chris@0 21 * @var \Drupal\book\BookOutlineStorageInterface
Chris@0 22 */
Chris@0 23 protected $bookOutlineStorage;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The entity type manager.
Chris@0 27 *
Chris@0 28 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 29 */
Chris@0 30 protected $entityTypeManager;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Constructs a new BookUninstallValidator.
Chris@0 34 *
Chris@0 35 * @param \Drupal\book\BookOutlineStorageInterface $book_outline_storage
Chris@0 36 * The book outline storage.
Chris@0 37 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@0 38 * The entity type manager.
Chris@0 39 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
Chris@0 40 * The string translation service.
Chris@0 41 */
Chris@0 42 public function __construct(BookOutlineStorageInterface $book_outline_storage, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
Chris@0 43 $this->bookOutlineStorage = $book_outline_storage;
Chris@0 44 $this->entityTypeManager = $entity_type_manager;
Chris@0 45 $this->stringTranslation = $string_translation;
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@0 49 * {@inheritdoc}
Chris@0 50 */
Chris@0 51 public function validate($module) {
Chris@0 52 $reasons = [];
Chris@0 53 if ($module == 'book') {
Chris@0 54 if ($this->hasBookOutlines()) {
Chris@0 55 $reasons[] = $this->t('To uninstall Book, delete all content that is part of a book');
Chris@0 56 }
Chris@0 57 else {
Chris@0 58 // The book node type is provided by the Book module. Prevent uninstall
Chris@0 59 // if there are any nodes of that type.
Chris@0 60 if ($this->hasBookNodes()) {
Chris@0 61 $reasons[] = $this->t('To uninstall Book, delete all content that has the Book content type');
Chris@0 62 }
Chris@0 63 }
Chris@0 64 }
Chris@0 65 return $reasons;
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * Checks if there are any books in an outline.
Chris@0 70 *
Chris@0 71 * @return bool
Chris@0 72 * TRUE if there are books, FALSE if not.
Chris@0 73 */
Chris@0 74 protected function hasBookOutlines() {
Chris@0 75 return $this->bookOutlineStorage->hasBooks();
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * Determines if there is any book nodes or not.
Chris@0 80 *
Chris@0 81 * @return bool
Chris@0 82 * TRUE if there are book nodes, FALSE otherwise.
Chris@0 83 */
Chris@0 84 protected function hasBookNodes() {
Chris@0 85 $nodes = $this->entityTypeManager->getStorage('node')->getQuery()
Chris@0 86 ->condition('type', 'book')
Chris@0 87 ->accessCheck(FALSE)
Chris@0 88 ->range(0, 1)
Chris@0 89 ->execute();
Chris@0 90 return !empty($nodes);
Chris@0 91 }
Chris@0 92
Chris@0 93 }