webform_ajax_page.module in Webform Ajax Page 7
Same filename and directory in other branches
Ajax page element for webform
File
webform_ajax_page.moduleView source
<?php
/**
* @file
* Ajax page element for webform
*/
/**
* Implements hook_webform_component_info().
*/
function webform_ajax_page_webform_component_info() {
$components = array();
$components['ajax_page'] = array(
'label' => t('Ajax page'),
'description' => t('Ajax page for webform'),
'features' => array(
'csv' => FALSE,
'required' => FALSE,
'conditional' => FALSE,
'group' => TRUE,
),
);
return $components;
}
/**
* Implements hook_form_alter().
*/
function webform_ajax_page_form_alter(&$form, &$form_state, $form_id) {
//if there are more than 1 webforms on the page;
static $amount = 0;
//amount of forms
static $panelamount = 0;
//amount of ajax pages
if (strpos($form_id, 'webform_client_form') !== FALSE) {
$panels = array();
$setting = array(
'WebFormAjaxPage' => array(),
);
foreach (element_children($form['submitted']) as $name) {
$component = $form['submitted'][$name];
if (isset($component['#type']) && $component['#type'] == 'ajax_page') {
$panels[] = "<a href='#{$component['#id']}-{$panelamount}'>{$component['#title']}</a>";
$form['submitted'][$name]['#id'] = "{$component['#id']}-{$panelamount}";
$setting['WebFormAjaxPage']['labels']["{$component['#id']}-{$panelamount}"] = array(
'next' => $component['#webform_component']['extra']['next_label'],
'previous' => $component['#webform_component']['extra']['prev_label'],
);
$panelamount++;
}
}
if (!empty($setting['WebFormAjaxPage'])) {
drupal_add_js($setting, 'setting');
}
$hide_submit_nids = variable_get('webform_ajax_page_hide_submit_nodes', array());
$hide_submit = array_key_exists($form['#node']->nid, $hide_submit_nids) ? $hide_submit_nids[$form['#node']->nid] : variable_get('webform_ajax_page_hide_submit', 1);
if (!empty($panels)) {
$setting = array(
'WebFormAjaxPage' => array(
'forms' => array(
$amount => array(
'id' => str_replace('_', '-', $form_id),
'wrapper' => "webform-page-tabs-{$amount}",
'hide_submit' => $hide_submit,
),
),
),
);
$form['#attached']['js'][] = array(
'data' => $setting,
'type' => 'setting',
);
$list = "<ul>\n";
$i = 0;
$panelnumber = 0;
foreach ($panels as $panel) {
$list .= "<li class='panel-{$panelnumber}'>{$panel}</li>\n";
$panelnumber++;
}
$list .= "</ul>\n";
$form['submitted']['tabs_prefix'] = array(
'#type' => 'markup',
'#markup' => "<div id='webform-page-tabs-{$amount}'>" . $list,
'#weight' => -1,
);
$form['submitted']['tabs_suffix'] = array(
'#type' => 'markup',
'#markup' => '</div>',
'#weight' => 1500,
);
$amount++;
}
}
elseif ($form_id == 'webform_admin_settings') {
$form['buttons']['#weight'] = 1;
$form['ajax_page'] = array(
'#type' => 'fieldset',
'#title' => 'Webform Ajax Page',
'#description' => 'Webform ajax page settings',
);
$form['ajax_page']['webform_ajax_page_css'] = array(
'#type' => 'radios',
'#title' => t('Include the standard css provided by the Webform Ajax Page module'),
'#description' => t('If you set this option to "No", the default jQuery UI css will be used (Tabs display for pages, with previous and next links), if you set it to "Yes", no tabs will be displayed, only previous and next links will be visible.'),
'#default_value' => variable_get('webform_ajax_page_css', 1),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$form['ajax_page']['webform_ajax_page_prev_label'] = array(
'#type' => 'textfield',
'#title' => t('Label for the "previous page" button'),
'#description' => t('This is the label that will appear on the button to go to the previous page'),
'#default_value' => t(variable_get('webform_ajax_page_prev_label', '<< Previous Page')),
);
$form['ajax_page']['webform_ajax_page_next_label'] = array(
'#type' => 'textfield',
'#title' => t('Label for the "next page" button'),
'#description' => t('This is the label that will appear on the button to go to the next page'),
'#default_value' => t(variable_get('webform_ajax_page_next_label', 'Next Page >>')),
);
$form['ajax_page']['webform_ajax_page_hide_submit'] = array(
'#type' => 'radios',
'#title' => t('Hide the submit button until the last page?'),
'#description' => t('Determines if the "Submit" button on the form is hidden until the user gets to the last page of the form.'),
'#default_value' => variable_get('webform_ajax_page_hide_submit', 1),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
}
elseif ($form_id == "webform_components_form") {
$types = array();
$node = $form['#node'];
foreach ($node->webform['components'] as $component) {
$types[] = $component['type'];
}
if (in_array('pagebreak', $types) && in_array('ajax_page', $types)) {
drupal_set_message(t('Warning: You currently have page breaks and ajax pages in your webform. This will break the ajax page functionality.'), 'warning');
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function webform_ajax_page_form_webform_configure_form_alter(&$form, $form_state, $form_id) {
if (webform_node_has_ajax_page($form['#node'])) {
$form['ajax_page_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Ajax Page settings'),
'#description' => t('Configure Ajax Page settings for this webform'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$hide_submit_nids = variable_get('webform_ajax_page_hide_submit_nodes', array());
$default = array_key_exists($form['#node']->nid, $hide_submit_nids) ? $hide_submit_nids[$form['#node']->nid] : variable_get('webform_ajax_page_hide_submit', 1);
$form['ajax_page_settings']['ajax_page_hide_submit_override'] = array(
'#type' => 'checkbox',
'#title' => t('Override submit button visibility setting'),
'#description' => t('Override the default setting to hide the submit button until the last page or to show it from the beginning'),
'#default_value' => array_key_exists($form['#node']->nid, $hide_submit_nids),
);
$form['ajax_page_settings']['ajax_page_hide_submit'] = array(
'#type' => 'radios',
'#title' => t('Hide the submit button until the last page?'),
'#description' => t('Determines if the "Submit" button on the form is hidden until the user gets to the last page of the form.'),
'#default_value' => $default,
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#states' => array(
'visible' => array(
':input[name="ajax_page_hide_submit_override"]' => array(
'checked' => TRUE,
),
),
),
);
$form['#submit'][] = 'webform_ajax_page_webform_configure_form_submit';
}
}
/**
* Submit callback.
*/
function webform_ajax_page_webform_configure_form_submit($form, $form_state) {
$hide_submit_nids = variable_get('webform_ajax_page_hide_submit_nodes', array());
if (!$form_state['values']['ajax_page_hide_submit_override']) {
unset($hide_submit_nids[$form['#node']->nid]);
}
else {
$hide_submit_nids[$form['#node']->nid] = $form_state['values']['ajax_page_hide_submit'];
}
variable_set('webform_ajax_page_hide_submit_nodes', $hide_submit_nids);
}
/**
* Implements _webform_defaults_component().
*/
function _webform_defaults_ajax_page() {
return array(
'name' => '',
'form_key' => NULL,
'pid' => 0,
'weight' => 0,
'extra' => array(
'title_display' => 0,
'collapsible' => 0,
'collapsed' => 0,
'description' => '',
'private' => 0,
'prev_label' => t(variable_get('webform_ajax_page_prev_label', '<< Previous Page')),
'next_label' => t(variable_get('webform_ajax_page_next_label', 'Next Page >>')),
),
);
}
/**
* Implements _webform_render_component().
*/
function _webform_render_ajax_page($component, $value = NULL, $filter = TRUE) {
$component['weight'] += 10;
$class = array(
'webform-component-ajax-page',
'ui-tabs-panel',
);
$element = array(
'#type' => 'ajax_page',
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : NULL,
'#weight' => $component['weight'],
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
'#attributes' => array(
'class' => $class,
),
'#pre_render' => array(
'webform_element_title_display',
),
'#webform_component' => $component,
'#theme' => 'ajax_page',
//id's are made unique in hook_form_alter
'#id' => 'webform-component-ajax-page',
);
return $element;
}
/**
* Implements _webform_theme_component().
*/
function _webform_theme_ajax_page() {
return array(
'ajax_page' => array(
'render element' => 'element',
),
);
}
/**
* Theme callback
*/
function theme_ajax_page($variables) {
drupal_add_library('system', 'ui.tabs');
$element = $variables['element'];
if (!array_key_exists('#attributes', $element)) {
$element['#attributes'] = array();
}
if (variable_get('webform_ajax_page_css', 1)) {
drupal_add_css(drupal_get_path('module', 'webform_ajax_page') . '/webform_ajax_page.css');
}
$element['#children'] = !empty($element['#children']) ? $element['#children'] : drupal_render_children($element);
drupal_add_js(drupal_get_path('module', 'webform_ajax_page') . '/webform_ajax_page.js');
return '<div id="' . $element['#id'] . '" ' . drupal_attributes($element['#attributes']) . ' >' . ($element['#title'] ? '<h3>' . $element['#title'] . '</h3>' : '') . (isset($element['#description']) && $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '') . (!empty($element['#children']) ? $element['#children'] : '') . (isset($element['#value']) ? $element['#value'] : '') . "</div>\n";
}
/**
* Implements _webform_display_component().
*/
function _webform_display_ajax_page($component, $value, $format = 'html') {
if ($format == 'text') {
$element = array(
'#title' => $component['name'],
'#weight' => $component['weight'],
'#post_render' => array(
'webform_element_wrapper',
),
'#theme' => 'ajax_page',
'#theme_wrappers' => array(
'webform_element_text',
),
);
}
else {
$element = _webform_render_ajax_page($component, $value);
}
$element['#webform_component'] = $component;
$element['#format'] = $format;
return $element;
}
/**
* Implements _webform_edit_component().
*/
function _webform_edit_ajax_page($component) {
$form['display']['prev_label'] = array(
'#type' => 'textfield',
'#title' => t('Label for the "previous page" button'),
'#description' => t('This is the label that will appear on the button to go to the previous page (e.g. "Go to Page 1")'),
'#default_value' => $component['extra']['prev_label'],
'#parents' => array(
'extra',
'prev_label',
),
);
$form['display']['next_label'] = array(
'#type' => 'textfield',
'#title' => t('Label for the "next page" button'),
'#description' => t('This is the label that will appear on the button to go to the next page (e.g. "Go to Page 3")'),
'#default_value' => $component['extra']['next_label'],
'#parents' => array(
'extra',
'next_label',
),
);
return $form;
}
/**
* Helper function to check if a webform has an ajax page component.
*/
function webform_node_has_ajax_page($node) {
if (!isset($node->webform) || !isset($node->webform['components']) || empty($node->webform['components'])) {
return FALSE;
}
foreach ($node->webform['components'] as $component) {
if ($component['type'] == 'ajax_page') {
return TRUE;
}
}
return FALSE;
}
Functions
Name![]() |
Description |
---|---|
theme_ajax_page | Theme callback |
webform_ajax_page_form_alter | Implements hook_form_alter(). |
webform_ajax_page_form_webform_configure_form_alter | Implements hook_form_FORM_ID_alter(). |
webform_ajax_page_webform_component_info | Implements hook_webform_component_info(). |
webform_ajax_page_webform_configure_form_submit | Submit callback. |
webform_node_has_ajax_page | Helper function to check if a webform has an ajax page component. |
_webform_defaults_ajax_page | Implements _webform_defaults_component(). |
_webform_display_ajax_page | Implements _webform_display_component(). |
_webform_edit_ajax_page | Implements _webform_edit_component(). |
_webform_render_ajax_page | Implements _webform_render_component(). |
_webform_theme_ajax_page | Implements _webform_theme_component(). |