diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php b/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php index 5515a90a6b..07649aeba7 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php @@ -76,7 +76,14 @@ public function count() { */ protected function ensureLoaded() { if (!isset($this->list[0]) && $this->definition->isComputed()) { + // Create the first item in the list which will load the computed alias + // value if it exists. $this->list[0] = $this->createItem(0); + // If no alias actually exists remove the empty item. This ensures that + // count() will not return 1 for an empty list. + if ($this->list[0]->isEmpty()) { + unset($this->list[0]); + } } } diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php index 41063812dd..bf43f154e0 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php @@ -174,12 +174,12 @@ protected function ensureLoaded() { if ($alias) { $this->setValue($alias); } - else { + elseif (count($this->values)) { // If there is no existing alias, default the langcode to the current // language. // @todo Set the langcode to not specified for untranslatable fields // in https://www.drupal.org/node/2689459. - $this->langcode = $this->getLangcode(); + $this->langcode = $this->getLangcode(); } } $this->isLoaded = TRUE; diff --git a/core/modules/path/tests/src/Kernel/PathItemTest.php b/core/modules/path/tests/src/Kernel/PathItemTest.php index 7ddcdd0e92..0e2eaf8d90 100644 --- a/core/modules/path/tests/src/Kernel/PathItemTest.php +++ b/core/modules/path/tests/src/Kernel/PathItemTest.php @@ -57,12 +57,12 @@ public function testPathItem() { 'type' => 'foo', ]); // Test using count() first. - $this->assertEquals(1, count($node->get('path'))); + $this->assertEquals(0, count($node->get('path'))); $this->assertTrue($node->get('path')->isEmpty()); $node->save(); - $this->assertEquals(1, count($node->get('path'))); - $this->assertFalse($node->get('path')->isEmpty()); + $this->assertEquals(0, count($node->get('path'))); + $this->assertTrue($node->get('path')->isEmpty()); $node = Node::create([ 'title' => 'Testing no path', @@ -70,12 +70,12 @@ public function testPathItem() { ]); // Test using isEmpty() first. $this->assertTrue($node->get('path')->isEmpty()); - $this->assertEquals(1, count($node->get('path'))); + $this->assertEquals(0, count($node->get('path'))); $node->save(); - $this->assertFalse($node->get('path')->isEmpty()); - $this->assertEquals(1, count($node->get('path'))); + $this->assertTrue($node->get('path')->isEmpty()); + $this->assertEquals(0, count($node->get('path'))); // Test with an entity with path field set. $node = Node::create([