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.
1 Answer
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); ?>
</div>
</div>
I would like to alter the way the link in the title-field is built
?