Problem/Motivation

An error occurs when attempting to view an entity route for an entity that doesn't implement hasField.

Steps to reproduce

View an entity route for an entity without hasField. In my instance, it was viewing a Search API index. The error produced was:

Error: Call to undefined method Drupal\search_api\Entity\Index::hasField() in /var/www/html/web/modules/contrib/easy_breadcrumb/src/TitleResolver.php

Proposed resolution

Remaining tasks

Test for hasField on the entity before using it

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

pianomansam created an issue. See original summary.

pianomansam’s picture

Status: Active » Needs review
StatusFileSize
new680 bytes

Here is a small patch that tests for hasField before attempting to use it

pianomansam’s picture

StatusFileSize
new714 bytes

Another attempt at creating the patch

greg boggs’s picture

Looks like a reasonable work around for a Search API Bug, but we should probably fix this in Search API too.

pianomansam’s picture

@Greg Boggs is it a bug with Search API? From the sounds of it, not all entities routes will be for fieldable entities. The hasField method seems to imply it's a fieldable entity, which might not be true.

pianomansam’s picture

Title: TitleResolver relies on $entity->hasField which isn't present on all entities » TitleResolver checks for alternative field on non-fieldable entities
StatusFileSize
new1.05 KB

@Greg Boggs I read up more about this, and this isn't a problem with Search API. It's a bug here in easy_breadcrumb. In looking for the alternative field on the current routes entity, the code currently evaluates any entity route. But not all entity routes are fieldable entities. Trying to use hasField on non-fieldable entities (like Search API indexes) causes the error.

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...

I've added a new patch that intelligently looks to see if the route's entity is a FieldableEntityInterface entity before using hasField.

greg boggs’s picture

That looks perfect. Thank you for figuring this out! Can you open a merge request?

pianomansam’s picture

@Greg Boggs merge request created

  • soltz committed 2f28055 on 2.x
    only allow TitleResolver to check for alternative field on fieldable...

Greg Boggs credited soltz.

greg boggs’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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