CmsBlogUninstallValidator.php in Glazed CMS Blog 8
File
src/CmsBlogUninstallValidator.php
View source
<?php
namespace Drupal\cms_blog;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
class CmsBlogUninstallValidator implements ModuleUninstallValidatorInterface {
protected $queryFactory;
public function __construct(QueryFactory $query_factory) {
$this->queryFactory = $query_factory;
}
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;
}
protected function hasBlogNodes() {
$nodes = $this->queryFactory
->get('node')
->condition('type', 'cms_blog')
->accessCheck(FALSE)
->range(0, 1)
->execute();
return !empty($nodes);
}
protected function hasTerms($vid) {
$terms = $this->queryFactory
->get('taxonomy_term')
->condition('vid', $vid)
->accessCheck(FALSE)
->range(0, 1)
->execute();
return !empty($terms);
}
}