Loading core/lib/Drupal/Core/TypedData/ListInterface.php +8 −0 Original line number Diff line number Diff line Loading @@ -91,6 +91,14 @@ public function set($index, $value); */ public function first(); /** * Returns the last item in this list. * * @return \Drupal\Core\TypedData\TypedDataInterface|null * The last item in this list, or NULL if there are no items. */ public function last(): ?TypedDataInterface; /** * Appends a new item to the list. * Loading core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php +7 −0 Original line number Diff line number Diff line Loading @@ -303,4 +303,11 @@ public function __clone() { } } /** * {@inheritdoc} */ public function last(): ?TypedDataInterface { return $this->get($this->count() - 1); } } core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php +21 −0 Original line number Diff line number Diff line Loading @@ -695,4 +695,25 @@ public function testTypedDataValidation(): void { $this->assertSame('0.value', $violations[0]->getPropertyPath()); } /** * Tests the last() method on typed data lists. */ public function testTypedDataListsLast(): void { // Create an ItemList with two string items. $value = ['zero', 'one']; $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value); // Assert that the last item is the second one ('one'). $this->assertEquals('one', $typed_data->last()->getValue()); // Add another item to the list and check the last item. $value[] = 'two'; $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value); $this->assertEquals('two', $typed_data->last()->getValue()); // Check behavior with an empty list. $typed_data = $this->createTypedData(ListDataDefinition::create('string'), []); $this->assertNull($typed_data->last(), 'Empty list should return NULL.'); } } Loading
core/lib/Drupal/Core/TypedData/ListInterface.php +8 −0 Original line number Diff line number Diff line Loading @@ -91,6 +91,14 @@ public function set($index, $value); */ public function first(); /** * Returns the last item in this list. * * @return \Drupal\Core\TypedData\TypedDataInterface|null * The last item in this list, or NULL if there are no items. */ public function last(): ?TypedDataInterface; /** * Appends a new item to the list. * Loading
core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php +7 −0 Original line number Diff line number Diff line Loading @@ -303,4 +303,11 @@ public function __clone() { } } /** * {@inheritdoc} */ public function last(): ?TypedDataInterface { return $this->get($this->count() - 1); } }
core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php +21 −0 Original line number Diff line number Diff line Loading @@ -695,4 +695,25 @@ public function testTypedDataValidation(): void { $this->assertSame('0.value', $violations[0]->getPropertyPath()); } /** * Tests the last() method on typed data lists. */ public function testTypedDataListsLast(): void { // Create an ItemList with two string items. $value = ['zero', 'one']; $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value); // Assert that the last item is the second one ('one'). $this->assertEquals('one', $typed_data->last()->getValue()); // Add another item to the list and check the last item. $value[] = 'two'; $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value); $this->assertEquals('two', $typed_data->last()->getValue()); // Check behavior with an empty list. $typed_data = $this->createTypedData(ListDataDefinition::create('string'), []); $this->assertNull($typed_data->last(), 'Empty list should return NULL.'); } }