Community Documentation

taxonomy_get_term_by_name

5 taxonomy.module taxonomy_get_term_by_name($name)
6 taxonomy.module taxonomy_get_term_by_name($name)
7 taxonomy.module taxonomy_get_term_by_name($name)

Try to map a string to an existing term, as for glossary use.

Provides a case-insensitive and trimmed mapping, to maximize the likelihood of a successful match.

Parameters

name: Name of the term to search for.

Return value

An array of matching term objects.

File

modules/taxonomy/taxonomy.module, line 1022
Enables the organization of content into categories.

Code

<?php
function taxonomy_get_term_by_name($name) {
  $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.* FROM {term_data} t WHERE LOWER(t.name) = LOWER('%s')", 't', 'tid'), trim($name));
  $result = array();
  while ($term = db_fetch_object($db_result)) {
    $result[] = $term;
  }

  return $result;
}
?>

Comments

Are you having trouble

Are you having trouble freetagging identical words with different accents? Like "Rubén Amiel" and "Ruben Amiel"? The cause is MySQL and utf8_general_ci and the solution is to run this SQL on the term_data table's name column:

ALTER TABLE `term_data` CHANGE `name` `name` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL

I.e. to change the collation to 'utf8_bin' from 'utf8_general_ci'' for the name column.

http://stackoverflow.com/questions/500826/how-to-conduct-an-accent-sensi...

Getting taxonomy arguments from the URL and checking validity

Here is the working code to get the arguments from the URL and check taxonomy term validity using the above function.

I have used this code to pass the 2nd argument to block views from the URL i.e. if the URL is http://example.com/arg1/arg2 . So this code will get the arg2 and check its validity (of course you can change it with whatever you want to do). For someone who is not familiar with views, blocks and arguments, here is the step by step approach.

1. Select the 'Taxonomy:term' field in arguments section in the block view
2. Select 'Provide default argument' from 'Action to take if argument no present'
3. Select 'PHP Code' from 'Provide default argument options' and copy paste the code below in 'PHP Argument Code' section
4. Voila!! That is all. Save and test the block.

Tip: I would recommend to paste the code in page.tpl.php and var_dump or print_r to actually see what its going to pass as an argument before putting the code into the views block. If you have got a better way of debugging things, you can always do that. It's just a simple way. But DO NOT do this on a production site.

<?php
    $alias
= drupal_get_path_alias($_GET['q']); //get URL alias
   
if (!is_null($alias)) {
       
$path = explode('/', $alias);
        if (
array_key_exists('1', $path)) {
           
$tax_found = taxonomy_get_term_by_name(str_replace('-', ' ', $path[1]));
           
            if (
count ($tax_found) > 0) {
                return
strtolower($tax_found[0]->name); // this will return the taxonomy term if it exists
           
} else {
                return
"all"; // fall back to use all wildcard
           
}
           
        } else {
            return
"all"// fall back to use all wildcard
       
}
    } else {
        return
"all"// fall back to use all wildcard
   
}
?>

Weird problem with string variable as argument

1. I have a string "myterm" into $token variable;
2. run taxonomy_get_term_by_name($token);
3. I get nothing...

but...

1. I put the string "myterm" directly as argument
2. run taxonomy_get_term_by_name("myterm");
3. I get the tid

WTF?

Please, help me... what is wrong whit the firts case?

Login or register to post comments