Under /admin/structure/taxonomy/my_vocabulary/display/entity I've got a view mode "entity". For this view mode, I would like to alter the way the link in the title field is built (the title is replaced by a real field instance due to title module): the link should be rebuilt into an anchor link like this: <a href="#term-ID>Title</a>, where term-ID is replaced with the actul term ID of the term. How can I achieve this by a custom module or by a template function? Are render arrays good for this? Here's a screenhot of the admin page. enter image description here

  • what do you mean by this I would like to alter the way the link in the title-field is built?
    – iamroald
    Commented Aug 21, 2016 at 0:23
  • i edited my previous quetsion
    – Madam Adam
    Commented Aug 21, 2016 at 6:05
  • Where does that title field comes from? Do you have a screendump of the /admin/structure/taxonomy/my_vocabulary/display page?
    – devunder
    Commented Aug 21, 2016 at 11:00
  • Screenshot is added. I think the title is automatically added (like node titles are rendered automatically, too; node titles can be hidden by using exclude node title module, but for entities this doiesn't work. see here: drupal.stackexchange.com/questions/107895/…)
    – Madam Adam
    Commented Aug 21, 2016 at 21:11
  • I am not to familiar with this also so please bear with me :). Is Beschreibung the field that is replacing the default Title? Perhaps you can use drupal.org/project/custom_formatters or otherwise api.drupal.org/api/drupal/modules%21field%21field.api.php/…
    – devunder
    Commented Aug 23, 2016 at 15:37

1 Answer 1


I finally altered the title link by using a preprocess hook and a custom field template file (and a custom view mode for my entity, done with hook_entity_info_alter()):

  function hook_preprocess_taxonomy_term(&$variables) {
  $variables['view_mode'] = $variables['elements']['#view_mode'];
  $variables['term'] = $variables['elements']['#term'];
  $term = $variables['term'];
  $uri = entity_uri('taxonomy_term', $term);
  // Edits are here:
  $variables['term_url'] = url($uri['path'], $uri['options']);
  $variables['term_name'] = check_plain($term->name);
  $variables['page'] = $variables['view_mode'] == 'full' && taxonomy_term_is_page($term);
  $variables['term_id'] = drupal_html_id($term->name);  
  // Flatten the term object's member fields.
  $variables = array_merge((array) $term, $variables);
  // Helpful $content variable for templates.
  $variables['content'] = array();
  foreach (element_children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  field_attach_preprocess('taxonomy_term', $term, $variables['content'], $variables);

  // Gather classes, and clean up name so there are no underscores.
  $vocabulary_name_css = str_replace('_', '-', $term->vocabulary_machine_name);
  $variables['classes_array'][] = 'vocabulary-' . $vocabulary_name_css;

  $variables['theme_hook_suggestions'][] = 'taxonomy_term__' . $term->vocabulary_machine_name;
  $variables['theme_hook_suggestions'][] = 'taxonomy_term__' . $term->tid;

My taxonomy-term.tpl.php:

<div<?php print $attributes; ?>>
  <?php if (!$page && ($view_mode != 'subcategory_anchor')): ?>
    <h2><a href="<?php print $term_url; ?>"><?php print $term_name; ?></a></h2>
  <?php endif; ?>

  <?php if (!$page && ($view_mode == 'subcategory_anchor')): ?>
    <h2 id="<?php print $term_id ?>"><?php print $term_name; ?></h2>
  <?php endif; ?>

  <div<?php print $content_attributes; ?>>
    <?php print render($content); ?>

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.