You are here

class CmsBlogUninstallValidator in Glazed CMS Blog 8

Same name in this branch
  1. 8 src/CmsBlogUninstallValidator.php \Drupal\cms_blog\CmsBlogUninstallValidator
  2. 8 src/ProxyClass/CmsBlogUninstallValidator.php \Drupal\cms_blog\ProxyClass\CmsBlogUninstallValidator

Prevents CMS Blog module from being uninstalled if any blog entries exist.

Hierarchy

Expanded class hierarchy of CmsBlogUninstallValidator

1 string reference to 'CmsBlogUninstallValidator'
cms_blog.services.yml in ./cms_blog.services.yml
cms_blog.services.yml
1 service uses CmsBlogUninstallValidator
cms_blog.uninstall_validator in ./cms_blog.services.yml
Drupal\cms_blog\CmsBlogUninstallValidator

File

src/CmsBlogUninstallValidator.php, line 11

Namespace

Drupal\cms_blog
View source
class CmsBlogUninstallValidator implements ModuleUninstallValidatorInterface {

  /**
   * The entity query factory.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $queryFactory;

  /**
   * Constructs a new CmsBlogUninstallValidator.
   *
   * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
   *   The entity query factory.
   */
  public function __construct(QueryFactory $query_factory) {
    $this->queryFactory = $query_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function validate($module) {
    $reasons = [];
    if ($module == 'cms_blog') {
      if ($this
        ->hasBlogNodes()) {
        $reasons[] = t('To uninstall CMS Blog module, first delete all <em>Blog</em> content');
      }
      if ($this
        ->hasTerms('cms_blog_category')) {
        $reasons[] = t('To uninstall CMS Blog module, first delete all terms from Blog category vocabulary.');
      }
      if ($this
        ->hasTerms('cms_blog_tags')) {
        $reasons[] = t('To uninstall CMS Blog module, first delete all terms from Blog tags vocabulary.');
      }
    }
    return $reasons;
  }

  /**
   * Determines if there is any CMS Blog nodes or not.
   *
   * @return bool
   *   TRUE if there are blog nodes, FALSE otherwise.
   */
  protected function hasBlogNodes() {
    $nodes = $this->queryFactory
      ->get('node')
      ->condition('type', 'cms_blog')
      ->accessCheck(FALSE)
      ->range(0, 1)
      ->execute();
    return !empty($nodes);
  }

  /**
   * Determines if there are any taxonomy terms for a specified vocabulary.
   *
   * @param int $vid
   *   The ID of vocabulary to check for terms.
   *
   * @return bool
   *   TRUE if there are terms for this vocabulary, FALSE otherwise.
   */
  protected function hasTerms($vid) {
    $terms = $this->queryFactory
      ->get('taxonomy_term')
      ->condition('vid', $vid)
      ->accessCheck(FALSE)
      ->range(0, 1)
      ->execute();
    return !empty($terms);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CmsBlogUninstallValidator::$queryFactory protected property The entity query factory.
CmsBlogUninstallValidator::hasBlogNodes protected function Determines if there is any CMS Blog nodes or not.
CmsBlogUninstallValidator::hasTerms protected function Determines if there are any taxonomy terms for a specified vocabulary.
CmsBlogUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
CmsBlogUninstallValidator::__construct public function Constructs a new CmsBlogUninstallValidator.