You are here

class GroupContentUninstallValidator in Group 8

Same name in this branch
  1. 8 src/UninstallValidator/GroupContentUninstallValidator.php \Drupal\group\UninstallValidator\GroupContentUninstallValidator
  2. 8 src/ProxyClass/UninstallValidator/GroupContentUninstallValidator.php \Drupal\group\ProxyClass\UninstallValidator\GroupContentUninstallValidator
Same name and namespace in other branches
  1. 2.0.x src/UninstallValidator/GroupContentUninstallValidator.php \Drupal\group\UninstallValidator\GroupContentUninstallValidator

Hierarchy

Expanded class hierarchy of GroupContentUninstallValidator

1 string reference to 'GroupContentUninstallValidator'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses GroupContentUninstallValidator
group.uninstall_validator.group_content in ./group.services.yml
Drupal\group\UninstallValidator\GroupContentUninstallValidator

File

src/UninstallValidator/GroupContentUninstallValidator.php, line 12

Namespace

Drupal\group\UninstallValidator
View source
class GroupContentUninstallValidator implements ModuleUninstallValidatorInterface {
  use StringTranslationTrait;

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

  /**
   * The group content plugin manager.
   *
   * @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface
   */
  protected $pluginManager;

  /**
   * Constructs a new GroupContentUninstallValidator object.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\group\Plugin\GroupContentEnablerManagerInterface $plugin_manager
   *   The group content plugin manager.
   */
  public function __construct(TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager, GroupContentEnablerManagerInterface $plugin_manager) {
    $this->stringTranslation = $string_translation;
    $this->entityTypeManager = $entity_type_manager;
    $this->pluginManager = $plugin_manager;
  }

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

    /** @var \Drupal\group\Plugin\GroupContentEnablerInterface $plugin */
    foreach ($this->pluginManager
      ->getAll() as $plugin_id => $plugin) {
      if ($plugin
        ->getProvider() == $module && $this
        ->hasGroupContent($plugin_id)) {
        $plugin_names[] = $plugin
          ->getLabel();
      }
    }
    if (!empty($plugin_names)) {
      $reasons[] = $this
        ->t('The following group content plugins still have content for them: %plugins.', [
        '%plugins' => implode(', ', $plugin_names),
      ]);
    }
    return $reasons;
  }

  /**
   * Determines if there is any group content for a content enabler plugin.
   *
   * @param string $plugin_id
   *   The group content enabler plugin ID to check for group content.
   *
   * @return bool
   *   Whether there are group content entities for the given plugin ID.
   */
  protected function hasGroupContent($plugin_id) {
    $group_content_types = array_keys(GroupContentType::loadByContentPluginId($plugin_id));
    if (empty($group_content_types)) {
      return FALSE;
    }
    $entity_count = $this->entityTypeManager
      ->getStorage('group_content')
      ->getQuery()
      ->accessCheck(FALSE)
      ->condition('type', $group_content_types, 'IN')
      ->count()
      ->execute();
    return (bool) $entity_count;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupContentUninstallValidator::$entityTypeManager protected property The entity type manager.
GroupContentUninstallValidator::$pluginManager protected property The group content plugin manager.
GroupContentUninstallValidator::hasGroupContent protected function Determines if there is any group content for a content enabler plugin.
GroupContentUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
GroupContentUninstallValidator::__construct public function Constructs a new GroupContentUninstallValidator object.
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.