3 namespace Drupal\forum;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\Core\StringTranslation\TranslationInterface;
10 use Drupal\taxonomy\VocabularyInterface;
13 * Prevents forum module from being uninstalled whilst any forum nodes exist
14 * or there are any terms in the forum vocabulary.
16 class ForumUninstallValidator implements ModuleUninstallValidatorInterface {
18 use StringTranslationTrait;
21 * The entity type manager.
23 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
25 protected $entityTypeManager;
30 * @var \Drupal\Core\Config\ConfigFactoryInterface
32 protected $configFactory;
35 * Constructs a new ForumUninstallValidator.
37 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
38 * The entity type manager.
39 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
41 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
42 * The string translation service.
44 public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation) {
45 $this->entityTypeManager = $entity_type_manager;
46 $this->configFactory = $config_factory;
47 $this->stringTranslation = $string_translation;
53 public function validate($module) {
55 if ($module == 'forum') {
56 if ($this->hasForumNodes()) {
57 $reasons[] = $this->t('To uninstall Forum, first delete all <em>Forum</em> content');
60 $vocabulary = $this->getForumVocabulary();
61 if ($this->hasTermsForVocabulary($vocabulary)) {
62 if ($vocabulary->access('view')) {
63 $reasons[] = $this->t('To uninstall Forum, first delete all <a href=":url">%vocabulary</a> terms', [
64 '%vocabulary' => $vocabulary->label(),
65 ':url' => $vocabulary->toUrl('overview-form')->toString(),
69 $reasons[] = $this->t('To uninstall Forum, first delete all %vocabulary terms', [
70 '%vocabulary' => $vocabulary->label(),
80 * Determines if there are any forum nodes or not.
83 * TRUE if there are forum nodes, FALSE otherwise.
85 protected function hasForumNodes() {
86 $nodes = $this->entityTypeManager->getStorage('node')->getQuery()
87 ->condition('type', 'forum')
91 return !empty($nodes);
95 * Determines if there are any taxonomy terms for a specified vocabulary.
97 * @param \Drupal\taxonomy\VocabularyInterface $vocabulary
98 * The vocabulary to check for terms.
101 * TRUE if there are terms for this vocabulary, FALSE otherwise.
103 protected function hasTermsForVocabulary(VocabularyInterface $vocabulary) {
104 $terms = $this->entityTypeManager->getStorage('taxonomy_term')->getQuery()
105 ->condition('vid', $vocabulary->id())
109 return !empty($terms);
113 * Returns the vocabulary configured for forums.
115 * @return \Drupal\taxonomy\VocabularyInterface
116 * The vocabulary entity for forums.
118 protected function getForumVocabulary() {
119 $vid = $this->configFactory->get('forum.settings')->get('vocabulary');
120 return $this->entityTypeManager->getStorage('taxonomy_vocabulary')->load($vid);