courtly
courtly
courtly
courtly

Upcoming Events

San Francisco CiviCRM Meetup - February 8th, 2012
February 8th, 2012
Come meet others from the Bay Area who are interested in, using or developing (more...)

UK usergroup - London meetup
February 8th, 2012
Come and meet others from the UK that are using CiviCRM or are interested in (more...)

London user and administrator training
February 23rd, 2012
A comprehensive two day hands on training course covering the configuration, (more...)

CiviCRM London sprint Feb 2012
February 27th, 2012
Following the CiviCRM training here in London, we will have a CiviCRM code (more...)

Philadelphia - CiviCRM Meetup for Q1 2012
March 13th, 2012

UK South West - CiviCRM Meetup
March 20th, 2012
Come meet others from the Area who are interested in, using or developing for (more...)

[Bristol, UK] user and administrator training
March 21st, 2012
A comprehensive hands on training course covering the configuration, (more...)

San Francisco user and administrator training
March 29th, 2012
A comprehensive two day hands on training course covering the configuration, (more...)

CiviCRM Usability, Test and Code Sprint - San Francisco (March 2012)
March 29th, 2012
This usability, code and test sprint is targeted at CiviCRM users and (more...)

CiviCon 2012 San Francisco Bay Area - April 2nd 2012
April 2nd, 2012
CiviCon is THE annual event bringing together the people who use, develop, (more...)

CiviCRM Documentation, Test and Code Sprint - after CiviCon San Francisco (April 2012)
April 4th, 2012
This sprint is targeted at CiviCRM users and developers who want to work on (more...)

CiviCRM Components

Tools for engaging your supporters...

CiviContribute


CiviEvent


CiviMail


CiviMember


CiviReport


CiviMail: How to add default values for empty tokens

Not Just a Contact Database

These optional components give you more power to connect and engage your supporters.

  • civiCASE

  • Case management for clients and constituents.

  • civiEVENT

  • Online event registration and participant tracking.

  • civiMEMBER

  • Online signup and membership management.

  • civiMAIL

  • Personalized email blasts and newsletters.

  • civiREPORT

  • Report generation and template management.

September 17, 2009 - 21:16 — CiviCRM Team

There was a thread in the forum a few weeks ago about adding generic replacements for tokens that dont have a value for a specific contact. An example is where the first name field is blank and the organization would want to substitute friend or member instead of keeping it blank. The token engine does not provide this functionality by default, however implementing it via a hook is relatively trivial.

We use the tokenValues hook to "modify" the token values substituted in any outgoing email. If a contact does not have a token value for a specific token, we just insert the default value. The below code assumes you have a drupal module called civiexample. for joomla change the function name to joomla__civicrm_tokenValues and to standalone_civicrm_tokenValues for standalone.

function civiexample_civicrm_tokenValues( &$values, &$contactIDs ) {
    if ( is_array( $contactIDs ) ) {
        $single = false;
    } else {
        $contactIDs = array( $contactIDs );
        $single = true;
    }

    // lets assume we want default values for the below tokens
    static $defaults = array( 'first_name'  => 'DEFAULT FIRST',
                              'last_name'   => 'DEFAULT LAST',
                              'middle_name' => 'DEFAULT MIDDLE',
                              'home_URL'    => 'HOME URL' );

    foreach ( $contactIDs as $cid ) {
        if ( $single ) {
            $value =& $values;
        } else {
            $value =& $values[$cid];
        }

        foreach ( $defaults as $k => $v ) {
            if ( ! isset( $value[$k] ) || empty( $value[$k] ) ) {
                $value[$k] = $v;
            }
        }
    }
}
( categories: )