1

I just cant figure it out. How can change the title output of a term? I have a content type which displays the selected terms. The term is displayed as a link. How can I change this:

<div class="field-item even">
    <a href="/category/fe-sub">FE-Sub</a>
</div>

to something like this:

<div class="field-item even">
    <a href="/category/fe-sub">
        <strong>FE-Sub</strong>
    </a>
</div>

Edit: I tried it out with mymodule_preprocess_field(&$variables) but you may not enter html, it will print "-Element

function proposals_preprocess_field(&$variables) {
    if (isset($variables) && isset($variables['items']['0']['#options']['entity_type']) && $variables['items']['0']['#options']['entity_type'] == 'taxonomy_term') {
        $variables['items']['0']['#title'] = '<b>'.$variables['items']['0']['#title'].'</b>';
    }
}

renders link text:

<strong>term title</strong> instead of term title

EDIT: here is how i made it work: in your template.php:

function yourtheme_field__taxonomy_term_reference($variables) {
  $output = '';

  // Render the label, if it's not hidden.
  if (!$variables['label_hidden']) {
    $output .= '<h3 class="field-label">' . $variables['label'] . ': </h3>';
  }

  // Render the items.
  $output .= ($variables['element']['#label_display'] == 'inline') ? '<ul class="links inline">' : '<ul class="links">';

  foreach ($variables['items'] as $delta => $item) {
    // allow html
    $item['#options']['html'] = TRUE;
    // set html
    $item['#title'] = '<span class="label label-info">'.$item['#title'].'</span>';

    $output .= '<li class="taxonomy-term-reference-' . $delta . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</li>';
  }
  $output .= '</ul>';

  // Render the top-level DIV.
  $output = '<div class="' . $variables['classes'] . (!in_array('clearfix', $variables['classes_array']) ? ' clearfix' : '') . '"' . $variables['attributes'] .'>' . $output . '</div>';

  return $output;
}
1

To make changes to the taxonomy term out put you can use the below function. This is taken from the batrik theme so you would use the function mytheme_field__taxonomy_term_reference

To change the markup of the term change #markup the defualt value would be

<a href="/drupal7/class/class-1" typeof="skos:Concept" property="rdfs:label skos:prefLabel">term name</a>

change that to

 $item[#markup] = '<a href="/drupal7/class/class-1" typeof="skos:Concept" property="rdfs:label skos:prefLabel"><strong>term name</strong></a>'

function batrik_field__taxonomy_term_reference($variables) {
  $output = '';

  // Render the label, if it's not hidden.
  if (!$variables['label_hidden']) {
    $output .= '<h3 class="field-label">' . $variables['label'] . ': </h3>';
  }

  // Render the items.
  $output .= ($variables['element']['#label_display'] == 'inline') ? '<ul class="links inline">' : '<ul class="links">';
  foreach ($variables['items'] as $delta => $item) {
    $output .= '<li class="taxonomy-term-reference-' . $delta . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</li>';
  }
  $output .= '</ul>';

  // Render the top-level DIV.
  $output = '<div class="' . $variables['classes'] . (!in_array('clearfix', $variables['classes_array']) ? ' clearfix' : '') . '"' . $variables['attributes'] .'>' . $output . '</div>';

  return $output;
}
9
  • I am not using it with views
    – Alex
    Jan 24 '13 at 15:27
  • Where do you want to change it? in the node or taxonomy page? Jan 24 '13 at 15:34
  • On the node. Even when using views I can only seem to change the wrapper html, not the html inside the <a> since there is no replacement pattern like "term url"
    – Alex
    Jan 24 '13 at 15:51
  • with in view you dont have to use any replacement. just do what I mentioned in the answer. I am getting the info for the other query. Jan 24 '13 at 15:53
  • no, i am afraid you are wrong. "customize field html" does get wrapped around the title, but not if the term is being formatted as a link. then the "customize field html" element is being wrapped around the <a> tag, not the title directly.
    – Alex
    Jan 24 '13 at 15:58

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.