Skip to content

[4.x] entity_reference parent's language #1149

@bachy

Description

@bachy

nested Entity loaded via entity reference field come in the default language, not in the current language, meaning the language of the parent.
I can't find a way to get the right language from entity_reference.

type Article implements NodeInterface {
  ... 
  linked_materials: [Materiau]
  ...
}
type Materiau implements NodeInterface {
  id: Int!
  uuid: String!
  title: String!
  ...
}
$registry->addFieldResolver('Article', 'linked_materials',
   $builder->produce('entity_reference')
   ->map('entity', $builder->fromParent())
   ->map('field', $builder->fromValue('field_linked_materials'))
);

where i am now with my try

$registry->addFieldResolver('Article', 'linked_materials',
      $builder->compose(
        $builder->callback(function($parent, $args){
          $linkedmaterials = $parent->get('field_linked_materials')->getValue();
          $nids = [];
          foreach ($linkedmaterials as $key => $value) {
            $nids[] = $value['target_id'];
          }
          $lang = $parent->language()->getId();
          // return [
          //   "ids" => $nids,
          //   "language" => $lang
          // ];
          return $nids;
        }),
        $builder->produce('entity_load_multiple')
          ->map('type', $builder->fromValue('node'))
          ->map('bundles', $builder->fromValue(['materiau']))
          ->map('ids', $builder->fromParent())
          ->map('language', $builder->fromValue('fr')) // hard coded language working, how can i transmit the language from the parent ?
   )
);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions