You are here

abstract class OgDeleteOrphansBase in Organic groups 8

Base implementation for OgDeleteOrphans plugins.

Hierarchy

Expanded class hierarchy of OgDeleteOrphansBase

3 files declare their use of OgDeleteOrphansBase
Batch.php in src/Plugin/OgDeleteOrphans/Batch.php
Cron.php in src/Plugin/OgDeleteOrphans/Cron.php
Simple.php in src/Plugin/OgDeleteOrphans/Simple.php

File

src/OgDeleteOrphansBase.php, line 19

Namespace

Drupal\og
View source
abstract class OgDeleteOrphansBase extends PluginBase implements OgDeleteOrphansInterface, ContainerFactoryPluginInterface {
  use StringTranslationTrait;

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

  /**
   * The queue factory.
   *
   * @var \Drupal\Core\Queue\QueueFactory
   */
  protected $queueFactory;

  /**
   * The OG membership manager.
   *
   * @var \Drupal\og\MembershipManagerInterface
   */
  protected $membershipManager;

  /**
   * The OG group audience helper.
   *
   * @var \Drupal\og\OgGroupAudienceHelperInterface
   */
  protected $groupAudienceHelper;

  /**
   * Constructs an OgDeleteOrphansBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Queue\QueueFactory $queue_factory
   *   The queue factory.
   * @param \Drupal\og\MembershipManagerInterface $membership_manager
   *   The OG membership manager service.
   * @param \Drupal\og\OgGroupAudienceHelperInterface $group_audience_helper
   *   The OG group audience helper.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, QueueFactory $queue_factory, MembershipManagerInterface $membership_manager, OgGroupAudienceHelperInterface $group_audience_helper) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    $this->queueFactory = $queue_factory;
    $this->membershipManager = $membership_manager;
    $this->groupAudienceHelper = $group_audience_helper;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('queue'), $container
      ->get('og.membership_manager'), $container
      ->get('og.group_audience_helper'));
  }

  /**
   * {@inheritdoc}
   */
  public function register(EntityInterface $entity) {
    foreach ($this
      ->query($entity) as $entity_type => $orphans) {
      foreach ($orphans as $orphan) {
        $this
          ->getQueue()
          ->createItem([
          'type' => $entity_type,
          'id' => $orphan,
        ]);
      }
    }
  }

  /**
   * Queries the registered group entity for orphaned members to delete.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The group entity that is the basis for the query.
   *
   * @return array
   *   An associative array, keyed by entity type, each item an array of entity
   *   IDs to delete.
   */
  protected function query(EntityInterface $entity) {

    // Register orphaned group content.
    $orphans = $this->membershipManager
      ->getGroupContentIds($entity);

    // Register orphaned user memberships.
    $membership_ids = $this->entityTypeManager
      ->getStorage('og_membership')
      ->getQuery()
      ->condition('entity_type', $entity
      ->getEntityTypeId())
      ->condition('entity_id', $entity
      ->id())
      ->execute();
    if (!empty($membership_ids)) {
      $orphans['og_membership'] = $membership_ids;
    }
    return $orphans;
  }

  /**
   * Deletes an orphaned group content entity if it is fully orphaned.
   *
   * @param string $entity_type
   *   The group content entity type.
   * @param string $entity_id
   *   The group content entity ID.
   */
  protected function deleteOrphan($entity_type, $entity_id) {
    $entity = $this->entityTypeManager
      ->getStorage($entity_type)
      ->load($entity_id);

    // The entity might already be removed by other modules that implement
    // hook_entity_delete().
    if (!$entity) {
      return;
    }

    // Only delete group content that is fully orphaned, i.e. it is no longer
    // associated with any groups.
    if ($this->groupAudienceHelper
      ->hasGroupAudienceField($entity
      ->getEntityTypeId(), $entity
      ->bundle())) {

      // Only do a group count if the entity is actually group content.
      $group_count = $this->membershipManager
        ->getGroupCount($entity);
      if ($group_count == 0) {
        $entity
          ->delete();
      }
    }
    else {
      $entity
        ->delete();
    }
  }

  /**
   * Returns the queue of orphans to delete.
   *
   * @return \Drupal\Core\Queue\QueueInterface
   *   The queue.
   */
  protected function getQueue() {
    return $this->queueFactory
      ->get('og_orphaned_group_content', TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function configurationForm(array $form, FormStateInterface $form_state) {
    return [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgDeleteOrphansBase::$entityTypeManager protected property The entity type manager.
OgDeleteOrphansBase::$groupAudienceHelper protected property The OG group audience helper.
OgDeleteOrphansBase::$membershipManager protected property The OG membership manager.
OgDeleteOrphansBase::$queueFactory protected property The queue factory.
OgDeleteOrphansBase::configurationForm public function Returns the configuration form elements specific to this plugin. Overrides OgDeleteOrphansInterface::configurationForm 1
OgDeleteOrphansBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
OgDeleteOrphansBase::deleteOrphan protected function Deletes an orphaned group content entity if it is fully orphaned.
OgDeleteOrphansBase::getQueue protected function Returns the queue of orphans to delete. 1
OgDeleteOrphansBase::query protected function Queries the registered group entity for orphaned members to delete.
OgDeleteOrphansBase::register public function Registers a soon to be deleted group entity, for processing. Overrides OgDeleteOrphansInterface::register 1
OgDeleteOrphansBase::__construct public function Constructs an OgDeleteOrphansBase object. Overrides PluginBase::__construct
OgDeleteOrphansInterface::process public function Starts the deletion process. 3
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
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.