You are here

class AcquiadamUsageSubscriber in Media: Acquia DAM 8

Subscribe to EntityUsage events.

Hierarchy

  • class \Drupal\media_acquiadam_report\EventSubscriber\AcquiadamUsageSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AcquiadamUsageSubscriber

1 string reference to 'AcquiadamUsageSubscriber'
media_acquiadam_report.services.yml in modules/media_acquiadam_report/media_acquiadam_report.services.yml
modules/media_acquiadam_report/media_acquiadam_report.services.yml
1 service uses AcquiadamUsageSubscriber
media_acquiadam_report.acquiadam_usage_subscriber in modules/media_acquiadam_report/media_acquiadam_report.services.yml
Drupal\media_acquiadam_report\EventSubscriber\AcquiadamUsageSubscriber

File

modules/media_acquiadam_report/src/EventSubscriber/AcquiadamUsageSubscriber.php, line 14

Namespace

Drupal\media_acquiadam_report\EventSubscriber
View source
class AcquiadamUsageSubscriber implements EventSubscriberInterface {

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

  /**
   * Constructs a AcquiadamUsageSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Respond when entity usage records are added/updated.
    return [
      Events::USAGE_REGISTER => [
        'mediaUsageChange',
      ],
    ];
  }

  /**
   * This method is called whenever an EntityUsage event is dispatched.
   *
   * @param \Drupal\entity_usage\Events\EntityUsageEvent $event
   *   The event being triggered.
   */
  public function mediaUsageChange(EntityUsageEvent $event) {
    if ($event
      ->getTargetEntityType() == 'media') {
      $mid = $event
        ->getTargetEntityId();
      $asset_id_fields = media_acquiadam_get_bundle_asset_id_fields();
      $media = $this->entityTypeManager
        ->getStorage('media')
        ->load($mid);
      $media_bundle = $media
        ->bundle();

      // Clear cache tag on asset listing so entity usage count is up to date.
      if (array_key_exists($media_bundle, $asset_id_fields)) {
        Cache::invalidateTags([
          'config:views.view.acquia_dam_reporting',
        ]);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiadamUsageSubscriber::$entityTypeManager protected property Entity type manager service.
AcquiadamUsageSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AcquiadamUsageSubscriber::mediaUsageChange public function This method is called whenever an EntityUsage event is dispatched.
AcquiadamUsageSubscriber::__construct public function Constructs a AcquiadamUsageSubscriber object.