View source
<?php
function webform_node_value_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'webform_configure_form') {
webform_node_value_configuration_form($form);
}
if (isset($form['#node']->webform) && in_array('webform_client_form_submit', $form['#submit'])) {
$page_nid = webform_node_value_nid_from_page();
$form_nid = $form['details']['nid']['#value'];
if ($page_nid != $form_nid) {
$settings = webform_node_value_configuration_get($form['details']['nid']['#value']);
if (isset($settings->node_path_source) && $settings->node_path_source == 'page') {
$form['#action'] .= '?webform_node_value_nid=' . $page_nid;
}
}
array_unshift($form['#submit'], 'webform_node_value_client_submit');
}
}
function webform_node_value_configuration_form(&$form) {
$settings = webform_node_value_configuration_get($form['nid']['#value']);
$form['webform_node_value'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Set Value From A Node'),
'#description' => t('Fill a Webform field with the value of a node field.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['webform_node_value']['node_path_source'] = array(
'#type' => 'radios',
'#required' => FALSE,
'#title' => t('Path To Node'),
'#description' => t('Where to get the path to the node.'),
'#default_value' => isset($settings->node_path_source) ? $settings->node_path_source : 'unset',
'#options' => array(
'unset' => t('Value not set from a node'),
'page' => t('From page webform is on'),
'field' => t('From webform field'),
),
);
$form['webform_node_value']['prefix'] = array(
'#type' => 'textfield',
'#required' => FALSE,
'#title' => t('Path Alias Prefix'),
'#description' => t("Add a prefix to the path alias of the node, if necessary, e.g. 'sector/'. Ignore this if Path To Node is set to 'From page webform is on'."),
'#default_value' => isset($settings->prefix) ? $settings->prefix : '',
);
$form['webform_node_value']['webform_source'] = array(
'#type' => 'textfield',
'#required' => FALSE,
'#title' => t('Webform Source Field'),
'#description' => t("Webform field who's value (key) will be used as a path to the node, e.g. 'airline'. Ignore this if Path To Node is set to 'From page webform is on'."),
'#default_value' => isset($settings->webform_source) ? $settings->webform_source : '',
);
$form['webform_node_value']['node_field_name'] = array(
'#type' => 'textfield',
'#required' => FALSE,
'#title' => t('Node Source Field'),
'#description' => t("Node field to get a value from, e.g. 'field_sector_contact_email'"),
'#default_value' => isset($settings->node_field_name) ? $settings->node_field_name : '',
);
$form['webform_node_value']['webform_destination'] = array(
'#type' => 'textfield',
'#required' => FALSE,
'#title' => t('Webform Target Field'),
'#description' => t("Webform field to put value from node into, e.g. 'send_to_email'"),
'#default_value' => isset($settings->webform_destination) ? $settings->webform_destination : '',
);
array_unshift($form['#submit'], 'webform_node_value_configuration_submit');
}
function webform_node_value_configuration_submit($form, &$form_state) {
$settings = $form_state['values']['webform_node_value'];
$settings['nid'] = $form_state['values']['nid'];
webform_node_value_configuration_save($settings);
return;
}
function webform_node_value_client_submit($form, &$form_state) {
$settings = webform_node_value_configuration_get($form_state['values']['details']['nid']);
if ($settings === FALSE) {
return;
}
switch ($settings->node_path_source) {
case 'page':
if (isset($_GET['webform_node_value_nid']) && !empty($_GET['webform_node_value_nid'])) {
if (user_access('node/' . $_GET['webform_node_value_nid'])) {
$nid = $_GET['webform_node_value_nid'];
}
else {
watchdog('webform_node_value', t('Attempt to access node that user did not have permission to access.'));
}
}
break;
case 'field':
if (!isset($form_state['values']['submitted'][$settings->webform_source])) {
watchdog('webform_node_value', 'Source field not found on Webform: %field', array(
'%field' => $settings->webform_source,
));
return;
}
$webform_value = $form_state['values']['submitted'][$settings->webform_source];
$nid = webform_node_value_nid_from_field($settings, $webform_value);
break;
}
if (empty($nid)) {
watchdog('webform_node_value', "Node ID %nid couldn't be found", array(
'%nid' => $nid,
));
return;
}
$node = node_load($nid);
$node_field = field_get_items('node', $node, $settings->node_field_name);
if (empty($node_field)) {
watchdog('webform_node_value', "Field %field could not be found for Node ID %nid (%title)", array(
'%field' => $settings->node_field_name,
'%nid' => $nid,
'%title' => $node->title,
));
return;
}
$node_field_value = field_view_value('node', $node, $settings->node_field_name, $node_field[0]);
$field_name = trim(check_plain($settings->webform_destination));
$form_state['values']['submitted'][$field_name] = strip_tags(render($node_field_value));
return;
}
function webform_node_value_nid_from_page() {
$path = current_path();
$path_parts = explode('/', $path);
if ($path_parts[0] != 'node' || !is_numeric($path_parts[1])) {
return FALSE;
}
$nid = $path_parts[1];
return $nid;
}
function webform_node_value_nid_from_field($settings, $webform_value) {
if (empty($webform_value)) {
return '';
}
$node_alias_path = ltrim($settings->prefix . $webform_value, '/');
$path_parts = explode('/', $node_alias_path);
if ($path_parts[0] != 'node' || !is_numeric($path_parts[1])) {
$node_system_path = drupal_get_normal_path($node_alias_path);
if ($node_alias_path == $node_system_path) {
watchdog('webform_node_value', 'System path not found %path', array(
'%path' => $node_system_path,
));
return '';
}
$path_parts = explode('/', $node_system_path);
}
if ($path_parts[0] != 'node' || !is_numeric($path_parts[1])) {
watchdog('webform_node_value', t('Node not found.'));
return '';
}
$nid = $path_parts[1];
return $nid;
}
function webform_node_value_configuration_save($settings) {
db_delete('webform_node_value')
->condition('nid', $settings['nid'])
->execute();
if (isset($settings['node_path_source']) && $settings['node_path_source'] != 'unset') {
db_insert('webform_node_value')
->fields($settings)
->execute();
}
}
function webform_node_value_configuration_get($nid) {
$configuration = db_query_range('SELECT * FROM {webform_node_value} WHERE nid = :nid', 0, 1, array(
':nid' => $nid,
))
->fetchObject();
return $configuration;
}