Open bachy opened 3 years ago
ok, i came up with this solution
$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();
$test="test";
return [
"ids" => $nids,
"language" => $lang
];
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('bundles', $builder->fromValue(['materiau']))
->map('ids', $builder->callback(function($parent, $args){
return $parent['ids'];
}))
->map('language', $builder->callback(function($parent, $args){
return $parent['language'];
}))
)
i also tried that, but entity_reference does not seems to understand the "language" parametre
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_linked_materials'))
->map('language', $builder->fromValue('fr')) // this is not working
// ->map('language', $builder->produce('entity_language')
// ->map('entity', $builder->fromParent())) // neither that
I noticed the same issue. Pull request #974 should sort out the issue but it has been open for almost a year now.
What is the status of this PR? Is it possible to pick up and work on remaining issues? I'd be happy to do so if it's possible.
Also running into this with entity reference on translated taxonomy. Also using version 4. @bachy example works but if a term is not translated it will error so this is what I got working for now to reference translated taxonomy terms or return the source term if not translated.
$builder->compose(
$builder->callback(function($parent, $args){
$memberRatings = $parent->get('field_member_rating')->getValue();
$tids = [];
foreach ($memberRatings as $key => $value) {
$tids[] = $value['target_id'];
}
$terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);
$language = $parent->language()->getId();
$termList = [];
foreach($terms as $term) {
if($term->hasTranslation($language)){
$translated_term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language);
$termList[] = $translated_term;
} else {
$termList[] = $term;
}
}
return $termList;
}),
)
Not sure if this is how chaining the data producers is intended to work but was trying something like this without success.
$builder->compose(
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_member_rating')),
$builder->produce('entity_translations')
->map('entity', $builder->fromParent())
->map('language', $builder->fromParent()),
),
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.
where i am now with my try