Score:2

How get the view mode\third_party_settings of a field inside hook_preprocess_image_formatter(&$variables)

br flag

I'm on D9.

I've added a custom option to the image field formatter following Drupal guidelines

In the preprocess of the field I can get the settings I've created:

function my_module_preprocess_field(&$variables) {
  if ($variables["element"]["#formatter"] === 'image') {
    $entity = $variables['element']['#object'];
    $view_mode = $variables['element']['#view_mode'];
    $field_name = $variables['element']['#field_name'];

    $entity_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
    $field_display = $entity_display->getComponent($field_name);

    $variables['my_settings'] = isset($field_display["third_party_settings"]["my_module"]["my_settings"]) && $field_display["third_party_settings"]["my_module"]["my_settings"];
  }
}

However, the field.html.twig file is not good for my need: I have to pass this settings to the image-formatter.html.twig file, because I need to place a div right after the img tag and not outside the a.

Unfortunately, I can't get that information in the preprocess of the image formatter, because I can't find a way to get the view mode:

function my_module_preprocess_image_formatter(&$variables) {
  $item = $variables['item'];
  $entity = $item->getEntity();
  $field = $item->getFieldDefinition();

  //todo how get the view_mode\the third_party_settings?
  $entity_display = EntityViewDisplay::collectRenderDisplay($entity, $missing_view_mode);
}

Is that possible?

EDIT:

Thanks 4k4 answer, what I've done:

  1. Added inside the preprocess field function:
    if (!empty($variables['items'])) {
      foreach ($variables['items'] as &$item) {
        $item['content']['#item_attributes']['my_settings'] = $my_settings;
      }
    }

And then added a image preprocess function in order to have that info as variable and not attribute - more a preference than a strict necessity.

function my_module_preprocess_image(&$variables) {
  $variables['my_settings'] = $variables["attributes"]["my_settings"] ?? FALSE;
  unset($variables["attributes"]["my_settings"]);
}
leymannx avatar
ne flag
Drupal 7, but may still be relevant: https://drupal.stackexchange.com/a/39508/15055
Score:2
cn flag

I would add the view mode or the third party setting to the image attributes. Then it is available in both nested templates, image_formatter and image. You can remove it after you've stored it in a local variable, if you don't want that the attribute is rendered. But normally no one cares about an additional attribute.

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.