You are here

class EntityUpdateManager in Entity Usage 8

Same name and namespace in other branches
  1. 8.2 src/EntityUpdateManager.php \Drupal\entity_usage\EntityUpdateManager
  2. 8.3 src/EntityUpdateManager.php \Drupal\entity_usage\EntityUpdateManager

Class EntityUpdateManager.

@package Drupal\entity_usage

Hierarchy

Expanded class hierarchy of EntityUpdateManager

1 string reference to 'EntityUpdateManager'
entity_usage.services.yml in ./entity_usage.services.yml
entity_usage.services.yml
1 service uses EntityUpdateManager
entity_usage.entity_update_manager in ./entity_usage.services.yml
Drupal\entity_usage\EntityUpdateManager

File

src/EntityUpdateManager.php, line 12

Namespace

Drupal\entity_usage
View source
class EntityUpdateManager {

  /**
   * The usage tracking service.
   *
   * @var \Drupal\entity_usage\EntityUsage $usage_service
   */
  protected $usageService;

  /**
   * The PluginManager track service.
   *
   * @var \Drupal\entity_usage\EntityUsageTrackManager $TrackManager
   */
  protected $trackManager;

  /**
   * EntityUpdateManager constructor.
   *
   * @param \Drupal\entity_usage\EntityUsage $usage_service
   *   The usage tracking service.
   * @param \Drupal\entity_usage\EntityUsageTrackManager $track_manager
   *   The PluginManager track service.
   */
  public function __construct(EntityUsage $usage_service, EntityUsageTrackManager $track_manager) {
    $this->usageService = $usage_service;
    $this->trackManager = $track_manager;
  }

  /**
   * Track updates on creation of potential host entities.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity we are dealing with.
   */
  public function trackUpdateOnCreation(ContentEntityInterface $entity) {

    // Only act on content entities.
    if (!$entity instanceof ContentEntityInterface) {
      return;
    }

    // Call all plugins that want to track entity usages.
    foreach ($this->trackManager
      ->getDefinitions() as $plugin_id => $plugin_definition) {

      /** @var EntityUsageTrackInterface $instance */
      $instance = $this->trackManager
        ->createInstance($plugin_id);
      $instance
        ->trackOnEntityCreation($entity);
    }
  }

  /**
   * Track updates on deletion of potential host entities.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity we are dealing with.
   */
  public function trackUpdateOnDeletion(ContentEntityInterface $entity) {

    // Only act on content entities.
    if (!$entity instanceof ContentEntityInterface) {
      return;
    }

    // Call all plugins that want to track entity usages.
    foreach ($this->trackManager
      ->getDefinitions() as $plugin_id => $plugin_definition) {

      /** @var EntityUsageTrackInterface $instance */
      $instance = $this->trackManager
        ->createInstance($plugin_id);
      $instance
        ->trackOnEntityDeletion($entity);
    }

    // Now clean the possible usage of the entity that was deleted when target.
    $this->usageService
      ->delete($entity
      ->id(), $entity
      ->getEntityTypeId());
  }

  /**
   * Track updates on edit / update of potential host entities.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity we are dealing with.
   */
  public function trackUpdateOnEdition(ContentEntityInterface $entity) {

    // Only act on content entities.
    if (!$entity instanceof ContentEntityInterface) {
      return;
    }

    // Call all plugins that want to track entity usages.
    foreach ($this->trackManager
      ->getDefinitions() as $plugin_id => $plugin_definition) {

      /** @var EntityUsageTrackInterface $instance */
      $instance = $this->trackManager
        ->createInstance($plugin_id);
      $instance
        ->trackOnEntityUpdate($entity);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityUpdateManager::$trackManager protected property The PluginManager track service.
EntityUpdateManager::$usageService protected property The usage tracking service.
EntityUpdateManager::trackUpdateOnCreation public function Track updates on creation of potential host entities.
EntityUpdateManager::trackUpdateOnDeletion public function Track updates on deletion of potential host entities.
EntityUpdateManager::trackUpdateOnEdition public function Track updates on edit / update of potential host entities.
EntityUpdateManager::__construct public function EntityUpdateManager constructor.