UpdateEntityEncryption.php in Field Encryption 3.0.x
File
src/Plugin/QueueWorker/UpdateEntityEncryption.php
View source
<?php
namespace Drupal\field_encrypt\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UpdateEntityEncryption extends QueueWorkerBase implements ContainerFactoryPluginInterface, FieldEncryptQueueWorkerInterface {
use StringTranslationTrait;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_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'));
}
public function processItem($data) {
$entity_type = $this->entityTypeManager
->getDefinition($data['entity_type']);
$storage = $this->entityTypeManager
->getStorage($data['entity_type']);
$is_revisionable = $entity_type
->isRevisionable();
if ($is_revisionable) {
$entity = $storage
->loadRevision($data['entity_id']);
}
else {
$entity = $storage
->load($data['entity_id']);
}
if (empty($entity)) {
return;
}
if ($is_revisionable) {
$entity
->setNewRevision(FALSE);
}
$entity
->save();
}
public function batchMessage(array $data) {
return $this
->t('Updating @entity_type with ID @entity_id to use the latest field encryption settings', [
'@entity_type' => $this->entityTypeManager
->getDefinition($data['entity_type'])
->getSingularLabel(),
'@entity_id' => $data['entity_id'],
]);
}
}