3

Is there any way to validate a taxonomy term before it will be saved? I'm using Simple Hierarchical Select with its "add new item" functionality. I would like to enable this option for registered users. They are not completely trusted though. What I would like to do is to create some limit on number of terms that can be added, for example per day. This is a ajax callback of SHS that creates new terms:

function shs_json_term_add($vid, $parent, $term_name) {
  if (!user_access('edit terms in ' . $vid)) {
    // Sorry, but this user may not add a term to this vocabulary.
    return FALSE;
  }

  $term = (object) array(
    'vid' => $vid,
    'parent' => $parent,
    'name' => check_plain(filter_xss($term_name)),
  );
  // Save term.
  $status = taxonomy_term_save($term);

  // Return term object or FALSE (in case of errors).
  return ($status == SAVED_NEW) ? array('tid' => $term->tid, 'name' => $term->name) : FALSE;
}

As you can see, it basically checks permissions and saves term by using taxonomy_term_save($term); . Would there be a way for validating this term without patching SHS module?

1
  • 3
    Almost certainly yes - the Ajax callback is just a standard callback so you can override it with hook_menu_alter and implement more advanced access logic on your own callback
    – Clive
    Dec 13, 2013 at 12:12

1 Answer 1

3

there are these lines in shs.module:

$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);

I think we can do something like this:

function MY_MODULE_json_callbacks(&$callbacks) {
  $callbacks['shs_json_term_add']['callback'] = 'CUSTOM_CALLBACK';
}

function CUSTOM_CALLBACK($vid, $parent, $term_name) {
  if(!NEW_CONDITIONS) {
    return FALSE;
  }

  return shs_json_term_add($vid, $parent, $term_name);
}
1
  • 1
    Excellent solution, that will even work if the high performance js module is installed without any extra messing about
    – Clive
    Dec 13, 2013 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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