face3cbf459c363d8b3be1cae276f53c11ee81ac
[yaffs-website] / web / core / modules / forum / src / ForumUninstallValidator.php
1 <?php
2
3 namespace Drupal\forum;
4
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;
11
12 /**
13  * Prevents forum module from being uninstalled whilst any forum nodes exist
14  * or there are any terms in the forum vocabulary.
15  */
16 class ForumUninstallValidator implements ModuleUninstallValidatorInterface {
17
18   use StringTranslationTrait;
19
20   /**
21    * The entity type manager.
22    *
23    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24    */
25   protected $entityTypeManager;
26
27   /**
28    * The config factory.
29    *
30    * @var \Drupal\Core\Config\ConfigFactoryInterface
31    */
32   protected $configFactory;
33
34   /**
35    * Constructs a new ForumUninstallValidator.
36    *
37    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
38    *   The entity type manager.
39    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
40    *   The config factory.
41    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
42    *   The string translation service.
43    */
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;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function validate($module) {
54     $reasons = [];
55     if ($module == 'forum') {
56       if ($this->hasForumNodes()) {
57         $reasons[] = $this->t('To uninstall Forum, first delete all <em>Forum</em> content');
58       }
59
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(),
66           ]);
67         }
68         else {
69           $reasons[] = $this->t('To uninstall Forum, first delete all %vocabulary terms', [
70             '%vocabulary' => $vocabulary->label(),
71           ]);
72         }
73       }
74     }
75
76     return $reasons;
77   }
78
79   /**
80    * Determines if there are any forum nodes or not.
81    *
82    * @return bool
83    *   TRUE if there are forum nodes, FALSE otherwise.
84    */
85   protected function hasForumNodes() {
86     $nodes = $this->entityTypeManager->getStorage('node')->getQuery()
87       ->condition('type', 'forum')
88       ->accessCheck(FALSE)
89       ->range(0, 1)
90       ->execute();
91     return !empty($nodes);
92   }
93
94   /**
95    * Determines if there are any taxonomy terms for a specified vocabulary.
96    *
97    * @param \Drupal\taxonomy\VocabularyInterface $vocabulary
98    *   The vocabulary to check for terms.
99    *
100    * @return bool
101    *   TRUE if there are terms for this vocabulary, FALSE otherwise.
102    */
103   protected function hasTermsForVocabulary(VocabularyInterface $vocabulary) {
104     $terms = $this->entityTypeManager->getStorage('taxonomy_term')->getQuery()
105       ->condition('vid', $vocabulary->id())
106       ->accessCheck(FALSE)
107       ->range(0, 1)
108       ->execute();
109     return !empty($terms);
110   }
111
112   /**
113    * Returns the vocabulary configured for forums.
114    *
115    * @return \Drupal\taxonomy\VocabularyInterface
116    *   The vocabulary entity for forums.
117    */
118   protected function getForumVocabulary() {
119     $vid = $this->configFactory->get('forum.settings')->get('vocabulary');
120     return $this->entityTypeManager->getStorage('taxonomy_vocabulary')->load($vid);
121   }
122
123 }