Score:0

Unsuccessfully trying to get to the correct target_id

tw flag

So I've been programming a module that needs to show different pictures depending on the language code of the user (en vs. fa). For that I'm trying to get to the right target_ids. Debug($nodes) shows me the following:

        [field_picture_active] => Array
            (
                [x-default] => Array
                    (
                        [0] => Array
                            (
                                [target_id] => 209
                                [alt] => Introdution
                                [title] => 
                                [width] => 320
                                [height] => 320
                            )

                    )

                [fa] => Array
                    (
                        [0] => Array
                            (
                                [target_id] => 241
                                [alt] => test222
                                [title] => 
                                [width] => 320
                                [height] => 320
                            )

                    )

            )

The correct target_id would be 241 under [fa] but when I try the following ...

foreach ($nodes as $node) {
   $image_field = $node->get('field_picture_active')->first()->getValue();
   $fid = $image_field['target_id'];
}

... and then attempt to load the pictures for each nodes (or do debug($fid)), I just get the target_ids for each node that appear under [x-default] instead of [fa]. How can I access the target_ids under [fa]? Every other translation works perfectly fine, just those target_ids are not correct.

Any thoughts on this will be highly appreciated. Thank you very much.

Score:1
gb flag

If you want the value for a specific language, you should use the translated node. The following code should work

foreach ($nodes as $node) {
  if ($node->hasTranslation('fa')) {
    $node = $node->getTranslation('fa');
  }
  $image_field = $node->get('field_picture_active')->first()->getValue();
  $fid = $image_field['target_id'];
}

If there is no translation, the default value will be taken.

Quetzcodecoatl avatar
tw flag
Works like charm, thanks very much, good sir! With this, I even can combine the if-statement with the preferred user language code (or the site language code). Gorgeous!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.