Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I saw the screencast and thought it would be cool to have a token that contains the name of the reserved resource, so I created a token hook. Maybe you would like to add or extend this?
<?php
//this example right here just creates a token that provides the resouces name(s)
/**
* Implementation of hook_token_list
*
*/
function merci_token_list($type = 'all') {
if ($type == 'node') {
$tokens['node']['merci_resource'] = t('Reserved Resource');
return $tokens;
}
}
/**
* Implementation of hook_token_values()
* @see {merci_token_list}
*/
function merci_token_values($type, $object = NULL) {
switch ($type) {
case 'node':
$merci = merci_load($object);
if($merci) {
$arr = array();
foreach($merci->merci['reservation_items'] as $item) {
$arr[] = $item->ttitle;
}
if(count($arr) > 1) $values['merci_resource'] = check_plain(implode(", ",$arr));
else $values['merci_resource'] = check_plain(array_pop($arr));
if(strlen($values['merci_resource']) > 26) $values['merci_resource'] = substr(substr(),0,20)."...";
return $values;
}
break;
}
// No group info found. Return defaults.
$values['merci_resource'] = '';
return $values;
}
?>
Comment | File | Size | Author |
---|---|---|---|
#4 | updated_token.patch | 5.26 KB | kreynen |
#3 | merci-462758-3.patch | 4.02 KB | Dane Powell |
Comments
Comment #1
Dane Powell CreditAttribution: Dane Powell commentedI think this is a great idea- I'm implementing tokens for member cost and commercial cost as well. I'm also making it so you can include tokens in the header / footer of the contract. I'll submit a patch when I can.
Comment #2
Dane Powell CreditAttribution: Dane Powell commentedGreat, I have everything working as described in 1. Before I roll a patch, is there anything else I should add?
Also, in order to calculate commercial and member cost, I just duplicated a lot of code from the contract generation function. It seems to me like these calculations should be split off into a separate function, like
merci_commercial_cost($node)
, or the rental cost should be calculated and saved as a field of the reservation node in the database. Thoughts?Comment #3
Dane Powell CreditAttribution: Dane Powell commentedOkay, here's a first patch!
Comment #4
kreynen CreditAttribution: kreynen commentedI manually applied the changes in this patch after updating the spacing issues. I've attached what I think is a duplicate of the changes, but I'm getting this error...
warning: Invalid argument supplied for foreach() in /var/www/openmediaprofile/profiles/openmedia/modules/merci/merci.module on line 3135.
when I use [author-name] reserved [merci_resources] on Reservations with Autonode Title
Comment #5
Dane Powell CreditAttribution: Dane Powell commentedAh... I thought the merci_load line was redundant in the original submission, but now I see that it's not (in case the token_replace function is called while the node is in presave status).
Fix: change the line
$node = $object
to$node = merci_load($object);
The only problem is that because the node title is processed before the node is saved to the database, certain information (such as reserved items) aren't available and won't show up in the title. I'm not really sure what can be done to fix this.
Comment #6
kreynen CreditAttribution: kreynen commentedComment #8
Dane Powell CreditAttribution: Dane Powell commentedIt looks like this didn't get entirely committed, is there a reason the following line is commented out?
//$tokens['node']['merci_resources'] = t('Reserved resource');
Comment #9
Dane Powell CreditAttribution: Dane Powell commentedLooks like that line is still commented out with a TODO to fix it... what's wrong exactly?