3

I am using a module Simple hierarchical select in my website.

I have a taxonomy in which I have some parent terms and there children terms, I am getting two select list with this module. In one select list I am getting all parent terms, and the module is filtering the second select list as per the parent selected, showing children terms of parent term selected in select list 1.

I need to add some condition to the values I am getting in the select lists to alter this function shs_json_term_get_children using custom module.

The function declaration in the module is:

function shs_json_term_get_children($vid, $parent = array(), $settings = array()) {}

So I tried using:

function hook_shs_json_term_get_children_alter($vid, $parent = array(), $settings = array()) {}

and

function hook_shs_json_term_get_children_alter(&$vid, $parent = array(), $settings = array()) {}

but neither worked.

Any suggestions?

2

As far as I know, you can only alter the functionality the module exposes using hooks defined by that module, and you cannot alter just any function defined in the module. For instance, shs_json_term_get_children is just a callback, and you cannot alter it.

The SHS module defines several hooks that are listed in the shs.api.php file. For instance, there is a hook called hook_shs_term_get_children_alter, that allows you to modify the list of terms displayed to the user: http://cgit.drupalcode.org/shs/tree/shs.api.php?h=7.x-1.x#n73

/**
 * Alter the list of terms within a level of the term hierarchy.
 *
 * @param array $terms
 *   List of terms displayed to the user (single hierarchy level).
 * @param array $alter_options
 *   - vid: ID of vocabulary or field name
 *   - parent: ID of parent term
 *   - settings: Additional settings (for example "language", etc.,)
 */
function hook_shs_term_get_children_alter(&$terms, &$alter_options) {
}

In your module, you would need to implement that hook like that:

function YOURMODULENAME_shs_term_get_children_alter(&$terms, &$alter_options) {
  // Your code here.
}
1

There are some wrong assumptions:

  • shs_json_term_get_children() is not a hook, but a simple callback
  • Even if it were a hook, implementing a similar hook doesn't override any other hook, except in the case of alter hooks, which are those hooks whose names end with _alter
  • Implementations of hooks are not named like hook_shs_json_term_get_children_alter() if hook is not the machine name of the module implementing it

As said in the answer for Is there any way to validate taxonomy terms? the module defines shs_json_term_get_children() as callback using the following code.

$callbacks = array(
    'shs_json_term_get_children' => array(
      'callback' => 'shs_json_term_get_children',
      'arguments' => array(
        'vid' => 'is_numeric',
        'parent' => 'is_array',
        'settings' => 'is_array',
      ),
    ),
    'shs_json_term_add' => array(
      'callback' => 'shs_json_term_add',
      'arguments' => array(
        'vid' => 'is_numeric',
        'parent' => 'is_numeric',
        'name' => 'is_string',
      ),
    ),
  );
// Let other modules add some more callbacks and alter the existing. (!)
drupal_alter('shs_json_callbacks', $callbacks);

So, if you need to completely override that callback (i.e. not rely on the values returned from that callback), you can implement code like the following.

function mymodule_json_callbacks(&$callbacks) {
  $callbacks['shs_json_term_get_children']['callback'] = 'mymodule_json_term_get_children';
}

(Replace mymodule with the machine name of the module you are implementing.)

Your callback will receive the same arguments received from the original callback.

If, instead, you just want (for example) add a few terms to the ones returned from shs_json_term_get_children(), or remove a few terms from the ones returned from that function, you implement hook_shs_term_get_children_alter().

function mymodule_shs_term_get_children_alter(&$terms, &$alter_options) {
  // Alter $terms, or $alter_options.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.