Hi,

I need the description of the selected term to be displayed under the dropdown. Any code suggestion would be appreciated.
Thanks

Comments

solipsist’s picture

Been playing around with a workable solution. I haven't tested it but it seems to work. It requires modifying the module (see attached patch against current recommended release, not head mind).

SHS makes an AJAX call to retrieve terms. The resulting response doesn't include descriptions, but SHS allows you to hook in so that can be fixed. What does need hacking is making SHS provide hooks of a sort by providing JavaScript events ($.triggerHandler) we can use to trigger our own code.

This is not a finished solution, or particularly pretty, but it's something you can work with, I hope.

Put this in a custom module:

<?php
/**
 * SHS shs_term_get_children_alter()
 * Recursively add description
 */
function sitename_custom_shs_term_get_children_alter(&$terms, $context) {
  $keys = array_keys($terms);
  $root_vid = $keys[0];
  $fullterms = taxonomy_get_tree($root_vid, 0);
  foreach($fullterms as $key => $term) {
    $keyedterms[$term->tid] = $term;
  }
  recursiveTermUpdate($terms, $keyedterms);
}

function recursiveTermUpdate(&$terms, $fullterms) {
  foreach($terms as $tid => &$term) {
    if (is_array($term)) {
      recursiveTermUpdate($term, $fullterms);
    }
    else {
      $term = array('name' => $term, 'tid' => $tid, 'description' => $fullterms[$tid]->description);
    }
  }
  return $terms;
}


function sitename_custom_shs_json_callbacks_alter(&$callbacks) {
  // hijack the callback for getting terms, allowing us to alter the json payload
  $callbacks['shs_json_term_get_children'] = array(
    'callback' => 'sitename_custom_shs_json_term_get_children',
    'arguments' => array(
      'vid' => 'is_numeric',
      'parent' => 'is_array',
      'settings' => 'is_array',
    ),
  );
}




/**
 * JSON callback to get the list of children of a term.
 *
 * @param <int> $vid
 *   ID of vocabulary the term is associated to.
 * @param <int> $parent
 *   ID of parent term.
 * @param <array> $settings
 *   Additional settings (for example "display node count").
 *
 * @return <array>
 *   Associative list of child terms.
 *
 * @see shs_term_get_children()
 */
function sitename_custom_shs_json_term_get_children($vid, $parent = array(), $settings = array()) {
  $scope = $result = array();
  foreach ($parent as $tid) {
    $scope[] = shs_term_get_children($vid, $tid, $settings);
  }

  // Rewrite result set to preserve original sort of terms through JSON request.
  foreach ($scope as $terms) {
    foreach ($terms as $tid => $term) {
      $result[] = array(
        'tid' => $tid,
        'label' => $term['name'],
        'description' => $term['description']
      );
    }
  }

  return $result;
}

Add this to a custom JavaScript file:

Drupal.behaviors.shsCustom = { 
  attach : function(context, settings) {
    jQuery('#edit-shs-term-node-tid-depth-wrapper:not(".shscustom-processed")', context).after(jQuery('<div class="shscustom-term-description"><!--Empty--></div>')).addClass('shscustom-processed');
    jQuery('.shscustom-term-description').html(shsCustom.currentDescription);
  }
};

var shsCustom = {
  cache : []
};

jQuery(document).bind('shsElementChanged', function(event, tid) {
  shsCustom.currentDescription = typeof shsCustom.cache[tid] == 'undefined' ? '' : shsCustom.cache[tid];
});

jQuery(document).bind('shsGetTermChildren', function(event, data) {
  jQuery.each(data.data, function(index, obj) {
    if (typeof obj.description == "string") {
      shsCustom.cache[obj.tid] = obj.description;
    }
  });
});

Use the this patch to make the necessary changes to shs.js:

diff --git a/sites/all/modules/contrib/shs/js/shs.js b/sites/all/modules/contrib/shs/js/shs.js
index 28cea53..db04ae6 100644
--- a/sites/all/modules/contrib/shs/js/shs.js
+++ b/sites/all/modules/contrib/shs/js/shs.js
@@ -118,6 +118,7 @@
       },
       success: function(data) {
         if (data.success == true) {
+          $(document).triggerHandler('shsGetTermChildren', [data]);
           if ($element.prop) {
             var options = $element.prop('options');
           }
@@ -335,6 +336,7 @@

     // Set value of original field.
     updateFieldValue($triggering_element, base_id, level, settings.multiple);
+    $(document).triggerHandler('shsElementChanged', [$triggering_element.val()]);
   }

   /**
setki’s picture

Did somebody tried this?
I can't get it to work at my side. Still displaying "name" iso "description"

Or can somebody make a "shsd"-module with this implemented?

torgormack’s picture

Issue summary: View changes

Sorry I'm a bit late to this party - but has anyone managed to get this to work or is there a module?

stBorchert’s picture

Status: Active » Closed (won't fix)

Sorry, but this isn't possible out-of-the box with this version.

velocis’s picture

I needed this quite quickly, so dirty hack would be to mod line 599 of the shs.module. (Would be nice to see this is an option that can be turned on and off as needed).

From

$terms[$vid][$parent][0][$term->tid] = $term->name;

To

$terms[$vid][$parent][0][$term->tid] = $term->name . (!empty($term->description) ? " " . drupal_html_to_text($term->description,  NULL) : "" );