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
- Have a multilingual Drupal setup with path prefixes
- Create a search index that indexes nodes with translations
- Create a Search API view that uses the index and Format
Show: Rendered entity - Create a Content view that directly returns the nodes with Format
Show: Content - 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
- ✓ Write code
- Write tests?
- Reviews
User interface changes
None
API changes
None
Data model changes
None
Issue fork graphql_core_schema-3528084
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
Comment #2
das-peter commentedI've pushed a modification based on the first proposed solution:
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.Comment #4
das-peter commentedComment #6
czigor commentedCommitted, thanks!