EntityUsageRegenerateTrackingInfoWorker.php in Entity Usage 8.2
File
src/Plugin/QueueWorker/EntityUsageRegenerateTrackingInfoWorker.php
View source
<?php
namespace Drupal\entity_usage\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\entity_usage\EntityUpdateManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityUsageRegenerateTrackingInfoWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $logger;
protected $entityUsageUpdateManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LoggerChannelFactoryInterface $logger_factory, EntityUpdateManager $track_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->logger = $logger_factory
->get('entity_usage');
$this->entityUsageUpdateManager = $track_manager;
}
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('logger.factory'), $container
->get('entity_usage.entity_update_manager'));
}
public function processItem($data) {
if (empty($data['entity_type']) || empty($data['entity_id']) && empty($data['entity_revision_id'])) {
return;
}
$storage = $this->entityTypeManager
->getStorage($data['entity_type']);
if ($storage
->getEntityType()
->isRevisionable() && !empty($data['entity_revision_id'])) {
$entity = $storage
->loadRevision($data['entity_revision_id']);
}
elseif (!empty($data['entity_id'])) {
$entity = $storage
->load($data['entity_id']);
}
if ($entity) {
try {
$this->entityUsageUpdateManager
->trackUpdateOnCreation($entity);
} catch (\Exception $e) {
$this->logger
->warning("An error occurred when tracking usage info for entity with data: @data. Error message: {$e->getMessage()}", [
'@data' => json_encode($data),
]);
}
}
}
}