Skip to content
Snippets Groups Projects

Resolve #3503412 "Set page as homepage"

Files
20
@@ -3,6 +3,8 @@
namespace Drupal\experience_builder\AutoSave;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\experience_builder\AutoSaveData;
@@ -14,9 +16,12 @@ class AutoSaveManager {
public const CACHE_TAG = 'experience_builder__auto_save';
public const string SIMPLE_CONFIG_TYPE = 'simple_config';
public function __construct(
private readonly AutoSaveTempStoreFactory $tempStoreFactory,
private readonly CacheTagsInvalidatorInterface $cacheTagsInvalidator,
private readonly TypedConfigManagerInterface $typedConfigManager,
) {
}
@@ -36,24 +41,37 @@ class AutoSaveManager {
return $this->tempStoreFactory->get('experience_builder.auto_save.hashes', expire: $expire);
}
public function save(EntityInterface $entity, array $data): void {
$key = $this->getAutoSaveKey($entity);
public function save(EntityInterface|Config $object, array $data): void {
$key = $this->getAutoSaveKey($object);
if ($object instanceof EntityInterface) {
$object_type = $object->getEntityTypeId();
$entity_id = $object->id();
$label = self::getLabelToSave($object, $data);
}
else {
$object_type = self::SIMPLE_CONFIG_TYPE;
$entity_id = $object->getName();
$typed_config = $this->typedConfigManager->createFromNameAndData($object->getName(), $object->get());
$label = (string) $typed_config->getDataDefinition()->getLabel();
}
$data_hash = self::generateHash($data);
$stored_hash = $this->readHash($entity);
$stored_hash = $this->readHash($object);
if ($stored_hash !== NULL && \hash_equals($stored_hash, $data_hash)) {
// We've reset back to the original values. Clear the auto-save entry but
// keep the hash.
$this->delete($entity, FALSE);
$this->delete($object, FALSE);
return;
}
$auto_save_data = [
'entity_type' => $entity->getEntityTypeId(),
'entity_id' => $entity->id(),
'entity_type' => $object_type,
'entity_id' => $entity_id,
'data' => $data,
'data_hash' => $data_hash,
'langcode' => $entity instanceof TranslatableInterface ? $entity->language()->getId() : NULL,
'label' => self::getLabelToSave($entity, $data),
'langcode' => $object instanceof TranslatableInterface ? $object->language()->getId() : NULL,
'label' => $label,
];
$this->getTempStore()->set($key, $auto_save_data);
$this->cacheTagsInvalidator->invalidateTags([self::CACHE_TAG]);
@@ -66,13 +84,16 @@ class AutoSaveManager {
(string) $data['entity_form_fields'][$key];
}
public static function getAutoSaveKey(EntityInterface $entity): string {
public static function getAutoSaveKey(EntityInterface|Config $object): string {
if ($object instanceof Config) {
return self::SIMPLE_CONFIG_TYPE . ':' . $object->getName();
}
// @todo Make use of https://www.drupal.org/project/drupal/issues/3026957
// @todo This will likely to also take into account the workspace ID.
if ($entity instanceof TranslatableInterface) {
return $entity->getEntityTypeId() . ':' . $entity->id() . ':' . $entity->language()->getId();
if ($object instanceof TranslatableInterface) {
return $object->getEntityTypeId() . ':' . $object->id() . ':' . $object->language()->getId();
}
return $entity->getEntityTypeId() . ':' . $entity->id();
return $object->getEntityTypeId() . ':' . $object->id();
}
public function recordInitialClientSideRepresentation(EntityInterface $entity, array $data): static {
@@ -80,12 +101,12 @@ class AutoSaveManager {
return $this;
}
private function readHash(EntityInterface $entity): ?string {
return $this->getHashStore()->get(self::getAutoSaveKey($entity));
private function readHash(EntityInterface|Config $object): ?string {
return $this->getHashStore()->get(self::getAutoSaveKey($object));
}
public function getAutoSaveData(EntityInterface $entity): AutoSaveData {
$auto_save_data = $this->getTempStore()->get($this->getAutoSaveKey($entity));
public function getAutoSaveData(EntityInterface|Config $object): AutoSaveData {
$auto_save_data = $this->getTempStore()->get($this->getAutoSaveKey($object));
if (\is_null($auto_save_data)) {
return new AutoSaveData(NULL);
}
@@ -115,11 +136,11 @@ class AutoSaveManager {
/**
* @see \experience_builder_entity_update()
*/
public function delete(EntityInterface $entity, bool $resetHash = TRUE): void {
public function delete(EntityInterface|Config $object, bool $resetHash = TRUE): void {
$this->cacheTagsInvalidator->invalidateTags([self::CACHE_TAG]);
$this->getTempStore()->delete($this->getAutoSaveKey($entity));
$this->getTempStore()->delete($this->getAutoSaveKey($object));
if ($resetHash) {
$this->getHashStore()->delete($this->getAutoSaveKey($entity));
$this->getHashStore()->delete($this->getAutoSaveKey($object));
}
}
Loading