How can I show a flag link only for the "deepest" terms for a field with multiple values using hierarchical select? 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 =&gt; $item ) {
    dpm($item);
    $line  = '&lt;span class="lineage-item lineage-item-level-' . $level . '"&gt;';
    $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']-&gt;tid);

    $line .= '&lt;/span&gt;';
    $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).
Have you tried checking the $level variable? I have never used this module but the fact that there is a variable named $level suggests to me that this could be checked to only apply your flag link to the last 'level'.

edit in relation to comment:

Try this.

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 =&gt; $item ) {
    dpm($item);
    $line  = '&lt;span class="lineage-item lineage-item-level-' . $level . '"&gt;';
    $line .= drupal_render($item);
    //this adds a flag link to every term, not just the last term
    if(!count(taxonomy_get_children($item['#options']['entity']-&gt;tid))){
      $line .= flag_create_link('hobbies_and_interests', $item['#options']['entity']-&gt;tid);
    }

    $line .= '&lt;/span&gt;';
    $items[] = $line;
  }
  $output .= implode($separator, $items);
  return $output;
}