Skip to content
Snippets Groups Projects

#3503412 Allow setting page as homepage

Compare and
15 files
+ 360
45
Compare changes
  • Side-by-side
  • Inline
Files
15
@@ -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__autosave';
public const string SIMPLE_CONFIG_TYPE = 'system.simple';
public function __construct(
private readonly AutoSaveTempStoreFactory $tempStoreFactory,
private readonly CacheTagsInvalidatorInterface $cacheTagsInvalidator,
private readonly TypedConfigManagerInterface $typedConfigManager,
) {
}
@@ -28,32 +33,48 @@ class AutoSaveManager {
return $this->tempStoreFactory->get('experience_builder.auto_save', expire: $expire);
}
public function save(EntityInterface $entity, array $data): void {
$key = $this->getAutoSaveKey($entity);
public function save(EntityInterface|Config $object, array $data): void {
$key = self::getAutoSaveKey($object);
if ($object instanceof EntityInterface) {
$entity_type = $object->getEntityTypeId();
$entity_id = $object->id();
$label = $object->label();
}
else {
$entity_type = self::SIMPLE_CONFIG_TYPE;
$entity_id = $object->getName();
$typed_config = $this->typedConfigManager->createFromNameAndData($object->getName(), $object->get());
$label = $typed_config->getDataDefinition()->getLabel();
}
$auto_save_data = [
'entity_type' => $entity->getEntityTypeId(),
'entity_id' => $entity->id(),
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'data' => $data,
'data_hash' => \hash('xxh64', \serialize($data)),
'langcode' => $entity instanceof TranslatableInterface ? $entity->language()->getId() : NULL,
'langcode' => $object instanceof TranslatableInterface ? $object->language()->getId() : NULL,
// @todo Update label from incoming entity data once it exists.
'label' => (string) $entity->label(),
'label' => (string) $label,
];
$this->getTempStore()->set($key, $auto_save_data);
$this->cacheTagsInvalidator->invalidateTags([self::CACHE_TAG]);
}
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 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(self::getAutoSaveKey($object));
if (\is_null($auto_save_data)) {
return new AutoSaveData(NULL);
}
@@ -67,8 +88,7 @@ class AutoSaveManager {
/**
* Gets all auto-save data.
*
* @return array<string, array{data: array, owner: int, updated: int, entity_type: string, entity_id: string|int, label: string, langcode: ?string}>
* All auto-save data entries.
* @return array<string, array{data: array, owner: int, updated: int, entity_type: string, entity_id: string|int, label: string, langcode: ?string}> All auto-save data entries.
*/
public function getAllAutoSaveList(): array {
return \array_map(static fn (object $entry) => $entry->data +
@@ -80,9 +100,9 @@ class AutoSaveManager {
], $this->getTempStore()->getAll());
}
public function delete(EntityInterface $entity): void {
public function delete(EntityInterface|Config $object): void {
$this->cacheTagsInvalidator->invalidateTags([self::CACHE_TAG]);
$this->getTempStore()->delete($this->getAutoSaveKey($entity));
$this->getTempStore()->delete(self::getAutoSaveKey($object));
}
}
Loading