After creating a view for a search index which tracks an entity that has no bundles, I noticed that I couldn't select a view mode for the rendered row.

The bug boils down to the logic in src/Plugin/views/row/SearchApiRow.php: whether or not the entity definition has bundles, buildOptionsForm will call $datasource->getViewModes($bundle_id); , which will not work.

Not quite sure how to define a test for this, but IEF has to do something similar: https://www.drupal.org/node/2569193

Comments

bc created an issue. See original summary.

bc’s picture

StatusFileSize
new4.86 KB

I've created a stopgap patch to fix things in the meantime. I made Drupal\search_api\Plugin\search_api\datasource\ContentEntity::hasBundles public, and then I modified buildOptionsForm to only include the bare entity's view modes if it doesn't have any bundles.

SearchApiRow::render will check if $this->options['view_modes'][$datasource_id][$bundle] is set, but $bundle in this case is named in a different way than I created it above. There's a convention for creating a fake bundle name.. maybe I could use that here & the render method can expect that. What happens if an entity with bundles has a name that collides with the fake bundle name? Maybe we shoudl test for hasBundles here -- that would be more direct :)

drunken monkey’s picture

Component: Views integration » Plugins
Status: Active » Needs review
StatusFileSize
new2.47 KB
new3.06 KB

Thanks for reporting this problem! Seems we didn't think about that.
How about this for a solution? Doesn't seem necessary to have any other parts of the code know about the peculiars of the entity/bundle system.
Or does this not completely solve the problem, would we maybe still need to adapt SearchApiRow?

Testing proper integration for this in the Views UI would be great, but I fear there is no simple way to do that, so it would be much too much effort. (If you want to try it, though, go ahead – would be great to have.)
However, at least we can easily test this on the low level, to see whether the datasource behaves correctly.

joachim’s picture

Weird. I'm using a custom entity type which has no bundles defined, and I'm able to select a view mode for it.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

The new test looks great.

  • drunken monkey committed d60226c on 8.x-1.x
    Issue #2725903 by drunken monkey, bc: Fixed view modes reported for...

  • drunken monkey committed 2081c5a on 8.x-1.x
    Follow-up to #2725903 by drunken monkey, bc: Fixed view modes reported...
drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Seems I forgot to set the "tests only" patch to be tested – but verified that it fails locally now.
Thanks for reviewing!
Committed.

Status: Fixed » Closed (fixed)

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