Problem/Motivation

When using a Search API based view the translation management seems to go haywire as in; the content is always returned in the default language.

Steps to reproduce

  1. Have a multilingual Drupal setup with path prefixes
  2. Create a search index that indexes nodes with translations
  3. Create a Search API view that uses the index and Format Show: Rendered entity
  4. Create a Content view that directly returns the nodes with Format Show: Content
  5. Expose the two views in Graphql - assumed endpoint /graphql/general

Run a query like:

query ViewsLanguageHandling {
  searchApiView: entityById(entityType: VIEW, id: "testSearchApiView") {
    ... on View {
      executable(displayId: "default") {
        ... on ViewTestSearchApiViewDefault {
          execute(
            limit: 3
          ) {
            rows {
              ... on NodeEvent {
                titleRawField {
                  first {
                    value
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  contentView: entityById(entityType: VIEW, id: "testContentView") {
    id
    ... on View {
      executable(displayId: "default") {
        ... on ViewTestContentViewDefault {
          execute(
            limit: 3
          ) {
            rows {
              ... on NodeEvent {
                titleRawField {
                  first {
                    value
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Run the query on a non default language e.g. /de/graphql/general.
The results of the two views queries will differ - the Search API one will display the content in the default language while the Content view will show the proper translation.

Proposed resolution

This seems to be cause by \Drupal\graphql_core_schema\Plugin\GraphQL\DataProducer\ViewExecutor::getEntityTranslationRenderer()
There the renderer for the rows is determined under the implicit assumption that all handlers will use the approach from \Drupal\views\Entity\Render\EntityTranslationRenderTrait.
However, Search API uses an entirely different approach for that - in fact the rows / entities seem to already have the appropriate active language set ahead of time.

For lack of a better idea I think we could introduce a type check for SearchApi to bypass the call to getEntityTranslationByRelationship() in the resolver in \Drupal\graphql_core_schema\Plugin\GraphQL\DataProducer\ViewExecutor.

The other approach would be to add the type check in \Drupal\graphql_core_schema\Plugin\GraphQL\DataProducer\ViewExecutor::getEntityTranslationRenderer() and use Search APIs \Drupal\search_api\Plugin\views\EntityTranslationRenderer() as renderer.
This would allow to keep the whole getEntityTranslationByRelationship() handling in place.

Remaining tasks

  1. ✓ Write code
  2. Write tests?
  3. Reviews

User interface changes

None

API changes

None

Data model changes

None

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

das-peter created an issue. See original summary.

das-peter’s picture

Issue summary: View changes
Status: Active » Needs review

I've pushed a modification based on the first proposed solution:

        // The search api backend uses its own language management. So only run
        // translation handling if this isn't a search api backend view.
        $row_entity = $row->_entity;
        if (!($this->view->getQuery() instanceof SearchApiQuery)) {
          $row_entity = $self->getEntityTranslationByRelationship($row->_entity, $row);
        }

I'm not overly a fan of the readability of this because of the inverted condition - but this way getEntityTranslationByRelationship() is only run if necessary and no else condition is needed.

das-peter’s picture

Assigned: das-peter » Unassigned

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

czigor’s picture

Status: Needs review » Fixed

Committed, thanks!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

  • czigor committed 17caa287 on 1.0.x authored by das-peter
    [#3528084] feat: Views Search API: Wrong language used
    
    By: das-peter
    

Status: Fixed » Closed (fixed)

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