Problem/Motivation

A difference in argument types seems to be creating some misleading error messages for contrib projects...

In core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php and many other plugins, we have

protected function getEntitiesToView(<strong>EntityReferenceFieldItemListInterface</strong> $items, $langcode) {

but then related plugins (such as core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php) attempt to pass a FieldItemListInterface to getEntitiesToView instead of EntityReferenceFieldItemListInterface.

public function viewElements(<strong>FieldItemListInterface</strong> $items, $langcode) {
    $elements = [];

    foreach ($this-><strong>getEntitiesToView</strong>($items, $langcode) as $delta => $file) {

This can throw errors like the one below.

Proposed resolution

Contrib issues should extend EntityReferenceFieldItemListInterface instead of FieldItemListInterface similar to #2878691: Error: Recoverable fatal error: Argument 1 passed to Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView which seems to resolve the issue, but is not clear from the outset.

Remaining tasks

Determine if it is possible to make this clearer in Core somewhere or resolve the argument mismatch?

------ original issue description -----

The next plugins
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceIdFormatter
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter
Drupal\taxonomy\Plugin\Field\FieldFormatter\EntityReferenceTaxonomyTermRssFormatter
Drupal\user\Plugin\Field\FieldFormatter\AuthorFormatter
Drupal\entity_reference_revisions\Plugin\Field\FieldFormatter\EntityReferenceRevisionsEntityFormatter

In the method viewElements() the first argument implemented interface Drupal\Core\Field\FieldItemListInterface but the first argument of the method getEntitiesToView() incide previos one implemented interface Drupal\Core\Field\EntityReferenceFieldItemListInterface.

As a result, I've got an error -

TypeError: Argument 1 passed to Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView() must implement interface Drupal\Core\Field\EntityReferenceFieldItemListInterface, instance of Drupal\Core\Field\FieldItemList given, called in /var/www/some-site/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php on line 162 in Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->getEntitiesToView() (line 42 of core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php).

Comments

Tаo created an issue. See original summary.

tаo’s picture

Assigned: tаo » Unassigned
Status: Active » Needs review
StatusFileSize
new1.43 KB
tаo’s picture

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

froboy’s picture

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

I've tested the patch in #3 and (shockingly) it applies cleanly to Drupal core 9.2.9. We experienced the error above (TypeError: Argument 1 passed to...) when indexing content with search_api_solr. I'm still digging into the root cause, but applying the patch resolved our issue.

I'm changing the version to 9.4.x to see if this tests well there. Unfortunately my reproduction steps are relatively complex. I was able to recreate the error with the Open Y Virtual YMCA Search module from version 1.6 of https://github.com/ymcatwincities/openy_gated_content.

anpolimus’s picture

Approving that patch #3 fixes the issue

podarok’s picture

Status: Needs review » Reviewed & tested by the community

+1 RTBC
Works well for us on 9.3.9 core, PHP 8.1.4

quietone’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)
Issue tags: +Needs issue summary update, +Needs steps to reproduce, +Bug Smash Initiative

The issue summary begins with 'The next plugins' as if this is a follow up to some other work. Is it? If that is the case, please add the reference. From my reading of the issue is it not clear that this is reproducible with just Drupal core. Can anyone provide steps to reproduce? Adding tags.

I then searched and found #3138527: getEntitiesToView() should implements the same interface as viewElements() which has the same patch as this but with a different list of plugins in the Issue Summary. If this suggested fix here is relevant to more plugin/situations that should be discussed here. I am closing the other issue as a duplicate of this one and noting the plugins here:

  • Drupal\user\Plugin\Field\FieldFormatter\AuthorFormatter
  • Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter
  • Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter
  • Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceIdFormatter
  • Drupal\taxonomy\Plugin\Field\FieldFormatter\EntityReferenceTaxonomyTermRssFormatter

.

Cheers.

scott_earnest’s picture

StatusFileSize
new176.23 KB

perhaps helpful as a step to reproduce. I am seeing a warning in PHPStorm with phpcs enabled with standard "Drupal" (see attached). There is a similar file attached to this ticket from the reporter. Patch #3 fixes the warning.

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.

froboy’s picture

Status: Postponed (maintainer needs more info) » Needs work

This change seems to have upstream consequences as well. I ran into an issue with a Webforms Entity Reference field throwing this error:

Fatal error: Declaration of Drupal\webform\Plugin\Field\FieldFormatter\WebformEntityReferenceFormatterBase::getEntitiesToView(Drupal\Core\Field\EntityReferenceFieldItemListInterface $items, $langcode) must be compatible with Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView(Drupal\Core\Field\FieldItemListInterface $items, $langcode) in /var/www/modules/contrib/webform/src/Plugin/Field/FieldFormatter/WebformEntityReferenceFormatterBase.php on line 60

and that seems to be a result of this patch. A GitLab search reveals almost 100 other calls which would have to be updated as a result of this change.

So... unless this is going to be a breaking change we might want to rethink the solution.

@quietone regarding "The next plugins" I think that might just be a gap in translation. I'm guessing @tao meant "the following plugins"

Here's a list of all of the possibly affected plugins in Core.

I'm not sure of the best way to recreate the issue, but as @scott_earnest mentioned, there seems to be a mismatch here.

in core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php

protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {

but then in the plugins (this from core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php)

public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];

    foreach ($this->getEntitiesToView($items, $langcode) as $delta => $file) {

Sorry that's a lot... but it's all to say... I think this is "needs work", but I'm not sure what work exactly. :D

podarok’s picture

Priority: Normal » Major

+1 RTBC #3 on Drupal core 9.4.x

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.

froboy’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

I've made a change in our contributed module that I'm hoping will resolve this issue instead of needing the core patch.

Based on #2878691: Error: Recoverable fatal error: Argument 1 passed to Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView I'm hoping this is a case of misleading error messages and we'll be able to resolve it when similar situations arise, although that doesn't explain the issue raised in #10.

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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.