how to get a taxonomy term from the term's name in the url

Events happening in the community are now at Drupal community events on www.drupal.org.
gallamine's picture

Ok, so it's probably apparent right now that I'm working with taxonomy. So ... here's the current dilemma:

I'm trying to get a taxonomy term via a url. The problem is that only the taxonomy's name appears in the URL. Ordinarily (ha! this is my first module, so there is no "ordinary") I would use "taxonomy_get_term_by_name()", BUT the problem there is that the taxonomy term in the URL has all the bad characters escaped, and spaces turned into dashes. Sooo ... can't do that. So, what do I do?

e.g. If I have a term "Bob's Term is Awesome!", it'll appear in the URL as "term/bobs-term-is-awesome", which is what gets passed to my module, but "bobs-term-is-awesome" won't match via taxonomy_get_term_by_name()

Comments

URL alias?

ajross's picture

The URL for taxonomy term pages use the term id, not the term itself (if I remember correctly), so are you using Pathauto to set up an alias for term pages? If so you should still be able to access the term id using arg() and then access the term object with taxonomy_get_term($tid).

ajross, Good comment, and it

gallamine's picture

ajross,

Good comment, and it could potentially be part of the solution, but not the whole thing. I am, in fact, not actually using a taxonomy path - it's a different path, but with the taxonomy term name in it.
Views can seem to get the correct term object by using the following settings:

Argument: Taxonomy: Term ID
Display all values
Validator: Taxonomy term
Argument type: Term name/synonym converted to term ID

and I check "Transform dashes in URL to spaces in term name arguments"

So, the secret sauce in the View is, "Term name/synonym converted to term ID."

My PHP/Drupal-fu isn't strong enough to delve into Views to figure out how it does it. Anyone know?

string replace

ajross's picture

Ah ok - I just took a guess that it might be Pathauto since one of its defaults is to convert a taxonomy term id to the term with spaces separated by dashes. If it is not an alias for a URL with the term id, then you can't get the id as I describe.

I just took a peek at the API and taxonomy_get_term_by_name() does a case-insensitive match. So it should work to take your string as an argument from the URL and simply convert dashes to spaces (and it sounds like that is all Views does too). This is very easy to do with str_replace(), just do something like:

$term = 'a-taxonomy-term-with-many-words';
$new_term = str_replace('-',' ',$term);

That sounds like a passible

gallamine's picture

That sounds like a passible idea, that'll get me 80% there. My only concern is that if a user selects a term name with any special characters (like a period or exclamation point), this idea wont work. Is there any way to make SQL ignore special characters when it does it's search?

special characters

ajross's picture

Right - sorry. I missed your initial comment about the special characters. So what code/module is responsible for creating the URL path using the taxonomy term? If we look at how the term string is being manipulated there, it will probably be easier to convert it back.

ajross, Also, I just checked

gallamine's picture

ajross,

Also, I just checked and when I print($arg(1)) on the page "clubtag/termname", where "termname" is the taxonomy term's name. I get back "termname", NOT the term's ID.

triDUG

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: