Problem/Motivation

Search API doesn't support letting computed fields use the EntityView handler for rendering.

@See `_search_api_views_get_field_handler_for_property` function in `search_api.views.inc`

  // Since the Views EntityField handler class doesn't support computed fields,
  // neither can we (easily), so keep the fallback handler as the only
  // definition for those.
  if (isset($definition)
      && $property instanceof FieldItemDataDefinition
      && !$property->isComputed()
      && !$property->getFieldDefinition()->isComputed()) {

It seems like this may no longer be the case, however, issue #2852067: Add support for rendering computed fields to the "field" views field handler has been fixed. Is there a reason this logic still exists in the search api handlers?

I can see in issue #2981047: Allow adding computed bundle fields in Views that this has only been resolved for entity base fields and not bundle fields, but perhaps there is some better logic that we can perform to identify this?

This has been tested with datetime computed fields, and removing the 2 `isComputed()` checks doesn't pose any obvious issues so far in my testing.

Steps to reproduce

  1. Install Drupal 9.5.x
  2. Install Search API
  3. Add a computed datetime base field to any entity
  4. Build a search index from this entity using the computed field
  5. Try to render this field using Drupal's standard EntityView date formatters

Proposed resolution

Enable computed fields to use the EntityView handler for the given type (at least for base fields).

Remaining tasks

  • Work out which computed fields this can apply to
  • Apply the new logic

Issue fork search_api-3353719

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

kalpaitch created an issue. See original summary.

drunken monkey’s picture

Component: General code » Views integration
Category: Bug report » Feature request
Priority: Major » Normal
Status: Active » Needs work
Issue tags: +Needs tests

Thanks a lot for reporting this problem and providing a fix!

To make sure that this is indeed an improvement, though, we’ll need test coverage for this functionality, to make sure this works correctly both for computed base and bundle fields.

Also, please do not use issue forks/merge requests in this project, as testing doesn’t work for them. (See #3190024: Problem with test dependencies when testing issue forks.)

(Also, this is neither major nor a bug.)

lendude made their first commit to this issue’s fork.

nickdjm’s picture

I have experimented with removing the computed checks all together (with a bundled computed field) and it seems to work just fine.

Is there any specific concerns with removing the checks for computed fields all together?

nickdjm’s picture

For reference here is the change I made. I didn't want to over-take any of the existing MR stuff, so I just made a patch for now.

nickdjm’s picture

Status: Needs work » Needs review