When generating search results that include external datasources (those that aren't Drupal entities), the code to generate the results causes a fatal error due to the fact that the entity type does not exist in Drupal.

The interface for a datasource requires a viewItem method that can take the place of the exception-causing code and generate the desired output for both entities and non-entities. A patch is attached.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ChrisGrewe created an issue. See original summary.

borisson_’s picture

Assigned: ChrisGrewe » Unassigned

I'm not sure about this. This makes the controller not use entity rendering anymore. So that would be a regression vs the current behavior.

legolasbo’s picture

Status: Active » Needs review
FileSize
846 bytes
  /**
   * {@inheritdoc}
   */
  public function viewItem(ComplexDataInterface $item, $view_mode, $langcode = NULL) {
    try {
      if ($entity = $this->getEntity($item)) {
        $langcode = $langcode ?: $entity->language()->getId();
        return $this->getEntityTypeManager()->getViewBuilder($this->getEntityTypeId())->view($entity, $view_mode, $langcode);
      }
    }
    catch (\Exception $e) {
      // The most common reason for this would be a
      // \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException in
      // getViewBuilder(), because the entity type definition doesn't specify a
      // view_builder class.
    }
    return [];
  }

It looks like the viewItem method actually does render the entity using entity rendering, but falls back to an empty array if that's not possible. The suggested change would make things more robust if that's actually the case, so let's see what testbot thinks of it, but with a correct patch this time.

legolasbo’s picture

Testbot doesn't seem to mind. What do you think borisson_?

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Testbot here isn't to be as trusted as in the Search API module, but it does look good. I forgot about viewItem's behavior.

borisson_’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

This patch no longer applies

legolasbo’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
637 bytes

Rerolled

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC, will commit over the weekend or during dev days.

  • borisson_ committed 4b9eb3e on 8.x-1.x authored by legolasbo
    Issue #2846103 by legolasbo, ChrisGrewe: Controller fails for external...
borisson_’s picture

Committed and pushed, thanks!

borisson_’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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