-1

I have a problem trying to link image teaser to open in a new tab when clicked. How do i go about that? I know in html is target="_blank" attribute.

1
  • You'll need to provide more context to know where (e.g. module, theme, etc). to have someone answer this.
    – Shawn Conn
    Aug 15, 2018 at 20:42

1 Answer 1

1

One way you can do this is with mytheme_preprocess_field where mytheme is the name of your theme, inside of mytheme.theme file:

/**
 * Implements hook_preprocess_field().
 */
function mytheme_preprocess_field(&$variables) {
      if ($variables['field_name'] == 'field_mylink') {
        foreach ($variables['items'] as $key => $item) {
          $variables['items'][$key]['content']['#options']['attributes']['target'] = '_blank';
        }
      }
}

This would add target="_blank" to each value entered for the link field. But you don't mention if the field is a link field or image field though.

1
  • My page has other images but I want to target this one field that has unlimited images Aug 17, 2018 at 19:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.