3

I'm trying to implement a hook for inserting Event custom fields into Scheduled reminders; however, I'm not a developer so I'm really not sure what the code needs to look like. The custom field is custom_115 (Learning Materials link), which I know is the correct field as I located it using the API Explorer. The problem for me has been the hooks; not really understanding the structure. I'm running CiviCRM 5.7.2 on WordPress. I've pasted the following code (which I pieced together from various sources, and partly guessed) into my functions.php file but I know it's wrong and I don't know how to get it to work:

<?php 
/* CiviCRM token addition for custom_115 */
function civicrm_tokens(&$tokens) {
  $tokens['event'] = array(
    'event.custom_115' => 'Custom 115',
    );
}


/**
* Populates a token named "custom_115" with the value from a custom field 
* (id 115) for use in Scheduled Reminders
* Implements hook_civicrm_tokenValues().
*/

function civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
static $events = array();

  if (is_numeric($cids)) {
    // We're being called by CRM/Core/BAO/ActionSchedule.php:465
    // Params are structured differently than they're supposed to be, but we 
    // can work with it. See https://civicrm.stackexchange.com/questions/3573/can-tokenvalue-hooks-be-used-in-scheduled-reminder-emails

if ($event_id = CRM_Utils_Array::value('event.event_id', $values)) {
  // We've been passed an event ID

  if (!array_key_exists($event_id, $events)) {
    // We haven't looked up the custom field value yet

    // Fetch the custom field value from the database
    $event = civicrm_api3('Event', 'getsingle', array(
      'sequential' => 1,
      'return' => "custom_115",
      'id' => $event_id,
    ));

  $val = CRM_Utils_Array::value('custom_115', $event);

  // Keep this value in memory to use with the other event participants
  $events[$event_id] = $val;

  } else {
    // We stored the value previously
    $val = $events[$event_id];
  }

  $values['event.custom_115'] = $val;
  }

 }
}
?>

I'm assuming that if it is set up properly, I'll be able to locate "Custom_115" as an option in the Tokens list - is that right? Any help/advice would be appreciated. Thanks

0

The code doesn't go in functions.php, you need to create a little extension. See https://docs.civicrm.org/dev/en/latest/extensions.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.