Problem/Motivation

We are getting an error when adding blocks. There is an error in the browser console, "An AJAX HTTP error occurred". Drupal logs have:

TypeError: Drupal\ui_patterns_field_formatters\Plugin\Field\FieldFormatter\ComponentFormatterBase::findEntityBundleWithField(): Argument #1 ($entity_type_id) must be of type string, null given, called in …/ui_patterns/modules/ui_patterns_field_formatters/src/Plugin/Field/FieldFormatter/ComponentFormatterBase.php on line 223 in Drupal\ui_patterns_field_formatters\Plugin\Field\FieldFormatter\ComponentFormatterBase->findEntityBundleWithField() (line 174 of …/ui_patterns/modules/ui_patterns_field_formatters/src/Plugin/Field/FieldFormatter/ComponentFormatterBase.php)

Steps to reproduce

In CKEditor, click on the Block button. When "Select content block to embed" appears, choose a block and click "Select blocks". Check the browser console and logs for error messages.

There may be things that are specific to our setup that cause this to happen. However, looking at the code, ComponentFormatterBase::findEntityBundleWithField() requires that its first param is a string. It is called in ::getComponentSourceContexts(). The first param comes from the return value of BaseFieldDefinition::getTargetEntityTypeId(). That function does not promise to always return a string. It may also return NULL, which is what is happening here.

Proposed resolution

Check for a NULL return from BaseFieldDefinition::getTargetEntityTypeId() and handle the case where the value is NULL.

Remaining tasks

Implement.

User interface changes

None.

API changes

None.

Data model changes

None.

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

liam morland created an issue. See original summary.

just_like_good_vibes’s picture

Assigned: Unassigned » just_like_good_vibes

just_like_good_vibes’s picture

Assigned: just_like_good_vibes » Unassigned
Status: Active » Needs review

Hello, thank you for reporting.

When i read for example FieldDefinitionInterface in drupal core, in the comments :
"Returns the ID of the entity type the field is attached to.", "@return string".
But it is not in the signature, only in the comments.

That sounds like a very odd and strange case, where a field formatter would not have an entity_type_id to which it is attached.
i tried to patch the function and treat the case of entity_type_id being null, but my guess is that something else won't work at another place.

please review

liam morland’s picture

This patch fixed the problem for us. Thanks!

just_like_good_vibes’s picture

Title: TypeError: ::findEntityBundleWithField(): $entity_type_id must be of type string, null given » [2.0.3] TypeError: ::findEntityBundleWithField(): $entity_type_id must be of type string, null given
Assigned: Unassigned » just_like_good_vibes
Status: Needs review » Reviewed & tested by the community

thanks, i will merge

just_like_good_vibes’s picture

Assigned: just_like_good_vibes » Unassigned
Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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