Drupal v7.x - Taxonomy terms form - input validation

Costas

Administrator
Staff member
JavaScript:
//https://drupal.stackexchange.com/q/223140

function dining_taxonomy_term_presave($term)
{
    if ($term->vocabulary_machine_name == 'chairs' ) {

        $term_name = $term->name;

        if (strpos($term_name, ',') !== false) {
            drupal_set_message('Comma is not allowed!', 'error');
            drupal_goto("koula/structure/taxonomy/chairs/add");
            return;
        }

        $sql    = "select tid from taxonomy_term_data
        where vid = (select vid from taxonomy_vocabulary where machine_name = 'chairs' limit 1)
        and
        taxonomy_term_data.name = '".$term_name."'";

        //https://www.drupal.org/docs/7/api/database-api/result-sets
        $result = db_query($sql);

        if ($result->rowCount() > 0) {
            drupal_set_message('Duplicate name!', 'error');
            drupal_goto("koula/structure/taxonomy/chairs/add");
        }

    }
}

/*
Normally the validation should be made @ ALTER event but didnt fire on drupal v7.x 
https://www.drupal.org/docs/7/api/entity-api/additional-hooks-for-exportable-entities
https://drupal.stackexchange.com/questions/35899/hook-taxonomy-term-view-alter-is-not-being-called

we made this workaround with goto, working without adding the record to dbase.
*/

the same can be done for nodes</b>.
 
Top