On my site, users can add terms to their user profiles via a taxonomy field. They can add multiple terms with multiple depths using the hierarchical select widget, like this:
- Trees -> Fruit trees -> Apple trees
- CMS Software -> Drupal
These terms are displayed on the user profile page using a view with the hierarchical select formatter.
I have a flag that allows users to flag taxonomy terms. I want to print a flag link after the last term on each line, like this:
- Trees -> Fruit trees -> Apple trees (flag me)
- CMS Software -> Drupal (flag me)
The code for this is in hierarchical_select/modules/hs_taxonomy.module
:
function theme_hs_taxonomy_formatter_lineage($variables) {
$output = '';
$lineage = $variables['lineage'];
$separator = theme('hierarchical_select_item_separator');
// Render each item within a lineage.
$items = array();
foreach ($lineage as $level => $item ) {
dpm($item);
$line = '<span class="lineage-item lineage-item-level-' . $level . '">';
$line .= drupal_render($item);
//this adds a flag link to every term, not just the last term
$line .= flag_create_link('hobbies_and_interests', $item['#options']['entity']->tid);
$line .= '</span>';
$items[] = $line;
}
$output .= implode($separator, $items);
return $output;
}
As I commented in the code above, currently I am showing a flag link for every term, not the last term in each row. This output (which I don't want) looks like this:
- Trees (flag me) -> Fruit trees (flag me) -> Apple trees (flag me)
- CMS Software (flag me) -> Drupal (flag me)
This is ugly; I just want the "flag me" link to appear at the end for the final term in each line. However, I don't know how to target only the last term in each row. I tried using end() but $output appears to be a string, not an array, so I'm lost at how to proceed (the answer may be obvious, but I'm a PHP novice).