Tokens innerhalb von Themes benutzen?
![](http://www.drupalcenter.de/files/noavatar_mini.gif)
am 21.08.2008 - 12:56 Uhr in
Ich möchte gerne ein Token in meinem Template benutzen! Das Token Modul habe ich natürlich installiert.
Dazu habe ich mal die Api des Tokens Moduls durchforstet und das richtige ist glaube ich das:
token_get_values($type = 'global', $object = NULL)
Dabei kriegt man dann soetwas:
stdClass Object {
[tokens] => array(
[0] => mytoken1,
[1] => mytoken2
),
[values] => array(
[0] => value1,
[1] => value2,
)
}
Nun möchte ich den Token, [term-id], dass heißt die Funktion dieses Tokens in meinem Template verwenden. Es gibt mir die "ID of top taxonomy term". Also die tid des ersten Taxonomy terms.
Diese tid möchte ich für eine Funktion aus einem anderen thread(http://www.drupalcenter.de/node/12082) verwenden, ich habe bereits die ersten Schritte gewagt:
<?php
$term_values = token_get_values($type = 'global', $object = NULL);
$average = custom_average_xy($values['term-id']);
?>
Leider kommt eine Fehlermeldung: Das Script sagt mir dann, es könnte das stdClass Object nicht als Array nutzen. Wie muss ich dann vorgehen?
- Anmelden oder Registrieren um Kommentare zu schreiben
drupaler3000 schrieb Dabei
am 21.08.2008 - 13:35 Uhr
Dabei kriegt man dann soetwas:
stdClass Object {
[tokens] => array(
[0] => mytoken1,
[1] => mytoken2
),
[values] => array(
[0] => value1,
[1] => value2,
)
}
Ok, daran siehst du, dass die Rückgabe kein Array, sondern ein Objekt ist (stdClass Object). Auch wenn print_r das etwas array-like darstellt.
<?php
$term_values = token_get_values($type = 'global', $object = NULL);
$average = custom_average_xy($values['term-id']);
?>
Leider kommt eine Fehlermeldung: Das Script sagt mir dann, es könnte das stdClass Object nicht als Array nutzen. Wie muss ich dann vorgehen?
Na klar, du sprichst das Objekt $values ja auch als Array und nicht als Objekt an. Probier mal:
<?php
$term_values = token_get_values($type = 'global', $object = NULL);
$average = custom_average_xy($term_values->term-id);
?>
Es kommt zwar keine
am 21.08.2008 - 13:55 Uhr
Es kommt zwar keine Fehlermeldung, aber es gibt auch leider nichts aus...
Ich habe es ja auch schon probiert mit array davor oder is_array.
Ich habe es halt mit $values['term-id'], weil es so auch im modul definiert ist:
<?php
// And now taxonomy, which is a bit more work. This code is adapted from
// pathauto's handling code; it's intended for compatability with it.
if (module_exists('taxonomy') && !empty($node->taxonomy) && is_array($node->taxonomy)) {
foreach ($node->taxonomy as $term) {
$original_term = $term;
if ((object)$term) {
// With freetagging it's somewhat hard to get the tid, vid, name values
// Rather than duplicating taxonomy.module code here you should make sure your calling module
// has a weight of at least 1 which will run after taxonomy has saved the data which allows us to
// pull it out of the db here.
if (!isset($term->name) || !isset($term->tid)) {
$vid = db_result(db_query("SELECT t.vid FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.nid = %d ORDER BY v.weight, t.weight, t.name LIMIT 1", $object->nid));
if (!$vid) {
continue;
}
$term = db_fetch_object(db_query("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight LIMIT 1", $vid, $object->nid));
$term->vid = $vid;
}
// Ok, if we still don't have a term name maybe this is a pre-taxonomy submit node
// So if it's a number we can get data from it
if(!isset($term->name) && is_array($original_term)) {
$tid = array_shift($original_term);
if (is_numeric($tid)) {
$term = taxonomy_get_term($tid);
}
}
$values['term'] = check_plain($term->name);
$values['term-raw'] = $term->name;
$values['term-id'] = $term->tid;
$vid = $term->vid;
if(!empty($vid)) {
$vocabulary = taxonomy_get_vocabulary($vid);
$values['vocab'] = check_plain($vocabulary->name);
$values['vocab-raw'] = $vocabulary->name;
$values['vocab-id'] = $vocabulary->vid;
}
break;
}
}
}
// It's possible to leave that block and still not have good data.
// So, we test for these and if not set, set them.
if (!isset($values['term'])) {
$values['term'] = '';
$values['term-raw'] = '';
$values['term-id'] = '';
$values['vocab'] = '';
$values['vocab-raw'] = '';
$values['vocab-id'] = '';
}
break;
}
return $values;
?>
Könnte man vielleicht einfach diese Funktion nutzen(token_node.inc), oder damit eine neue Funktion definieren, und praktisch die Funktion des tokens noch mal selber definieren?
Ich habe es gerade nochmal
am 21.08.2008 - 15:05 Uhr
Ich habe es gerade nochmal direkt ausgegeben, und er gibt nicht NICHTS aus, sondern 0! ...