1

In my views I can do this easily using the rewrite results and writing this line

<span class="glyphicon glyphicon-earphone list-icon"></span>  [field_business_phone]

How can I do the same with the node page. It doesn't necessarily have to be a rewrite method but I am looking for an easy method of doing this. The end result I am looking for is something like this. Views fields with icons

2

Use the preprocess function, like:

function YOUR_THEME_preprocess_field(&$variables) {
  $element = $variables['element'];

  if (isset($element['#field_name'])) {
    if($element['#field_name'] == 'field_business_phone') {
      foreach ($variables['items'] as $k => $value) {
        $variables['items'][$k]['#markup'] = '<span class="glyphicon glyphicon-earphone list-icon"></span>' . $variables['items'][$k]['#markup'];
      }
    }
  }
}

Or overwrite the field templates, in a file named field--field-business-phone.tpl.php, containing something like:

<?php

<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php if (!$label_hidden): ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>
  <div class="field__items"<?php print $content_attributes; ?>>
    <?php foreach ($items as $delta => $item): ?>
      <div class="field__item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><span class="glyphicon glyphicon-earphone list-icon"></span> <?php print render($item); ?></div>
    <?php endforeach; ?>
  </div>
</div>
  • Well...simple as that I guess...I can't understand most of the code provided due to my lack of php and drupal api knowledge...but it did the trick. Thanks a lot. – pierostz Aug 8 '16 at 13:08
  • I did actually but I don't have enough reputation I guess. – pierostz Aug 8 '16 at 13:34
  • A strange thing is happening I use the code below to show the icons but the website field doesn't show it. I double checked the name of the field and cleared the caches. Do you have any clues why this is happening and how I can troubleshoot? if($element['#field_name'] == 'field_business_website') { foreach ($variables['items'] as $k => $value) { $variables['items'][$k]['#markup'] = '<span class="glyphicon glyphicon-link list-icon"></span>' . $variables['items'][$k]['#markup']; } } – pierostz Aug 9 '16 at 6:50
  • See if the code get's hit. place a drupal_set_message('hit: ' . $element['#field_name']); just before the if statement, and you should see all field names from all fields that get preprocessed. – rémy Aug 9 '16 at 7:29
  • Strangely it does get hit. hit: field_business_website . No markup (span) is added though. – pierostz Aug 9 '16 at 7:53

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.