FileLinkMetadataUpdate.php in File Link 2.0.x
File
src/Plugin/QueueWorker/FileLinkMetadataUpdate.php
View source
<?php
namespace Drupal\file_link\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\file_link\FileLinkQueueItem;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileLinkMetadataUpdate extends QueueWorkerBase implements ContainerFactoryPluginInterface {
protected static $processing = 0;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
}
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'));
}
public function processItem($data) {
if ($data instanceof FileLinkQueueItem) {
$storage = $this->entityTypeManager
->getStorage($data
->getType());
if ($data
->getRevisionId() !== NULL) {
$entity = $storage
->loadRevision($data
->getRevisionId());
}
else {
$entity = $storage
->load($data
->getId());
}
if ($entity === NULL) {
return;
}
if ($entity
->hasTranslation($data
->getLang())) {
$entity = $entity
->getTranslation($data
->getLang());
}
if ($entity
->getEntityType()
->isRevisionable()) {
$entity
->setNewRevision(FALSE);
}
if ($entity instanceof EntityChangedInterface) {
if ($entity
->getChangedTime() > $data
->getTime()) {
return;
}
$entity
->setChangedTime($entity
->getChangedTime());
}
static::$processing++;
try {
$entity
->save();
} catch (\Exception $exception) {
static::$processing--;
throw $exception;
}
static::$processing--;
}
}
public static function isProcessing() : bool {
return static::$processing > 0;
}
}