0

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).

0

2 Answers 2

1
+50

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 => $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
    if(!count(taxonomy_get_children($item['#options']['entity']->tid))){
      $line .= flag_create_link('hobbies_and_interests', $item['#options']['entity']->tid);
    }

    $line .= '</span>';
    $items[] = $line;
  }
  $output .= implode($separator, $items);
  return $output;
}
2
  • The $level variable does give the "depth" of the term (0 for root level, 1 for root+1, etc.) However, as my examples suggest, the deepest level is different for different terms. Some terms have no children and exist at the root level, while other terms have children several levels deep. Thus, knowing the depth ($level) alone is not sufficient to check if the term has no children. Commented Feb 2, 2013 at 14:36
  • answer amended.
    – 2pha
    Commented Feb 2, 2013 at 15:07
1

You could check if it has no children, By using taxonomy_get_children($tid, $vid):

$tid = $item['#options']['entity']->tid;
$vid = $item['#options']['entity']->vid;
$children = taxonomy_get_children($tid, $vid);
if (empty($children)) {
  // Print flag link here.
}

If there is no child it will be empty, It does also caching itself. Look at documentation here on drupal APIs.

1
  • There is an error in $children = taxonomy_get_children($tid); you are missing $vid parameter it should be $children = taxonomy_get_children($tid, $vid);
    – Alex
    Commented Nov 6, 2017 at 18:00

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.