PHP error on Search API views

Created on 11 June 2024, 7 months ago
Updated 11 July 2024, 6 months ago

Problem/Motivation

Panopoly Magic adds the ability to configure display type and view modes for views blocks when placed in a layout. When the view is built on a Search API index, there's a fatal PHP error.

Steps to reproduce

  1. Install the latest 3.0.x-dev of Panopoly with its demo content.
  2. Add a new display to the Panopoly Search (Database) view by cloning the existing Block display.
  3. In the Block Settings section, edit the Allow Settings and check the box for Display type.
  4. Save the view.
  5. Create a new landing page.
  6. Edit the layout of the page. Add your new Views block to the page.
  7. Note the block is not added successfully.
  8. Check the Recent Log Messages for PHP errors and note the error, see example below.

Error: Call to a member function id() on bool in Drupal\panopoly_magic\Plugin\views\display\MagicBlock->buildBlockFormDisplayType() (line 318 of /panopoly_magic/src/Plugin/views/display/MagicBlock.php).

The error is happening because Search API views don't have a base entity type, so they return FALSE for $this->view->getBaseEntityType().

Proposed resolution

It may be possible to use a different function to find out which types of entities are being indexed for the Search API index that's used as the base of the view. (As far as I know, you can't include more than one entity type in an index.) A stopgap would be to say that if there's no base entity type, we assume that it's nodes and go from there.

Remaining tasks

Determine best approach; patch and test.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Magic

Created by

πŸ‡ΊπŸ‡ΈUnited States cboyden

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @cboyden
  • Status changed to Needs review 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States cboyden

    Here's a patch with a proof of concept that is working on a Panopoly child distribution

  • Status changed to Needs work 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States cboyden

    As it turns out Search API can have multiple types of entities as data sources in the same index, so it's not feasible to just assume that everything is a node. And if there are multiple data sources, they won't necessarily all have the same view modes configured. Maybe there's a way to query the search index to see if there are multiple data sources (in which case, switching view modes maybe shouldn't be allowed) or only one (in which case we need the entity type just as if it were a regular view).

  • πŸ‡ΊπŸ‡ΈUnited States cboyden

    This new patch is a total kludge but it does manage to get the correct view modes for a Search API view that uses a single datasource.

  • πŸ‡ΊπŸ‡ΈUnited States dsnopek USA

    I've spent a little time thinking about this, and I don't think we should try to make the MagicBlock try to work as if the View was using the underlying entity type, given that Search API indexes can hold multiple entities types. I think that MagicBlock should only be able to work on Views of a single particular entity type.

    So, I think we should focus on fixing the fatal error:

    The error is happening because Search API views don't have a base entity type, so they return FALSE for $this->view->getBaseEntityType().

    If that function returns FALSE, then MagicBlock shouldn't allow changing the display type.

    Ideally, it'd be great if we could remove the "Display Type" option when configuring a view that isn't on an entity type, so that site builders can't even enable this option for a View that it wouldn't work on.

  • Status changed to Needs review 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States cboyden

    Here's a minimal patch that should keep Views blocks without an entity base table from being able to change display type. I wasn't able to figure out a way to hide the option in the Views UI, so Views can still be configured to allow changing display type, but the options won't be available on the block config if there's no base table.

  • πŸ‡ΊπŸ‡ΈUnited States dsnopek USA

    Thanks! It works great in my testing :-)

  • Status changed to RTBC 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dsnopek USA
  • Status changed to Fixed 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dsnopek USA
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024