Problem/Motivation

A common need is to display not the entire referenced entity, but just the label. Today, you have to request those separately.

Proposed resolution

At #2808217-60: To be able to view Vocabulary config entities via REST, one should not have to grant the 'administer taxonomy' permission, @Berdir proposes to automatically expose entity reference labels.

Remaining tasks

  1. Discuss whether we want this.
  2. Discuss whether we need a way to turn this off, to retain 100% BC.

User interface changes

None.

API changes

Theoretically a BC break. But since this is only adding new data to the existing responses, this is really an API addition.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wim Leers created an issue. See original summary.

Wim Leers’s picture

Status: Active » Needs review
FileSize
2.92 KB
tedbow’s picture

Issue tags: +Needs tests

I like the change and it seems like it could reduce extra requests for each referenced entity to get the label.

Discuss whether we need a way to turn this off, to retain 100% BC.

I would hope we would not need another BC_* flag for this. I guess this something we didn't talk about in DrupalCon Vienna, whether API additions would need this BC_* flags.

I think if we can keep those to changes to existing field changes values that would be better and simpler.

We will need tests to prove these labels are actually returned, maybe just up the existing REST entity tests.

Wim Leers’s picture

We will need tests to prove these labels are actually returned, maybe just up the existing REST entity tests.

There should be lots and lots of failing tests due to these changes — IOW existing test coverage already covers this :)

Status: Needs review » Needs work

The last submitted patch, 2: 2913518-2.patch, failed testing. View results

tedbow’s picture

Status: Needs work » Needs review
FileSize
13.9 KB
16.57 KB

Added 'label' key to tests where needed.

Status: Needs review » Needs work

The last submitted patch, 6: 2913518-6.patch, failed testing. View results

tedbow’s picture

Status: Needs work » Needs review
FileSize
1.45 KB
18.02 KB

This patch fixes media test fail.

Another problem I am seeing is the difference in how Config Entity(really non FieldableEntityInterface) is handled in hal_json format.

In the current patch NodeHalJsonAnonTest passes but NodeJsonAnonTest does not(there are other similar cases).

This is because they both rely on \Drupal\Tests\rest\Functional\EntityResource\Node\NodeResourceTestBase::getExpectedNormalizedEntity for the value of type which is an entity reference field with the target type node_type.

For json format the label 'article' is returned but for 'hal_json' not label is returned.

This because in \Drupal\hal\Normalizer\EntityReferenceItemNormalizer::normalize we have

// If this is not a content entity, let the parent implementation handle it,
    // only content entities are supported as embedded resources.
    if (!($target_entity instanceof FieldableEntityInterface)) {
      return parent::normalize($field_item, $format, $context);
    }

So for node_type it is handled by \Drupal\hal\Normalizer\FieldItemNormalizer which does not handle 'label'.

So maybe we I think should either handle the logic for Config entities in \Drupal\hal\Normalizer\EntityReferenceItemNormalizer also or we could change it to ContentEntityReferenceItemNormalizer and add a new ConfigEntityReferenceItemNormalizer. They would both have

protected $supportedInterfaceOrClass = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem';

So we would have to supportsNormalization()in each normalizer to make sure they only covered the correct $target_entity values.

Of if changing the name of EntityReferenceItemNormalizer is a BC break we could just add the new ConfigEntityReferenceItemNormalizer but still use supportsNormalization() in both the new and existing normalizers.

Status: Needs review » Needs work

The last submitted patch, 8: 2913518-8.patch, failed testing. View results

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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.

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.

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.