You are here

function shs_json_term_add in Simple hierarchical select 7

Adds a term with ajax.

Parameters

string $token: Custom validation token.

int $vid: ID of vocabulary to create the term in.

int $parent: ID of parent term (0 for top level).

string $term_name: Name of new term.

string $field: Name of field requesting the term list (DOM element name).

Return value

mixed Array with tid and name or FALSE on error.

1 string reference to 'shs_json_term_add'
shs_json_callbacks in ./shs.module
Get a list of supported JSON callbacks.

File

./shs.module, line 1039
Provides an additional widget for term fields to create hierarchical selects.

Code

function shs_json_term_add($token, $vid, $parent, $term_name, $field = NULL) {
  global $language_content;
  if (empty($token)) {
    return FALSE;
  }
  if (!is_numeric($vid)) {
    return FALSE;
  }
  if (!shs_add_term_access($vid, $parent, $field)) {

    // Sorry, but this user may not add a term to this vocabulary.
    return FALSE;
  }
  $term = (object) array(
    'vid' => $vid,
    'parent' => $parent,
    'name' => str_replace('&', '&', filter_xss($term_name)),
    'language' => $language_content->language,
  );

  // 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;
}