}
}
- // might as well get all the hook tokens to
+ // Get all the hook tokens too
$hookTokens = array();
CRM_Utils_Hook::tokens($hookTokens);
foreach ($hookTokens as $tokenValues) {
/**
* Formats a token list for the select2 widget
+ *
* @param $tokens
* @return array
*/
}
/**
- * Get mailing token.
+ * Get tokens for one or more entity type
+ *
+ * Output will be formatted either as a flat list,
+ * or pass sequential=1 to retrieve as a hierarchy formatted for select2.
*
* @param array $params
+ * Should contain an array of entities to retrieve tokens for.
*
* @return array
* @throws \API_Exception
function civicrm_api3_mailing_gettokens($params) {
$tokens = array();
foreach ((array) $params['entity'] as $ent) {
- $func = $ent . 'Tokens';
+ $func = lcfirst($ent) . 'Tokens';
if (!method_exists('CRM_Core_SelectValues', $func)) {
throw new API_Exception('Unknown token entity: ' . $ent);
}
'title' => 'Entity',
'options' => array(),
);
+ // Fetch a list of token functions and format to look like entity names
foreach (get_class_methods('CRM_Core_SelectValues') as $func) {
if (strpos($func, 'Tokens')) {
- $ent = str_replace('Tokens', '', $func);
+ $ent = ucfirst(str_replace('Tokens', '', $func));
$params['entity']['options'][$ent] = $ent;
}
}
/**
* Test Generated example demonstrating the Mailing.gettokens API.
*
- * Demonstrates fetching tokens for one or more entities (in this case "contact" and "mailing").
+ * Demonstrates fetching tokens for one or more entities (in this case "Contact" and "Mailing").
* Optionally pass sequential=1 to have output ready-formatted for the select2 widget.
*
* @return array
function mailing_gettokens_example() {
$params = array(
'entity' => array(
- '0' => 'contact',
- '1' => 'mailing',
+ '0' => 'Contact',
+ '1' => 'Mailing',
),
);
* Test Mailing.gettokens.
*/
public function testMailGetTokens() {
- $description = "Demonstrates fetching tokens for one or more entities (in this case \"contact\" and \"mailing\").
+ $description = "Demonstrates fetching tokens for one or more entities (in this case \"Contact\" and \"Mailing\").
Optionally pass sequential=1 to have output ready-formatted for the select2 widget.";
- $result = $this->callAPIAndDocument($this->_entity, 'gettokens', array('entity' => array('contact', 'mailing')), __FUNCTION__, __FILE__, $description);
- $this->assertContains('Contact Type', $result);
+ $result = $this->callAPIAndDocument($this->_entity, 'gettokens', array('entity' => array('Contact', 'Mailing')), __FUNCTION__, __FILE__, $description);
+ $this->assertContains('Contact Type', $result['values']);
+
+ // Check that passing "sequential" correctly outputs a hierarchical array
+ $result = $this->callAPISuccess($this->_entity, 'gettokens', array('entity' => 'contact', 'sequential' => 1));
+ $this->assertArrayHasKey('text', $result['values'][0]);
+ $this->assertArrayHasKey('id', $result['values'][0]['children'][0]);
}
//@ todo tests below here are all failure tests which are not hugely useful - need success tests