I am using Search API Solr and Search API attachments so my results page returns both node and file entities.

However, when a file entity is returned in the matched results, I get a "No link template "canonical" found for the "file" entity type" error because of the $entity->toUrl()->toString() call in template_preprocess_search_api_page_result.

I realize this is a core issue (see related discussions #2402533: Provide File::createFileUrl() as a replacement for the deprecated File:url() implementation and #2922487: Follow-up for #2910211: fix all deprecation warnings), but attached is a first pass at an interim patch which prevents the fatal error.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hitfactory created an issue. See original summary.

whiplashomega’s picture

I ran into this same error hitfactory, and think I have an easier fix, though my git-foo is bad. If you simply replace the line
$variables['url'] = $entity->toUrl()->toString();

with

$variables['url'] = $entity->url();

it should work. That particular method is overridden on the file entity type to return the files actual path, and for all other entities calls $entity->toUrl() within its own code.

zanvidmar’s picture

legolasbo’s picture

Status: Needs work » Needs review

Setting to the correct status.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Seems like a good solution.

borisson_’s picture

Status: Reviewed & tested by the community » Fixed

  • borisson_ committed b0b4e96 on 8.x-1.x authored by zanvidmar
    Issue #2965839 by hitfactory, zanvidmar, borisson_: Search result page...

Status: Fixed » Closed (fixed)

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

Ki’s picture

Here's checking if the entity is a paragraph.