Posts Drupal v7.x - Taxonomy terms form - input validation
Post
Cancel

Drupal v7.x - Taxonomy terms form - input validation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//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.

origin - https://www.pipiscrew.com/?p=16602 drupal-v7-x-taxonomy-terms-form-input-validation

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags