Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Ability to use 'submit_button' component in a multi-level webform-structure (tree). #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 71 additions & 15 deletions webform_submit_button.module
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
function webform_submit_button_webform_component_info() {
$components = array();
$components['submit_button'] = array(
'label' => t('Submit button'),
'label' => t('Submit button'),
'description' => t('Displays Submit Button in the Form as a webform component.'),
'features' => array(
'analysis' => FALSE,
'csv' => FALSE,
'default_value' => FALSE,
'description' => FALSE,
'email' => FALSE,
'required' => FALSE,
'conditional' => FALSE,
'title_display' => FALSE,
'private' => FALSE,
'features' => array(
'analysis' => FALSE,
'csv' => FALSE,
'default_value' => FALSE,
'description' => FALSE,
'email' => FALSE,
'required' => FALSE,
'conditional' => FALSE,
'title_display' => FALSE,
'private' => FALSE,
'wrapper_classes' => FALSE,
'css_classes' => FALSE,
'css_classes' => FALSE,
),
'file' => 'webform_submit_button_component.inc',
'file' => 'webform_submit_button_component.inc',
);

return $components;
Expand Down Expand Up @@ -55,7 +55,6 @@ function webform_submit_button_form_alter(&$form, &$form_state, $form_id) {
$current_component_type = $value['#webform_component']['type'];

if ($current_component_type == 'submit_button') {

// Get the weight of the user's submit button component.
$current_weight = $value['#webform_component']['weight'];

Expand All @@ -64,12 +63,69 @@ function webform_submit_button_form_alter(&$form, &$form_state, $form_id) {
$actions['#weight'] = $current_weight;

// Move the webform submit button into the position of the
// user's submit button component. Remove the user's component since it
// user's submit button component. Remove the user's component since it
// was really just a placeholder for the real thing.

unset($form['actions']);
unset($form['submitted'][$key]);
$form['submitted']['submit_button'] = $actions;

// 'submit_button' is found at the top level, so we return now.
return;
}
}

// Try to find 'submit_button' component somewhere in the "haystack".
$tree = _webform_submit_button_find_component($form['submitted']);

if(!empty($tree)) {
$tree = array_reverse($tree);

// Try to make a reference to the 'submit_button' component.
foreach ($tree as $key => $value) {
if(!isset($element)) {
$element = &$form['submitted'][$value];
continue;
}

$element = &$element[$value];
}

if(!empty($element)) {
// Get the weight of the user's submit button component.
$current_weight = $element['#webform_component']['weight'];

// Apply the weight to the actual webform submit button.
$actions = $form['actions'];
$actions['#weight'] = $current_weight;

$form['actions']['#access'] = FALSE;
$element = $actions;
}
}

}

/**
* Helper function to find "submit_button" component somewhere in a "haystack".
*/
function _webform_submit_button_find_component($components, &$tree = array()) {
foreach ($components as $key => $value) {
if (!isset($value['#webform_component']['type'])) {
continue;
}

if ($value['#webform_component']['type'] == 'submit_button') {
$tree[] = $key;
return $tree;
}

$found = _webform_submit_button_find_component($value, $tree);
if (!empty($found)) {
$tree[] = $key;
return $tree;
}
}

return $tree;
}