0

On a field of field type = LINK and widget = LINK, I want to add an tag on the rendered output.

The reason is to be able to add a class, not to the whole tag output, but to append a FontAwesome icon.

Hook_page_alter, of course shows all of the rendered HTML. I really don't want to mess with that.

I tried hook_entity_view_alter. That works in a clumsy fashion but two problems:

1) It only affects the output from the node displayed via a view 2) It appends the tag in new after the displayed link.

It appends the tag in new after the displayed link.

| improve this question | | | | |
1

You can theme individual fields: https://api.drupal.org/api/drupal/modules!field!field.module/function/theme_field/7

You can either have a theme function in template.php or a tpl.php template file, the former is preferred for performance reasons.

Using a theme function you could do something like:

THEMENAME_field__link($vars) {
  $output = '';
  foreach ($vars['items'] as $delta => $item) {
    $link = l($item['#title'], $item['#href']);
    $output .= '<div class="' . $vars['classes'] . '"' . $vars['item_attributes'][$delta] . '>' . $link . '<i class="fa fa-download">&nbsp;</></div>';
  }
  return $output;
}

Though you'll have to check $vars for specifics. As for checking the widget I think you can get a clue from $vars['element']['#formatter']

| improve this answer | | | | |
  • Given the configuration I had, this is the best answer. However, due to some other requirements, I finally gave in to using Display Suite (which I swore I would never do again). Side note: to my surprise, this new version of DS is a far cry from what I learned to hate with Drupal 6 and early versions for Drupal 7. It still add a significant overhead, but worth it, in this case. – sea26.2 Aug 9 '14 at 1:15
  • @user908998 I'd be reluctant to go that route as it seems like having that stuff in the db really muddies the whole separation of concerns thing but I understand sometimes you just need to get it done. – bloke_zero Aug 9 '14 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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