function LayoutOverrideFieldHelper::updateTempstoreEntityContext

Updates a layout overrides's entity context when entity values change.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity with the overridden layout.

File

core/modules/layout_builder/src/LayoutOverrideFieldHelper.php, line 45

Class

LayoutOverrideFieldHelper
Provides methods to help with entities using Layout Builder.

Namespace

Drupal\layout_builder

Code

public function updateTempstoreEntityContext(FieldableEntityInterface $entity) : void {
  if ($section_storage = $this->getOverridesSectionStorageForEntity($entity)) {
    // This is only necessary if there is a layout override in the tempstore.
    if ($this->layoutTempstoreRepository
      ->has($section_storage)) {
      /** @var \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorageInterface $override_temp_store */
      $override_temp_store = $this->layoutTempstoreRepository
        ->get($section_storage);
      // Get the entity currently in the tempstore's entity context.
      $stored_entity = $override_temp_store->getContextValue('entity');
      // Update the tempstore entity context with a copy of the new entity,
      // but retain the value of the layout field from the tempstore.
      $updated_entity = $entity;
      $updated_entity->{OverridesSectionStorage::FIELD_NAME} = $stored_entity->{OverridesSectionStorage::FIELD_NAME};
      $override_temp_store->setContextValue('entity', $updated_entity);
      $this->layoutTempstoreRepository
        ->set($override_temp_store);
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.