You are here

class ForumUninstallValidator in Drupal 9

Same name in this branch
  1. 9 core/modules/forum/src/ForumUninstallValidator.php \Drupal\forum\ForumUninstallValidator
  2. 9 core/modules/forum/src/ProxyClass/ForumUninstallValidator.php \Drupal\forum\ProxyClass\ForumUninstallValidator
Same name and namespace in other branches
  1. 8 core/modules/forum/src/ForumUninstallValidator.php \Drupal\forum\ForumUninstallValidator

Prevents forum module from being uninstalled whilst any forum nodes exist or there are any terms in the forum vocabulary.

Hierarchy

Expanded class hierarchy of ForumUninstallValidator

1 string reference to 'ForumUninstallValidator'
forum.services.yml in core/modules/forum/forum.services.yml
core/modules/forum/forum.services.yml
1 service uses ForumUninstallValidator
forum.uninstall_validator in core/modules/forum/forum.services.yml
Drupal\forum\ForumUninstallValidator

File

core/modules/forum/src/ForumUninstallValidator.php, line 16

Namespace

Drupal\forum
View source
class ForumUninstallValidator implements ModuleUninstallValidatorInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new ForumUninstallValidator.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation) {
    $this->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 <em>Forum</em> content');
      }
      $vocabulary = $this
        ->getForumVocabulary();
      if ($this
        ->hasTermsForVocabulary($vocabulary)) {
        if ($vocabulary
          ->access('view')) {
          $reasons[] = $this
            ->t('To uninstall Forum, first delete all <a href=":url">%vocabulary</a> 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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ForumUninstallValidator::$configFactory protected property The config factory.
ForumUninstallValidator::$entityTypeManager protected property The entity type manager.
ForumUninstallValidator::getForumVocabulary protected function Returns the vocabulary configured for forums.
ForumUninstallValidator::hasForumNodes protected function Determines if there are any forum nodes or not.
ForumUninstallValidator::hasTermsForVocabulary protected function Determines if there are any taxonomy terms for a specified vocabulary.
ForumUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
ForumUninstallValidator::__construct public function Constructs a new ForumUninstallValidator.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.