You are here

class UpdateEntityEncryption in Field Encryption 3.0.x

A Queue Worker that updates an entity's encryption on cron run.

This re-saves the entity causing it to use the current field encryption settings. This can:

  • encrypt fields that have become encrypted after the entity was last saved
  • decrypt fields that no longer are set to be encrypted
  • change the encryption profile that is used.

Plugin annotation


@QueueWorker(
  id = "field_encrypt_update_entity_encryption",
  title = @Translation("Field encrypt: update encrption profile."),
  cron = {"time" = 15}
)

Hierarchy

Expanded class hierarchy of UpdateEntityEncryption

File

src/Plugin/QueueWorker/UpdateEntityEncryption.php, line 27

Namespace

Drupal\field_encrypt\Plugin\QueueWorker
View source
class UpdateEntityEncryption extends QueueWorkerBase implements ContainerFactoryPluginInterface, FieldEncryptQueueWorkerInterface {
  use StringTranslationTrait;

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

  /**
   * Creates a new UpdateEntityEncryption object.
   *
   * @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 $entity_type_manager
   *   The entity type manager.
   */
  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;
  }

  /**
   * {@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) {
    $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 the entity no longer exists then there is nothing to do.
    if (empty($entity)) {
      return;
    }

    // Don't create unnecessary revisions.
    if ($is_revisionable) {
      $entity
        ->setNewRevision(FALSE);
    }
    $entity
      ->save();
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UpdateEntityEncryption::$entityTypeManager protected property The entity type manager.
UpdateEntityEncryption::batchMessage public function Generates a batch message for an item. Overrides FieldEncryptQueueWorkerInterface::batchMessage
UpdateEntityEncryption::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
UpdateEntityEncryption::processItem public function Works on a single queue item. Overrides QueueWorkerInterface::processItem
UpdateEntityEncryption::__construct public function Creates a new UpdateEntityEncryption object. Overrides PluginBase::__construct