You are here

class DeletedWorkspaceQueue in Multiversion 8

Class DeletedWorkspaceQueue

Plugin annotation


@QueueWorker(
  id = "deleted_workspace_queue",
  title = @Translation("Queue of deleted workspaces"),
  cron = {"time" = 60}
)

Hierarchy

Expanded class hierarchy of DeletedWorkspaceQueue

File

src/Plugin/QueueWorker/DeletedWorkspaceQueue.php, line 24

Namespace

Drupal\multiversion\Plugin\QueueWorker
View source
class DeletedWorkspaceQueue extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var \Drupal\multiversion\Workspace\WorkspaceManagerInterface
   */
  private $workspaceManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, WorkspaceManagerInterface $workspace_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    $this->workspaceManager = $workspace_manager;
  }

  /**
   * {@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'), $container
      ->get('workspace.manager'));
  }

  /**
   * @param mixed $data
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Exception
   */
  public function processItem($data) {
    $storage = $this->entityTypeManager
      ->getStorage($data['entity_type_id']);
    if ($storage instanceof ContentEntityStorageInterface && !empty($data['workspace_id'])) {
      $workspace = Workspace::load($data['workspace_id']);
      if ($workspace) {
        $this->workspaceManager
          ->setActiveWorkspace($workspace);
      }
      $original_storage = $storage
        ->getOriginalStorage();
      $entity = $original_storage
        ->load($data['entity_id']);
      if ($entity) {
        $this
          ->deleteEntity($original_storage, $entity);
      }
      $default_workspace = Workspace::load(\Drupal::getContainer()
        ->getParameter('workspace.default'));
      if ($default_workspace) {
        $this->workspaceManager
          ->setActiveWorkspace($default_workspace);
      }
    }
    elseif ($data['entity_type_id'] == 'workspace') {
      $entity = $storage
        ->load($data['entity_id']);
      if ($entity) {
        $this
          ->deleteEntity($storage, $entity);

        // Cleanup indexes.
        $database = \Drupal::database();
        $collections = [
          'multiversion.entity_index.id.' . $data['entity_id'],
          'multiversion.entity_index.uuid.' . $data['entity_id'],
          'multiversion.entity_index.rev.' . $data['entity_id'],
        ];
        $database
          ->delete('key_value')
          ->condition('collection', $collections, 'IN')
          ->execute();

        // Delete sequence indexes.
        $database
          ->delete('key_value_sorted')
          ->condition('collection', 'multiversion.entity_index.sequence.' . $data['entity_id'])
          ->execute();

        // Delete revision tree indexes.
        $database
          ->delete('key_value')
          ->condition('collection', 'multiversion.entity_index.rev.tree.' . $data['entity_id'] . '.%', 'LIKE')
          ->execute();
      }
    }
  }

  /**
   * @param \Drupal\Core\Entity\ContentEntityStorageInterface $storage
   * @param \Drupal\Core\Entity\ContentEntityStorageInterface $entity
   */
  protected function deleteEntity($storage, $entity) {
    try {
      $storage
        ->delete([
        $entity,
      ]);
    } catch (Exception $e) {
      $arguments = Error::decodeException($e) + [
        '%uuid' => $entity
          ->uuid(),
      ];
      $message = t('%type: @message in %function (line %line of %file). The error occurred while deleting the entity with the UUID: %uuid.', $arguments);
      watchdog_exception('Multiversion', $e, $message);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeletedWorkspaceQueue::$entityTypeManager protected property
DeletedWorkspaceQueue::$workspaceManager private property
DeletedWorkspaceQueue::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
DeletedWorkspaceQueue::deleteEntity protected function
DeletedWorkspaceQueue::processItem public function Overrides QueueWorkerInterface::processItem
DeletedWorkspaceQueue::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 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 3
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.