node.inc in Webform Node Element 6
Same filename and directory in other branches
Webform module node component.
File
components/node.incView source
<?php
/**
* @file
* Webform module node component.
*/
/**
* Implements _webform_defaults_component().
*/
function _webform_defaults_node() {
return array(
'name' => '',
'form_key' => NULL,
'pid' => 0,
'weight' => 0,
'value' => '',
'extra' => array(
'nid' => 0,
'title' => 0,
'view_mode' => FALSE,
),
);
}
/**
* Implements _webform_theme_component().
*/
function _webform_theme_node() {
return array(
'webform_node_element_node' => array(
'render element' => 'element',
),
'webform_display_node' => array(
'render element' => 'element',
),
);
}
/**
* Implements _webform_edit_component().
*/
function _webform_edit_node($component) {
$form = array();
$form['value'] = array(
'#type' => 'textfield',
'#size' => 5,
'#title' => t('Node'),
'#default_value' => $component['value'],
'#description' => t('Enter a valid node id, whose body will be shown as markup in the webform.') . theme('webform_token_help'),
'#weight' => -1,
'#element_validate' => array(
'_webform_edit_validate_node',
),
);
$form['extra']['nid'] = array(
'#type' => 'hidden',
'#default_value' => $component['extra']['nid'],
);
$form['extra']['view_mode'] = array(
'#type' => 'radios',
'#title' => t('Display Mode'),
'#default_value' => $component['extra']['view_mode'],
'#description' => t('Choose how you want the referenced node to be displayed.'),
'#options' => array(
TRUE => t('Display the node teaser'),
FALSE => t('Display the full node body'),
),
);
$form['extra']['title'] = array(
'#type' => 'checkbox',
'#title' => t('Override Title'),
'#default_value' => $component['extra']['title'],
'#description' => t('If checked, the title of the referenced node will be used as the component label.'),
'#options' => array(
0,
1,
),
);
$form['extra']['description'] = array();
$form['display'] = array(
'#type' => 'markup',
);
// Hide the display options.
// If ctools is present, use node title autocomplete.
if (module_exists('ctools')) {
$form['value']['#description'] = t('Enter a valid node id, or start typing to find the node whose body will be shown as markup in the webform.') . theme('webform_token_help');
$form['value']['#autocomplete_path'] = 'ctools/autocomplete/node';
unset($form['value']['#size']);
}
return $form;
}
/**
* Element validation callback. Ensure a valid node ID was entered.
*/
function _webform_edit_validate_node($element, &$form_state) {
if (!empty($element['#value'])) {
// If ctools, the value might be in the form of "node title [nid: N]".
if (module_exists('ctools')) {
_webform_node_autocomplete_validate($element);
}
$node = node_load($element['#value']);
if (empty($node->nid) || empty($node->status) && !user_access('administer nodes')) {
form_error($element, t('Invalid node'));
}
// Store the node id that we'll use. #value is just for show :-)
$form_state['values']['extra']['nid'] = $node->nid;
}
return TRUE;
}
/**
* Implements _webform_render_component().
*/
function _webform_render_node($component, $value = NULL, $filter = TRUE) {
$node = node_load($component['extra']['nid']);
// Add a bit of error checking; the target node may have been deleted.
if (empty($node->nid)) {
drupal_set_message(t('Unable to load referenced node id !nid', array(
'!nid' => $component['extra']['nid'],
)), 'error');
return array();
}
// All good, render the node body in the chosen format.
$component['#field_value'] = node_view($node, $component['extra']['view_mode']);
$element = array(
'#type' => 'hidden',
'#title' => $component['extra']['title'] ? check_plain($node->title) : $component['name'],
'#weight' => $component['weight'],
'#default_value' => $component['extra']['nid'],
'#input_format' => 'html',
'#prefix' => _webform_node_element_prefix($component),
'#suffix' => _webform_node_element_suffix($component),
'#webform_component' => $component,
);
if (isset($value)) {
$element['#default_value'] = $value[0];
}
return $element;
}
/**
* Implements _webform_display_component().
*/
function _webform_display_node($component, $value, $format = 'text') {
$node = node_load($component['extra']['nid']);
$content = node_view($node, $component['extra']['view_mode']);
return array(
'#title' => $component['name'],
'#weight' => $component['weight'],
'#theme' => 'webform_display_node',
'#theme_wrappers' => array(
'webform_display_node',
),
'#format' => 'text',
'#markup' => $content['body'] . $content['links'],
'#webform_component' => $component,
);
}
/**
* Implements _webform_analysis_component().
*/
function _webform_analysis_node($component, $sids = array()) {
$node = node_load($component['extra']['nid']);
return array(
array(
t('Nid'),
t('<a href="!link">@nid</a>', array(
'!link' => url('node/' . $node->nid),
'@nid' => $node->nid,
)),
),
array(
t('Title'),
check_plain($node->title),
),
array(
t('Type'),
$node->type,
),
array(
t('Display Mode'),
$component['extra']['view_mode'] ? t('Teaser') : t('Full'),
),
);
}
/**
* Implements _webform_table_component().
*/
function _webform_table_node($component, $value) {
$node = node_load($component['extra']['nid']);
$content = node_view($node, $component['extra']['view_mode']);
return check_plain($content['body'] . $content['links']);
}
/**
* Implements _webform_csv_headers_component().
*/
function _webform_csv_headers_node($component, $export_options) {
$header = array();
$header[0] = '';
$header[1] = '';
$header[2] = $component['name'];
return $header;
}
/**
* Implements _webform_csv_data_component().
*/
function _webform_csv_data_node($component, $export_options, $value) {
$node = node_load($component['extra']['nid']);
$content = node_view($node, $component['extra']['view_mode']);
return $content['body'] . $content['links'];
}
/**
* Overridable theme function.
*/
function theme_webform_display_node($variables) {
$element = $variables['element'];
return $element['#format'] == 'html' ? check_plain($element['#markup']) : $element['#markup'];
}
/**
* Helper to pull apart a possible ctools node autocomplete submission.
*
* This is lifted verbatim from the ctools_node_content_type_edit_form_validate()
* function at http://drupalcontrib.org/api/drupal/contributions--ctools--plugins--content_types--node--node.inc/7/source
*
* @param &$element
* A reference to the form element submitted by the user.
*/
function _webform_node_autocomplete_validate(&$element) {
$nid = $element['#value'];
$preg_matches = array();
$match = preg_match('/\\[nid: (\\d+)\\]/', $nid, $preg_matches);
if (!$match) {
$match = preg_match('/^nid: (\\d+)/', $nid, $preg_matches);
}
if ($match) {
$nid = $preg_matches[1];
}
if (is_numeric($nid)) {
$node = db_result(db_query("SELECT nid FROM {node} WHERE nid = %d", $nid));
}
else {
$node = db_result(db_query("SELECT nid FROM {node} WHERE LOWER(title) = LOWER('%s')", $nid));
}
if (!empty($node)) {
$element['#value'] = $node;
}
return;
}
/**
* Generate a prefix for the node field.
*
* This can't be done using a theme wrapper on Drupal 6.x, so sadly this
* needs two helper functions. The actual rendered node content is added
* in the otherfucntion, _webform_node_element_suffix().
*/
function _webform_node_element_prefix($element) {
$output = '<div class="form-item webform-component webform-component-node" id="webform-component-' . $element['form_key'] . '">' . "\n";
$output .= isset($element['#field_prefix']) ? '<span class="field-prefix">' . _webform_filter_xss($element['#field_prefix']) . '</span> ' : '';
return $output;
}
/**
* Generate a suffix for the node field.
*
* This can't be done using a theme wrapper on Drupal 6.x, so sadly this
* needs two helper functions. The actual rendered node content is added
* here.
*/
function _webform_node_element_suffix($element) {
$output = '';
// Because we're a hidden field, the rendered node content won't show up
// on the page. However, #field_value contains a copy. Prepend that on to
// the suffix output here.
$output .= isset($element['#field_value']) ? '<span class="field-value">' . $element['#field_value'] . '</span> ' : '';
$output .= isset($element['#field_suffix']) ? '<span class="field-suffix">' . _webform_filter_xss($element['#field_suffix']) . '</span>' : '';
$output .= "</div>\n";
return $output;
}
Functions
Name![]() |
Description |
---|---|
theme_webform_display_node | Overridable theme function. |
_webform_analysis_node | Implements _webform_analysis_component(). |
_webform_csv_data_node | Implements _webform_csv_data_component(). |
_webform_csv_headers_node | Implements _webform_csv_headers_component(). |
_webform_defaults_node | Implements _webform_defaults_component(). |
_webform_display_node | Implements _webform_display_component(). |
_webform_edit_node | Implements _webform_edit_component(). |
_webform_edit_validate_node | Element validation callback. Ensure a valid node ID was entered. |
_webform_node_autocomplete_validate | Helper to pull apart a possible ctools node autocomplete submission. |
_webform_node_element_prefix | Generate a prefix for the node field. |
_webform_node_element_suffix | Generate a suffix for the node field. |
_webform_render_node | Implements _webform_render_component(). |
_webform_table_node | Implements _webform_table_component(). |
_webform_theme_node | Implements _webform_theme_component(). |