Pergunta

How should I know which variables and objects can be used directly in the template.php file?This is what a template.php file created by somebody else contains:

function garland_separate_terms($node_taxonomy) {
  if ($node_taxonomy) {
    foreach ($node_taxonomy as $term) { 
      $links[$term->vid]['taxonomy_term_'. $term->tid] // …
      // …
    }
    // …
  }
}

Why can he use the $term object directly?

Foi útil?

Solução

Well first look at what this function is. Is it a theme function? It has a theme prefix, but i cant find it on http://drupalcontrib.org/api/search/6/separate_terms, so probably it isnt.

You get $node_taxonomy in argument, which seems to be a list of taxonomy terms, so you have access to those.

Everything else is not available to you, since you don't receive those variables, only the one in the argument. (well ok, you can access global variables, but I don't want to confuse you)

He can use $term because he knows that $node_taxonomy is an array of terms, and loops through them with foreach.

If in doubt next time use the devel module and add a dsm($node_taxonomy) line to see how that variable is built up.

Outras dicas

Simple answer - the line foreach ($node_taxonomy AS $term) is where the $term variable is coming from. That's a basic PHPism that treats $node_taxonomy as an array, and puts the current item from that array into the $term variable as it iterates over the array.

As snufkin noted, $node_taxonomy is the only variable you're getting access to automatically.

As already reported by Eaton, the template.php file contains functions that are called from the template files used by themes.
garland_separate_terms() is then not a function present in the file used by Garland Drupal comes with.

page.tpl.php has access to the variable $node, which contains the node object when the page being visualized is a node page; also node.tpl.php has access to the variable $node. Those are some of the template files that can invoke that function, and which have access to a node object, from which is possible to access the list of taxonomy terms associated with the node.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a drupal.stackexchange
scroll top