entityTypeManager = $entity_type_manager; $this->configFactory = $config_factory; $this->stringTranslation = $string_translation; } /** * {@inheritdoc} */ public function validate($module) { $reasons = []; if ($module == 'forum') { if ($this->hasForumNodes()) { $reasons[] = $this->t('To uninstall Forum, first delete all Forum content'); } $vocabulary = $this->getForumVocabulary(); if ($this->hasTermsForVocabulary($vocabulary)) { if ($vocabulary->access('view')) { $reasons[] = $this->t('To uninstall Forum, first delete all %vocabulary terms', [ '%vocabulary' => $vocabulary->label(), ':url' => $vocabulary->toUrl('overview-form')->toString(), ]); } else { $reasons[] = $this->t('To uninstall Forum, first delete all %vocabulary terms', [ '%vocabulary' => $vocabulary->label(), ]); } } } return $reasons; } /** * Determines if there are any forum nodes or not. * * @return bool * TRUE if there are forum nodes, FALSE otherwise. */ protected function hasForumNodes() { $nodes = $this->entityTypeManager->getStorage('node')->getQuery() ->condition('type', 'forum') ->accessCheck(FALSE) ->range(0, 1) ->execute(); return !empty($nodes); } /** * Determines if there are any taxonomy terms for a specified vocabulary. * * @param \Drupal\taxonomy\VocabularyInterface $vocabulary * The vocabulary to check for terms. * * @return bool * TRUE if there are terms for this vocabulary, FALSE otherwise. */ protected function hasTermsForVocabulary(VocabularyInterface $vocabulary) { $terms = $this->entityTypeManager->getStorage('taxonomy_term')->getQuery() ->condition('vid', $vocabulary->id()) ->accessCheck(FALSE) ->range(0, 1) ->execute(); return !empty($terms); } /** * Returns the vocabulary configured for forums. * * @return \Drupal\taxonomy\VocabularyInterface * The vocabulary entity for forums. */ protected function getForumVocabulary() { $vid = $this->configFactory->get('forum.settings')->get('vocabulary'); return $this->entityTypeManager->getStorage('taxonomy_vocabulary')->load($vid); } }