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

Issue fork drupal-3128190

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

rlmumford created an issue. See original summary.

rlmumford’s picture

Status: Active » Needs review
StatusFileSize
new705 bytes
msuthars’s picture

Assigned: Unassigned » msuthars
msuthars’s picture

Assigned: msuthars » Unassigned
Status: Needs review » Needs work

@rlmumford Use $this->getTypedDataManager() instead of $this->typedDataManager. Also can you write test case for it.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

sja112’s picture

Status: Needs work » Needs review
StatusFileSize
new1.12 KB
new1.09 KB

Updated patch to add $this->getTypedDataManager() instead of $this->typedDataManager.

Please review.

msuthars’s picture

StatusFileSize
new716 bytes
new760 bytes

No need to add TypedDataTrait trait, Parent class already using this triat. Updated the patch.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Need tests

getLabel should be typehinted as a new function.

Also some test coverage will be needed

Thanks!

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

phthlaap’s picture

Status: Needs work » Needs review
StatusFileSize
new1.81 KB
new1.71 KB

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

Thank you.

smustgrave’s picture

Status: Needs review » Needs work

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

Recommend turning patch to MR as patches are going away.

phthlaap’s picture

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

smustgrave’s picture

Status: Needs work » Needs review

Will add to my list to take a look!

smustgrave’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: -Need tests +Needs change record

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

phthlaap’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record

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

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! Think this one is good.

quietone’s picture

Status: Reviewed & tested by the community » Needs review

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.

quietone’s picture

Status: Needs review » Needs work

Prashant.c made their first commit to this issue’s fork.

prashant.c’s picture

  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
phthlaap’s picture

Status: Needs work » Needs review

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

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Feedback appears to be addressed.

quietone’s picture

Status: Reviewed & tested by the community » Needs work

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

Keshav Patel made their first commit to this issue’s fork.

keshav patel’s picture

Status: Needs work » Needs review

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

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Appears suggestions were adopted

alexpott’s picture

Version: 11.x-dev » 10.3.x-dev
Category: Feature request » Task
Status: Reviewed & tested by the community » Fixed

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,...

  • alexpott committed 94b03bcd on 11.x
    Issue #3128190 by phthlaap, Keshav Patel, msuthars, sja112, rlmumford,...
alexpott’s picture

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?

Status: Fixed » Closed (fixed)

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

phthlaap’s picture

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