Changes

Jump to navigation Jump to search
5,381 bytes added ,  21:06, 28 January 2016
adds hook code
You can create your own tokens by implementing <code>hook_civicrm_tokens()</code> and <code>hook_civicrm_tokenValues()</code>. See
<source lang="php"><?php/** * A convenience function so that we can map our custom fields and labels * in one place, and share them between hooks without having to write to the db * with variable_set() */function getHaystack($stack='general') { $genKeys = array ( "wUrl" => "custom_40", "mainpage" => "custom_41", "base" => "custom_42", "sitename" => "custom_43", "logo" => "custom_44", "generator" => "custom_45", "phpversion" => "custom_46", // "phpsapi", "dbtype" => "custom_47", "dbversion" => "custom_48", // "externalimages", // "langconversion", // "titleconversion", // "linkprefixcharset", // "linkprefix", // "linktrail", // "legaltitlechars", // "git-hash", // "git-branch", // "case", // "lang", // "fallback", // "fallback8bitEncoding", "writeapi" => "custom_49", "timezone" => "custom_50", "timeoffset" => "custom_51", "articlepath" => "custom_52", "scriptpath" => "custom_53", // "script", // "variantarticlepath", "server" => "custom_54", "servername" => "custom_55", "wikiid" => "custom_56", "time" => "custom_57", "maxuploadsize" => "custom_58", // "thumblimits", // "imagelimits", "favicon" => "custom_59", ); $statKeys = array( "wUrl" => "custom_60", "pages" => "custom_61", "articles" => "custom_62", "edits" => "custom_63", "images" => "custom_64", "users" => "custom_65", "activeusers" => "custom_66", "admins" => "custom_67", "jobs" => "custom_68", ); switch ($stack) { case 'general': $haystack = $genKeys; $addLabels = array ( 'recorded' => 'custom_69', ); $haystack += $addLabels; break; case 'stats': $haystack = $statKeys; $addLabels = array ( 'recorded' => 'custom_70', ); $haystack += $addLabels; break; default: die('no stack by that name'); } return $haystack;}/** * implementation of hook_civicrm_tokens() * Much appreciation and thanks to Eileen McNaughton who helped me get the logic * correct on this. cf.https://github.com/eileenmcnaughton/civicrm_views_token/blob/master/civicrm_views_token.module#L16 * * In the end, we want to populate more $tokens in a format like * $token['general.generator'] = 'Generator'; * $token['general.sitename'] = 'Sitename'; * where the $token key is the element that needs to be populated * by hook_civicrm_tokenValues() * The $token value here is used in the UI as a label for the token. */function eqt_civicrm_tokens(&$tokens) { $labels = getHaystack('general'); $tokens['general'] = array(); foreach ($labels as $k => $v) { $tokens['general']["general.$k"] = "$k ($v)"; } $labels = getHaystack('stats'); $tokens['stats'] = array(); foreach ($labels as $k => $v) { $tokens['stats']["stats.$k"] = "$k ($v)"; }}/** * implementation of hook_civicrm_tokenValues() */function eqt_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) { // for debugging // watchdog('eqt',"eqt_civicrm_tokenValues(\$values, \$cids, \$job, \$tokens, \$context)<pre>\n\$values=" . var_export($values,1) . "\n\$cids=" . var_export($cids,1) . "\n\$job=" . $job ."\n\$tokens=" . var_export($tokens,1) . "\n\$context=" . $context . "\n</pre>"); if ( array_key_exists('general', $tokens) ) { $haystack = getHaystack('general'); foreach ($cids as $cid) { $params = array( 'sequential' => 1, 'entity_id' => $cid, ); $result = civicrm_api3('CustomValue', 'get', $params); if (!$result['is_error']) { $customdata = ($result['values']); foreach ($customdata as $val) { $needle = 'custom_' . $val['id']; $label = array_search($needle, $haystack); if (!$label) { continue; } $label = 'general.' . $label; $values[$cid][$label] = $val['latest']; } } } } if ( array_key_exists('stats', $tokens) ) { $haystack = getHaystack('stats'); foreach ($cids as $cid) { $params = array( 'sequential' => 1, 'entity_id' => $cid, ); $result = civicrm_api3('CustomValue', 'get', $params); if (!$result['is_error']) { $customdata = ($result['values']); foreach ($customdata as $val) { $needle = 'custom_' . $val['id']; $label = array_search($needle, $haystack); if (!$label) { continue; } $label = 'stats.' . $label; $values[$cid][$label] = $val['latest']; } } } }}</source>
* [https://civicrm.stackexchange.com/questions/2558/tokens-for-custom-field-set-with-multiple-records?rq=1 Stack Exchange]
4,558

edits

Navigation menu