You are here

protected function ConfigSubscriber::setUncacheableEntityTypes in Field Encryption 8.2

Figure out which entity types are uncacheable due to encrypted fields.

1 call to ConfigSubscriber::setUncacheableEntityTypes()
ConfigSubscriber::onConfigSave in src/EventSubscriber/ConfigSubscriber.php
React on the configuration save event.

File

src/EventSubscriber/ConfigSubscriber.php, line 212

Class

ConfigSubscriber
Updates existing data when field encryption settings are updated.

Namespace

Drupal\field_encrypt\EventSubscriber

Code

protected function setUncacheableEntityTypes() {
  $types = [];
  $entity_types = $this->entityTypeManager
    ->getDefinitions();
  $old_uncacheable_types = $this->state
    ->get('uncacheable_entity_types', []);
  foreach ($entity_types as $entity_type) {
    if ($entity_type instanceof ContentEntityTypeInterface) {
      $storage_class = $this->entityTypeManager
        ->createHandlerInstance($entity_type
        ->getStorageClass(), $entity_type);
      if ($storage_class instanceof DynamicallyFieldableEntityStorageInterface) {

        // Query by filtering on the ID as this is more efficient than filtering
        // on the entity_type property directly.
        $ids = $this->entityTypeManager
          ->getStorage('field_storage_config')
          ->getQuery()
          ->condition('id', $entity_type
          ->id() . '.', 'STARTS_WITH')
          ->execute();

        // Fetch all fields on entity type.
        $field_storages = FieldStorageConfig::loadMultiple($ids);
        if ($field_storages) {
          foreach ($field_storages as $storage) {

            // Check if field is encrypted.
            if ($storage
              ->getThirdPartySetting('field_encrypt', 'uncacheable', FALSE) == TRUE) {

              // If there is an encrypted field, mark this entity type as
              // uncacheable.
              $type = $storage
                ->getTargetEntityTypeId();
              $types[$type] = $type;
            }
          }
        }
      }
    }
  }
  $this->state
    ->set('uncacheable_entity_types', $types);

  // @see field_encrypt_entity_type_alter()
  $this->entityTypeManager
    ->clearCachedDefinitions();
  $entity_types = $this->entityTypeManager
    ->getDefinitions();
  $changed_types = array_merge(array_diff($old_uncacheable_types, $types), array_diff($types, $old_uncacheable_types));

  // Types that have changed need to have their last installed definition
  // updated. We need to be careful to only change the settings we are
  // interested in.
  foreach ($changed_types as $type) {
    $last_installed_definition = $this->entitySchemaRepository
      ->getLastInstalledDefinition($type);
    $last_installed_definition
      ->set('static_cache', $entity_types[$type]
      ->get('static_cache') ?? FALSE)
      ->set('render_cache', $entity_types[$type]
      ->get('render_cache') ?? FALSE)
      ->set('persistent_cache', $entity_types[$type]
      ->get('persistent_cache') ?? FALSE);
    $this->entitySchemaRepository
      ->setLastInstalledDefinition($last_installed_definition);
  }
}