[development] Node Token Replacement + CCK

Dave Reid dave at davereid.net
Tue Feb 15 14:50:28 UTC 2011


Since you're already writing custom code to handle it, I think it would be
easier to define the token entirely in your own custom module and add a
small admin interface that saves this price to a variable, rather than
abusing the node system to fit your purpose.

funciton custommodule_token_list($type = 'all') {
  if ($type == 'all' || $type == 'global') {
    $tokens['price-foo'] = t('The price of foo.');
    return $tokens;
  }
}

function custommodule_token_values($type, $object = NULL, $options =
array()) {
  if ($type == 'global') {
    $tokens['price-foo'] = check_plain(variable_get('price_foo', '$10.00'));
    return $tokens;
  }
}

Dave Reid
dave at davereid.net


On Tue, Feb 15, 2011 at 4:28 AM, DTH <david at hartster.org> wrote:

> Quick question: my original intention was to set it up so that people
> could use something like, for example, {{PRICE_OF_FOO}} in node
> content, so that when the price of foo changes, it only has to be
> changed in one place rather than in lots of different places.
>
> Rather than reinvent the wheel, I've used Token + Token Filter, so
> that people can use [token custom price-foo]. Everything works fine
> with:
>
> function custommodule_token_values($type, $object = NULL, $options =
> array()) {
>  if ($type == 'custom') {
>    $tokens['price-foo']      = "$19.99";
>    return $tokens;
>  }
> }
>
> The next step was to stick a UI on this so anybody can update the
> price. To do so I created a new content type "tokens", and added CCK
> fields for foo_price, foo_version_number etc. The code now looks like:
>
> function custommodule_get_tokens(){
>        $node = new stdClass();
>        $node->nid =12;
>        $node->vid = db_result(db_query("SELECT vid FROM {node} WHERE nid =
> %d",$node->nid));
>        $node->type = 'tokens';
>        $content = content_storage('load', $node);
>        return
> array("price-foo"=>$content['field_foo_price'][0]['value'],"version-foo"=>$content['field_foo_version_number'][0]['value']);
> }
>
> function custommodule_token_values($type, $object = NULL, $options =
> array()) {
>  if ($type == 'custom') {
>    $tokens = custommodule_get_tokens();
>    return $tokens;
>  }
> }
>
> Basically: is this the best way to do this, or is there a simpler/more
> native to giving users node tokens they can edit themselves. I wanted
> to check before I embarked on a round of clearing up the UI so that
> the "tokens" content type doesn't have all the menu/published/language
> stuff on etc, making sure people can't hit node/12 etc.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20110215/1df9e723/attachment.html 


More information about the development mailing list