2

So I'm preprocessing a field: preprocess_field and trying to add attributes to a Link. I've tried:

$variables['items'][0]['attributes']['target'] = '_blank'; $variables['attributes']['target'] = '_blank'; $variables['#attributes']['target'] = '_blank'; $variables['content_attributes']['target'] = '_blank'; $variables['content_attributes']['attributes']['target'] = '_blank'; $variables['items'][0]['#attributes']['target'] = '_blank'; $variables['items'][0]['options']['attributes']['target'] = '_blank'; $variables['items'][0]['#options']['attributes']['target'] = '_blank'; $variables['items'][0]['#options']['target'] = '_blank'; $variables['items'][0]['#content']['#options']['attributes']['target'] = '_blank'; $variables['items'][0]['#content']['options']['attributes']['target'] = '_blank'; $variables['items'][0]['#options']['attributes']['target'] = '_blank';

A silly amount of attempts, none of these worked. How can I add attributes to a link?

1 Answer 1

1

There were some close ones, but this is the correct way to set an attribute to the first link:

function mymodule_preprocess_field(&$variables) {
  if ($variables['field_type'] == 'link') {
    $variables['items']['0']['content']['#attributes']['target'] = '_blank';
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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