You are here

class FileLinkMetadataUpdate in File Link 2.0.x

Defines 'file_link_metadata_update' queue worker.

Plugin annotation


@QueueWorker(
  id = "file_link_metadata_update",
  title = @Translation("File Link Metadata Update"),
  cron = {"time" = 60}
)

Hierarchy

Expanded class hierarchy of FileLinkMetadataUpdate

2 files declare their use of FileLinkMetadataUpdate
FileLinkItem.php in src/Plugin/Field/FieldType/FileLinkItem.php
LinkToFileConstraint.php in src/Plugin/Validation/Constraint/LinkToFileConstraint.php

File

src/Plugin/QueueWorker/FileLinkMetadataUpdate.php, line 21

Namespace

Drupal\file_link\Plugin\QueueWorker
View source
class FileLinkMetadataUpdate extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
   * Counter of processing queue workers.
   *
   * @var int
   */
  protected static $processing = 0;

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

  /**
   * FileLinkMetadataUpdate constructor.
   *
   * @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 $entityTypeManager
   *   The entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@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'));
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($data) {
    if ($data instanceof FileLinkQueueItem) {
      $storage = $this->entityTypeManager
        ->getStorage($data
        ->getType());

      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      if ($data
        ->getRevisionId() !== NULL) {
        $entity = $storage
          ->loadRevision($data
          ->getRevisionId());
      }
      else {
        $entity = $storage
          ->load($data
          ->getId());
      }
      if ($entity === NULL) {

        // The entity must have been removed.
        return;
      }
      if ($entity
        ->hasTranslation($data
        ->getLang())) {
        $entity = $entity
          ->getTranslation($data
          ->getLang());
      }

      // Do not create a revision but re-save the revision.
      if ($entity
        ->getEntityType()
        ->isRevisionable()) {
        $entity
          ->setNewRevision(FALSE);
      }
      if ($entity instanceof EntityChangedInterface) {
        if ($entity
          ->getChangedTime() > $data
          ->getTime()) {

          // The entity has been changed since.
          return;
        }

        // Do not update the changed time here.
        $entity
          ->setChangedTime($entity
          ->getChangedTime());
      }

      // Set the static property to be processing.
      static::$processing++;
      try {
        $entity
          ->save();
      } catch (\Exception $exception) {

        // Decrease the counter and re-throw the exception.
        static::$processing--;
        throw $exception;
      }
      static::$processing--;
    }
  }

  /**
   * Indication of whether the queue is processing.
   *
   * @return bool
   *   True if the queue worker is processing.
   */
  public static function isProcessing() : bool {
    return static::$processing > 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileLinkMetadataUpdate::$entityTypeManager protected property The entity type manager.
FileLinkMetadataUpdate::$processing protected static property Counter of processing queue workers.
FileLinkMetadataUpdate::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FileLinkMetadataUpdate::isProcessing public static function Indication of whether the queue is processing.
FileLinkMetadataUpdate::processItem public function Works on a single queue item. Overrides QueueWorkerInterface::processItem
FileLinkMetadataUpdate::__construct public function FileLinkMetadataUpdate constructor. Overrides PluginBase::__construct
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 2
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.