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).
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | Screen Shot 2022-05-09 at 2.05.10 PM.png | 176.23 KB | scott_earnest |
| #3 | getentitiestoview_change_the_interface_of_the_first_argument_3138528_3.patch | 2.42 KB | tаo |
| #2 | getentitiestoview_change_the_interface_of_the_first_argument_3138528_2.patch | 1.43 KB | tаo |
| Image_090.png | 35.32 KB | tаo |
Comments
Comment #2
tаo commentedComment #3
tаo commentedComment #6
froboyI'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.
Comment #7
anpolimusApproving that patch #3 fixes the issue
Comment #8
podarok+1 RTBC
Works well for us on 9.3.9 core, PHP 8.1.4
Comment #9
quietone commentedThe 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:
.
Cheers.
Comment #10
scott_earnest commentedperhaps 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.
Comment #12
froboyThis 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 60and 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
but then in the plugins (this from core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php)
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
Comment #13
podarok+1 RTBC #3 on Drupal core 9.4.x
Comment #15
froboyI'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.