FieldItemDataDefinition::getLabel() should show the label of the field type

Created on 16 April 2020, about 4 years ago
Updated 21 March 2024, 2 months ago

Problem/Motivation

\Drupal::service('typed_data_manager')->createDataDefinition('entity:user')->getLabel(); helpfully returns the the label of the entity type, in this case user.

\Drupal::service('typed_data_manager')->createDataDefinition('field_item:string')->getLabel(); returns nothing.

It would be helpful if this returned the label of the field type. This can be used when working with configurable contexts to show what type of data a context is.

Steps to reproduce

Run \Drupal::service('typed_data_manager')->createDataDefinition('field_item:string')->getLabel(); get nothing returned

Proposed resolution

Add a getLabel() method to FieldItemDataDefinition that returns the label of the field type plugin.

Remaining tasks

Change record

User interface changes

NA

API changes

core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php has a new method getLabel().

Data model changes

NA

Release notes snippet

NA

๐Ÿ“Œ Task
Status

Fixed

Version

10.3 โœจ

Component
Fieldย  โ†’

Last updated about 7 hours ago

Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom rlmumford Manchester

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    getLabel should be typehinted as a new function.

    Also some test coverage will be needed

    Thanks!

  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ป๐Ÿ‡ณVietnam phthlaap

    I've added a test for the getLabel method. I would appreciate your assistance in reviewing it.

    Thank you.

  • last update 6 months ago
    30,676 pass
  • Status changed to Needs work 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    #13 I should of also tagged for a change record. Can be super simple.

    Recommend turning patch to MR as patches are going away.

  • ๐Ÿ‡ป๐Ÿ‡ณVietnam phthlaap

    I have created a merge request. Could you please take a look and let me know if everything is in order?

  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Will add to my list to take a look!

  • Pipeline finished with Success
    6 months ago
    Total: 59020s
    #55315
  • Status changed to Needs work 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Removing tests tag as they have been added and can be seen in the test-only feature

    There was 1 failure:
    1) Drupal\KernelTests\Core\Field\FieldItemTest::testFieldItemDataDefinitionReturnLabel
    Failed asserting that null matches expected 'Text (plain)'.
    /builds/issue/drupal-3128190/vendor/phpunit/phpunit/src/Framework/Constraint/Equality/IsEqual.php:94
    /builds/issue/drupal-3128190/core/tests/Drupal/KernelTests/Core/Field/FieldItemTest.php:109
    /builds/issue/drupal-3128190/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
    FAILURES!
    Tests: 2, Assertions: 31, Failures: 1.
    

    Believe the last thing to do is draft a change record.

    Updated issue summary too with the correct template

  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ป๐Ÿ‡ณVietnam phthlaap

    Please help to review change record https://www.drupal.org/node/3404250 โ†’

  • Status changed to RTBC 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Thanks! Think this one is good.

  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone New Zealand

    I'm triaging RTBC issues โ†’ . I read the IS and the comments, the MR and the Change record.

    Thanks for working on this feature!

    Overall, the change is working but there is some more things to do to finish this.

    • There is a comment in the MR that this has an out of scope change.
    • The test should also test when there is a label.
    • The change record should explain what the return value will be when there is a label and when there is not. Currently, it is a misleading.

    Back to needs work for a few more things.

  • Status changed to Needs work 5 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone New Zealand
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Prashant.c Dharamshala

    Prashant.c โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Prashant.c Dharamshala
    1. There is a comment in the MR that this has an out of scope change
    2. DONE

    3. The test should also test when there is a label.
      It is there https://git.drupalcode.org/project/drupal/-/merge_requests/5550/diffs#dc...
    4. The change record should explain what the return value will be when there is a label and when there is not. Currently, it is a misleading.
      Still pending
  • Pipeline finished with Success
    5 months ago
    Total: 553s
    #68654
  • Pipeline finished with Failed
    5 months ago
    Total: 115s
    #72381
  • Pipeline finished with Failed
    5 months ago
    Total: 210s
    #72383
  • Pipeline finished with Failed
    5 months ago
    #72387
  • Pipeline finished with Failed
    5 months ago
    Total: 250s
    #72388
  • Pipeline finished with Success
    5 months ago
    Total: 841s
    #72392
  • Status changed to Needs review 5 months ago
  • ๐Ÿ‡ป๐Ÿ‡ณVietnam phthlaap

    I resolved all items in comment #23. Please help to review.

  • Pipeline finished with Success
    5 months ago
    Total: 552s
    #72403
  • Status changed to RTBC 5 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Feedback appears to be addressed.

  • Status changed to Needs work 4 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone New Zealand

    I left two suggestions in the MR to simply method name and a comment.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Keshav Patel

    Keshav Patel โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Canceled
    4 months ago
    Total: 130s
    #90152
  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Keshav Patel

    Updated the method name and comment as per #29, Please review.

  • Pipeline finished with Success
    4 months ago
    Total: 599s
    #90154
  • Status changed to RTBC 4 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Appears suggestions were adopted

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    Committed and pushed 94b03bcdec to 11.x and 973d283bea to 10.3.x. Thanks!

    This is almost a bug...

    We cannot add a return type here. This would break contrib code that extends this class and overrides the method already.

    diff --git a/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php b/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php
    index 2cd2137b0e..2604eec502 100644
    --- a/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php
    +++ b/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php
    @@ -98,7 +98,7 @@ public function setFieldDefinition($field_definition) {
        *
        * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
        */
    -  public function getLabel(): string {
    +  public function getLabel() {
         return parent::getLabel() ?: $this->getTypedDataManager()->getDefinition($this->getDataType())['label'];
       }
    

    Fixed on commit.

    • alexpott โ†’ committed 973d283b on 10.3.x
      Issue #3128190 by phthlaap, Keshav Patel, msuthars, sja112, rlmumford,...
  • Status changed to Fixed 3 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    After committing I wonder if we should push this fix to the parent class and make the following change...

    diff --git a/core/lib/Drupal/Core/TypedData/DataDefinition.php b/core/lib/Drupal/Core/TypedData/DataDefinition.php
    index a82f819b4c..c4acc004ec 100644
    --- a/core/lib/Drupal/Core/TypedData/DataDefinition.php
    +++ b/core/lib/Drupal/Core/TypedData/DataDefinition.php
    @@ -72,7 +72,7 @@ public function setDataType($type) {
        * {@inheritdoc}
        */
       public function getLabel() {
    -    return $this->definition['label'] ?? NULL;
    +    return $this->definition['label'] ?? $this->getTypedDataManager()->getDefinition($this->getDataType())['label'] ?? NULL;
       }
     
       /**
    

    And remove the override in FieldItemDataDefinition

    Can someone file a follow-up to do this?

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Pipeline finished with Failed
    2 months ago
    Total: 547s
    #124349
  • ๐Ÿ‡ป๐Ÿ‡ณVietnam phthlaap

    As @alexpott suggestion, I moved code to parent class but I dont know how to fix the test failed.

Production build 0.69.0 2024