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;
}
?>
CommentFileSizeAuthor
#4 updated_token.patch5.26 KBkreynen
#3 merci-462758-3.patch4.02 KBDane Powell
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dane Powell’s picture

I 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.

Dane Powell’s picture

Great, 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?

Dane Powell’s picture

Status: Active » Needs review
FileSize
4.02 KB

Okay, here's a first patch!

kreynen’s picture

FileSize
5.26 KB

I 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

Dane Powell’s picture

Ah... 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.

kreynen’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Dane Powell’s picture

Version: 6.x-1.x-dev » 6.x-2.0-beta2
Status: Closed (fixed) » Active

It 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');

Dane Powell’s picture

Looks like that line is still commented out with a TODO to fix it... what's wrong exactly?

  • kreynen committed a3b97dc on 8.x-2.x
    committed initial changes for #462758, but commented out the $tokens['...