Score:0

How can I set the label of a multiple image field programmatically

in flag

I have an image field with multiple images, field_sponsors.

I need to be able to set the label of this field programatically. The default label on the field is set to "Sponsors", but sometimes it needs to be labelled differently on output.

I've tried this in my .theme file in the preprocess_node function, but it does not work.

$node->set('field_sponsor', array(
      'label' => 'BATMAN!',
      ));

I don't want to do it in twig because I have 5 different fields to do this for and I need to do multiple checks to determine the correct output text.

cn flag
You want to be changing the render array that contains the label aka `$variables` that is passed by reference, not the actual node object. Within $variables you will find the render array for your field and the label for it.
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.