You are here

class WebformNodeUninstallValidator in Webform 8.5

Same name in this branch
  1. 8.5 modules/webform_node/src/WebformNodeUninstallValidator.php \Drupal\webform_node\WebformNodeUninstallValidator
  2. 8.5 modules/webform_node/src/ProxyClass/WebformNodeUninstallValidator.php \Drupal\webform_node\ProxyClass\WebformNodeUninstallValidator
Same name and namespace in other branches
  1. 6.x modules/webform_node/src/WebformNodeUninstallValidator.php \Drupal\webform_node\WebformNodeUninstallValidator

Prevents webform_node module from being uninstalled whilst any webform nodes exist.

Hierarchy

Expanded class hierarchy of WebformNodeUninstallValidator

1 string reference to 'WebformNodeUninstallValidator'
webform_node.services.yml in modules/webform_node/webform_node.services.yml
modules/webform_node/webform_node.services.yml
1 service uses WebformNodeUninstallValidator
webform_node.uninstall_validator in modules/webform_node/webform_node.services.yml
Drupal\webform_node\WebformNodeUninstallValidator

File

modules/webform_node/src/WebformNodeUninstallValidator.php, line 13

Namespace

Drupal\webform_node
View source
class WebformNodeUninstallValidator implements ModuleUninstallValidatorInterface {
  use StringTranslationTrait;

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

  /**
   * Constructs a WebformNodeUninstallValidator.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function validate($module) {
    $reasons = [];
    if ($module === 'webform_node') {

      // The webform node type is provided by the Webform node module. Prevent
      // uninstall if there are any nodes of that type.
      if ($this
        ->hasWebformNodes()) {
        $reasons[] = $this
          ->t('To uninstall Webform node, delete all content that has the Webform content type.');
      }
    }
    return $reasons;
  }

  /**
   * Determines if there is any webform nodes or not.
   *
   * @return bool
   *   TRUE if there are webform nodes, FALSE otherwise.
   */
  protected function hasWebformNodes() {
    $nodes = $this->entityTypeManager
      ->getStorage('node')
      ->getQuery()
      ->condition('type', 'webform')
      ->accessCheck(FALSE)
      ->range(0, 1)
      ->execute();
    return !empty($nodes);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.
WebformNodeUninstallValidator::$entityTypeManager protected property The entity type manager.
WebformNodeUninstallValidator::hasWebformNodes protected function Determines if there is any webform nodes or not.
WebformNodeUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
WebformNodeUninstallValidator::__construct public function Constructs a WebformNodeUninstallValidator.