Este snippet es uno de nuestros favoritos, ya que nos permitirá mostrar, con taxonomías, el padre, el hijo y los demás niveles de un vocabulario jerárquico:
(Visto en: http://drupal.org/node/1532198 )
Un saludo a todos los drupaleros de este planeta y a los del resto de la galaxia.
Añadir el "Parent term / Child term" pertenecientes a una Hierarchical select
Este ejemplo serviría para implementar un nodo donde, arriba del título, requirás añadir por ejemplo: "Coche > Renault > Diesel"... es decir, que requiráis usar una serie de jerarquía en las categorías que estéis usando en vuestro proyecto web.<?php
$p = taxonomy_get_parents($node->field_municipio[0]['value']);
foreach($p AS $parent) {
$node->field_municipio[0]['value'] = $parent->name;
}
?>
<strong><?php print check_plain($node->field_municipio[0]['value']) ?> / <?php print $node->field_municipio[0]['view'] ?></strong>
Un listado de todos los parents terms de una taxonomía
<?php
// Usaremos el vocabulario de taxonomía "42"
$vid = 42;
// url parent term if exist
$parent = arg(1);
// internal use
$depth = -1;
// how depth search, 1parents, 2children, etc.
$max_depth = 1;
// if no url arg, then set to cero.
if (!is_numeric ($parent)) {
$parent=0;
}
// declare items array
$items = array();
// get terms. If exist an URL parent get the children, if not get only the parents.
$terms = taxonomy_get_tree($vid, $parent, $depth, $max_depth);
// use the terms, in this case show a list.
foreach ( $terms as $term ) {
$items[] = l($term->name, "taxonomy/term/$term->tid") ;
}
if ( count($items) ) { print theme('item_list', $items);}
?>
Muestra un listado con todas las categorías (ya sean parent o child) y un sumatorio de los child terms entre paréntesis
Este snippet queda chulísimo en un bloque que queramos meter en una tienda virtual o en un blog informativo típico. Esperamos que os guste.<?php
//Configuramos el vocabulario número "42" en este ejemplo
$resultados = db_query ("SELECT DISTINCT t2.tid, t2.name
FROM `term_data` AS t2
INNER JOIN `term_hierarchy` ON parent = t2.tid
INNER JOIN `term_data` AS t1 ON term_hierarchy.tid = t1.tid
WHERE t1.vid = 42 ORDER BY t2.name ASC ");
while ($cats = db_fetch_object($resultados)) {
$terms = taxonomy_get_tree( 4, $cats->tid); /* necesitamos esto para el print de más abajo */
print l($cats->name, "taxonomy/term/$cats->tid");
print "<ul>";
foreach ( $terms as $term ) {
$count = db_result(db_query("SELECT DISTINCT COUNT(nid) FROM {term_node} WHERE tid = %d ", $term->tid));
if ($count) { /* don't show terms with 0 count */
print "<li>".l($term->name .' ('. $count .')', "taxonomy/term/$term->tid") ."</li>";
}
} /* fin del foreach */
print "</ul>";
}
?>
Categoría