function webform_rules_tokens in Webform Rules 7
Implements hook_tokens().
File
- ./
webform_rules.module, line 210 - Adds rules integration for webform submissions.
Code
function webform_rules_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'webform' && is_array($data['webform'])) {
// Get component attributes.
$webform_node = reset($data[$type]);
if (empty($webform_node->nid)) {
return $replacements;
}
$webform = node_load($webform_node->nid);
$components = $webform->webform['components'];
//component['value'] have to be like array([0] => xxx)
foreach ($webform_node->data as $cid => $val) {
$c_key = $components[$cid]['form_key'];
$webform_node->{$c_key} = array(
'nid' => $components[$cid]['nid'],
'cid' => $cid,
'pid' => $components[$cid]['pid'],
'form_key' => $c_key,
'name' => $components[$cid]['name'],
'type' => $components[$cid]['type'],
'value' => $val,
'extra' => $components[$cid]['extra'],
'required' => $components[$cid]['required'],
'weight' => $components[$cid]['weight'],
'page_num' => $components[$cid]['page_num'],
);
}
$components = get_object_vars($webform_node);
//Convert object into array
$component_names = array_keys($components);
//Read machine-readable-name of components
foreach ($tokens as $name => $original) {
if ($name == 'sid') {
$replacements[$original] = $webform_node->sid;
}
elseif ($name == 'data') {
$replacements[$original] = webform_rules_prepare_component_value($webform_node);
}
elseif ($name == 'data-raw') {
$replacements[$original] = webform_rules_prepare_component_value($webform_node, TRUE);
}
else {
// Split token name to get the components name.
$token = explode('-', $name);
$component_name = array_shift($token);
if (!in_array($component_name, $component_names)) {
continue;
}
// Join token name (without component name).
$token = implode('-', $token);
// Webform component.
$component = $webform_node->{$component_name};
$component_value = $component['value'];
//Use value as default
if (empty($token)) {
$token = 'value';
}
switch ($token) {
case 'title':
$replacements[$original] = $component['name'];
break;
case 'display':
$replacements[$original] = webform_rules_render_component($component, $component_value);
break;
case 'display-html':
$replacements[$original] = webform_rules_render_component($component, $component_value, 'html');
break;
case 'value':
$replacements[$original] = webform_rules_render_component($component, $component_value, 'text', FALSE);
break;
case 'value-html':
$replacements[$original] = webform_rules_render_component($component, $component_value, 'html', FALSE);
break;
case 'value-raw':
$replacements[$original] = webform_rules_prepare_component_value($component_value, TRUE);
break;
}
}
}
}
return $replacements;
}