diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php b/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php index a459b6e..a9c27d4 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php @@ -38,18 +38,23 @@ public function delete() { * {@inheritdoc} */ public function getValue($include_computed = FALSE) { - /** - * @todo Is there better way to do this??? - * This is needed because: - * 1. for this class to work with \Drupal\serialization\Normalizer\ListNormalizer::normalize - * I think because \Drupal\Core\Entity\ContentEntityBase::__sleep call $field->getValue() - * otherwise this will always return empty. - * 2. For $node->path->getValue() to work. - * getValue() will work if you just saved node with the path - * But if you try node_load($node->id(), TRUE); getValue() will NOT work. - */ - $this->first()->getValue(); + // Automatically create the first item for computed fields. + // @todo: Move this to the base class. + if (!isset($this->list[0]) && $this->definition->isComputed()) { + $this->list[0] = $this->createItem(0); + } return parent::getValue($include_computed); } + /** + * {@inheritdoc} + */ + public function isEmpty() { + // Automatically create the first item for computed fields. + if (!isset($this->list[0]) && $this->definition->isComputed()) { + $this->list[0] = $this->createItem(0); + } + return parent::isEmpty(); + } + } diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php index 9c218a4..a3f8892 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php @@ -78,6 +78,14 @@ public function getValue() { /** * {@inheritdoc} */ + public function isEmpty() { + $this->ensureLoaded(); + return parent::isEmpty(); + } + + /** + * {@inheritdoc} + */ public function preSave() { $this->alias = trim($this->alias); } diff --git a/core/modules/path/tests/src/Kernel/PathItemTest.php b/core/modules/path/tests/src/Kernel/PathItemTest.php new file mode 100644 index 0000000..461c596 --- /dev/null +++ b/core/modules/path/tests/src/Kernel/PathItemTest.php @@ -0,0 +1,109 @@ +installEntitySchema('node'); + $this->installEntitySchema('user'); + + $this->installSchema('node', ['node_access']); + + \Drupal::service('router.builder')->rebuild(); + + $node_type = NodeType::create(['type' => 'foo']); + $node_type->save(); + + } + + /** + * Tests for no canonical link templates. + */ + public function testPathItem() { + + /** @var \Drupal\Core\Path\AliasStorageInterface $alias_storage */ + $alias_storage = \Drupal::service('path.alias_storage'); + + $node_storage = \Drupal::entityTypeManager()->getStorage('node'); + + $node = Node::create([ + 'title' => 'Testing create()', + 'type' => 'foo', + 'path' => ['alias' => '/foo'] + ]); + $this->assertFalse($node->get('path')->isEmpty()); + $this->assertEquals('/foo', $node->get('path')->alias); + + $node->save(); + $this->assertFalse($node->get('path')->isEmpty()); + $this->assertEquals('/foo', $node->get('path')->alias); + + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId()); + $this->assertEquals('/foo', $stored_alias); + + $node_storage->resetCache(); + $loaded_node = $node_storage->load($node->id()); + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/foo', $loaded_node->get('path')->alias); + + $node_storage->resetCache(); + $loaded_node = $node_storage->load($node->id()); + $values = $loaded_node->get('path')->getValue(); + $this->assertEquals('/foo', $values[0]['alias']); + + $loaded_node->get('path')->alias = '/bar'; + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/bar', $loaded_node->get('path')->alias); + + $loaded_node->save(); + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/bar', $loaded_node->get('path')->alias); + + $node_storage->resetCache(); + $loaded_node = $node_storage->load($node->id()); + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/bar', $loaded_node->get('path')->alias); + + $loaded_node->get('path')->alias = '/bar'; + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/bar', $loaded_node->get('path')->alias); + + $loaded_node->save(); + $this->assertFalse($loaded_node->get('path')->isEmpty()); + $this->assertEquals('/bar', $loaded_node->get('path')->alias); + + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId()); + $this->assertEquals('/bar', $stored_alias); + + $old_alias = $alias_storage->lookupPathSource('/foo', $node->language()->getId()); + $this->assertFalse($old_alias); + + $loaded_node->get('path')->alias = ''; + $this->assertEquals('', $loaded_node->get('path')->alias); + + $loaded_node->save(); + + $stored_alias = $alias_storage->lookupPathAlias('/' . $node->toUrl()->getInternalPath(), $node->language()->getId()); + $this->assertFalse($stored_alias); + } + +}