diff --git a/src/AutoSave/AutoSaveManager.php b/src/AutoSave/AutoSaveManager.php index 7dd8275c4f32cb1bafdcc5e5519f4e7932983f70..bc38385bb934a3228901185f368b009418bfa9ae 100644 --- a/src/AutoSave/AutoSaveManager.php +++ b/src/AutoSave/AutoSaveManager.php @@ -36,13 +36,19 @@ class AutoSaveManager { 'data' => $data, 'data_hash' => \hash('xxh64', \serialize($data)), 'langcode' => $entity instanceof TranslatableInterface ? $entity->language()->getId() : NULL, - // @todo Update label from incoming entity data once it exists. - 'label' => (string) $entity->label(), + 'label' => self::getLabelToSave($entity, $data), ]; $this->getTempStore()->set($key, $auto_save_data); $this->cacheTagsInvalidator->invalidateTags([self::CACHE_TAG]); } + public static function getLabelToSave(EntityInterface $entity, array $data): string { + $key = \sprintf("%s[0][value]", $entity->getEntityType()->getKey('label')); + return empty($data['entity_form_fields'][$key]) ? + (string) $entity->label() : + (string) $data['entity_form_fields'][$key]; + } + public static function getAutoSaveKey(EntityInterface $entity): string { // @todo Make use of https://www.drupal.org/project/drupal/issues/3026957 // @todo This will likely to also take into account the workspace ID. diff --git a/tests/src/Kernel/ApiAutoSaveControllerTest.php b/tests/src/Kernel/ApiAutoSaveControllerTest.php index 16a5c6d4d79c50c0484a63d5c6898b6d714a48ae..36bd38d81f202554279f7bb549924352aefa9e6d 100644 --- a/tests/src/Kernel/ApiAutoSaveControllerTest.php +++ b/tests/src/Kernel/ApiAutoSaveControllerTest.php @@ -76,7 +76,11 @@ final class ApiAutoSaveControllerTest extends KernelTestBase { ], ], 'model' => [], - 'entity_form_fields' => [], + 'entity_form_fields' => [ + // Ensure that if the form title is empty, the saved title will be + // returned. + 'title[0][value]' => '', + ], ]; $anonAccountContent = Node::create([ 'type' => 'article', @@ -105,7 +109,10 @@ final class ApiAutoSaveControllerTest extends KernelTestBase { self::assertNotFalse($sampleData); $data = \json_decode($sampleData, TRUE); $data += ['entity_form_fields' => []]; - // Full data. + // Update the page title. + $new_title = $this->getRandomGenerator()->sentences(10); + $data['entity_form_fields']['title[0][value]'] = $new_title; + $account1content = Node::load(1); \assert($account1content instanceof NodeInterface); $autoSave->save($account1content, $data); @@ -238,7 +245,7 @@ final class ApiAutoSaveControllerTest extends KernelTestBase { 'avatar' => $avatarUrl, 'uri' => $account1->toUrl()->toString(), ], - 'label' => $account1content->label(), + 'label' => $new_title, 'data_hash' => \hash('xxh64', \serialize($data)), ], \array_diff_key($content['node:1:en'], \array_flip(['updated']))); self::assertEquals([ @@ -604,7 +611,7 @@ final class ApiAutoSaveControllerTest extends KernelTestBase { 'meta' => [ 'entity_type' => 'node', 'entity_id' => $node1->id(), - 'label' => $node1->label(), + 'label' => $validClientJson['entity_form_fields']['title[0][value]'], 'autosave_key' => $autoSave->getAutoSaveKey($node1), ], ], @@ -643,7 +650,7 @@ final class ApiAutoSaveControllerTest extends KernelTestBase { 'meta' => [ 'entity_type' => 'node', 'entity_id' => $node1->id(), - 'label' => $node1->label(), + 'label' => $validClientJson['entity_form_fields']['title[0][value]'], 'autosave_key' => $autoSave->getAutoSaveKey($node1), ], ],