Issue #3128190 by phthlaap, Keshav Patel, msuthars, sja112, rlmumford, Prashant.c, smustgrave, quietone: FieldItemDataDefinition::getLabel() should show the label of the field type

(cherry picked from commit 94b03bcdec)
merge-requests/6880/head
Alex Pott 2024-03-02 11:34:57 +00:00
parent c57ce025c3
commit 973d283bea
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
2 changed files with 27 additions and 0 deletions

View File

@ -87,4 +87,19 @@ class FieldItemDataDefinition extends DataDefinition implements FieldItemDataDef
return $this;
}
/**
* Gets the label of the field type.
*
* If the label hasn't been set, then fall back to the label of the
* typed data definition.
*
* @return string
* The label of the field type.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getLabel() {
return parent::getLabel() ?: $this->getTypedDataManager()->getDefinition($this->getDataType())['label'];
}
}

View File

@ -99,4 +99,16 @@ class FieldItemTest extends EntityKernelTestBase {
$this->assertEquals($expected_value, $entity->{$this->fieldName}->value);
}
/**
* Tests \Drupal\Core\Field\TypedData\FieldItemDataDefinition::getLabel().
*/
public function testGetLabel(): void {
$data_definition = \Drupal::service('typed_data_manager')->createDataDefinition('field_item:string');
$this->assertEquals('Text (plain)', $data_definition->getLabel());
$label = 'Foo bar';
$data_definition->setLabel($label);
$this->assertEquals($label, $data_definition->getLabel());
}
}