DrupalBin
Submit Code
About
Recent Posts
Code
Fix for Fix for Code
Fix for Code
Code
Node type whitelist
an axe coach accessories
coach purses married
Fix for de las ghd alturas
de las ghd alturas
Code
more
User login
Log in using OpenID:
What is OpenID?
Username:
*
Password:
*
Log in using OpenID
Cancel OpenID login
Create new account
Request new password
Tags
CCK
drupal
fapi
jquery
menu
module
php
simpletest
taxonomy
test
theme
views
more tags
Home
Fix for Filter nodes by a taxonomy_content field with view argument accepting terms names and synonyms
View
Fix
July 22, 2009 - 5:07am —
mongolito404
/**
* The following code is used as validator for a view argument to filter nodes having the passed
* term as value for a taxonomy_content field. The argument can be the term name or one of
* its synonyms. It works, but the title of the page is the returned tid and not the term's name.
*/
$terms
=
taxonomy_get_term_by_name
(
$argument
)
;
$synonym_root
=
taxonomy_get_synonym_root
(
$argument
)
;
if
(
$synonym_root
)
$terms
[
]
=
$synonym_root
;
foreach
(
$terms
as
$term
)
{
$parents
=
taxonomy_get_parents_all
(
$term
-
>
tid
)
;
foreach
(
$parents
as
$parent
)
{
if
(
$parent
-
>
name
== REQUIRED_PARENT_NAME
)
{
$handler
-
>
argument
=
$term
-
>
tid
;
return
TRUE
;
}
}
}
return
FALSE
;
/**
* Since the view is exported in a custom module created with the features module (see http://drupal.org/project/features),
* the following preprocess_page function is used to overrides the title (and head_title).
*/
function
MYMODULE_preprocess_page
(
&
$vars
)
{
if
(
arg
(
0
)
== VIEW_PATH
&&
arg
(
1
)
&&
is_numeric
(
$vars
[
'title'
]
)
)
{
$tid
=
(
int
)
$vars
[
'title'
]
;
$term
=
taxonomy_get_term
(
$tid
)
;
if
(
$term
)
{
$vars
[
'title'
]
=
check_plain
(
$term
-
>
name
)
;
$vars
[
'head_title'
]
=
str_replace
(
$tid
,
check_plain
(
$term
-
>
name
)
,
$vars
[
'head_title'
]
)
;
}
}
}
/**
* The module also needs to set it weight to be greater than views' one (10) so its preprocess_page is called after views' one.
*/
function
MYMODULE_install
(
)
{
db_query
(
"UPDATE {system} SET weight = 11 WHERE name = 'MYMODULE'"
)
;
}
taxonomy
validator
views
views2
Submit Fix
Summary:
Tags:
Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Show summary in full view
/** * The following code is used as validator for a view argument to filter nodes having the passed * term as value for a taxonomy_content field. The argument can be the term name or one of * its synonyms. It works, but the title of the page is the returned tid and not the term's name. */ $terms = taxonomy_get_term_by_name($argument); $synonym_root = taxonomy_get_synonym_root($argument); if($synonym_root) $terms[] = $synonym_root; foreach($terms as $term) { $parents = taxonomy_get_parents_all($term->tid); foreach($parents as $parent) { if($parent->name == REQUIRED_PARENT_NAME) { $handler->argument = $term->tid; return TRUE; } } } return FALSE; /** * Since the view is exported in a custom module created with the features module (see http://drupal.org/project/features), * the following preprocess_page function is used to overrides the title (and head_title). */ function MYMODULE_preprocess_page(&$vars) { if(arg(0) == VIEW_PATH && arg(1) && is_numeric($vars['title'])) { $tid = (int)$vars['title']; $term = taxonomy_get_term($tid); if($term) { $vars['title'] = check_plain($term->name); $vars['head_title'] = str_replace($tid, check_plain($term->name), $vars['head_title']); } } } /** * The module also needs to set it weight to be greater than views' one (10) so its preprocess_page is called after views' one. */ function MYMODULE_install() { db_query("UPDATE {system} SET weight = 11 WHERE name = 'MYMODULE'"); }
Syntax highlighting mode:
ActionScript
ColdFusion
Diff
Drupal 5
Drupal 6
HTML
INI
Javascript
MySQL
PHP
Python
robots.txt
SQL
Text
Select the syntax highlighting mode to use.
See Also:
Order
Title:
URL:
-1
0
1
Title:
URL:
-1
0
1
Any links you'd like to have associated with the post (Drupal.org issue, Wikipedia article, etc).
File attachments
Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds.
Attach new file:
The maximum upload size is
1 MB
. Only files with the following extensions may be uploaded:
jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp phps
.