Edit: working code, although pretty ugly:
if (
isset($form['submitted']['stelle']['#default_value'])
&& $form['submitted']['stelle']['#default_value'] !== ''
&& is_numeric($form['submitted']['stelle']['#default_value'])
&& !isset($form_state['stelle'])
) {
$nid = $form['submitted']['stelle']['#default_value'];
$node = node_load($nid);
if ($node === false || (isset($node->type) && $node->type !== 'job')) {
$redirect_form = true;
}
else {
$type = $node->type;
if ($type == 'job') {
$form['submitted']['stelle']['#default_value'] = $node->title;
$form['submitted']['stelle']['#attributes']['disabled'] = 'disabled';
$form_state['stelle'] = $form['submitted']['stelle'];
$form['submitted']['related']['#value'] = $nid;
$unhide_components = array(
'einleitung_standard'
);
}
}
}
elseif (isset($form_state['stelle']['#default_value']) && $form_state['stelle']['#default_value'] !== '' && strlen($form_state['stelle']['#default_value']) && $form_state['stelle']['#default_value'] !== 'initiativ') {
$form['submitted']['stelle']['#default_value'] = $form_state['stelle']['#default_value'];
}
else {
$form['submitted']['related']['#value'] = 'initiativ';
$form['submitted']['stelle']['#value'] = 'initiativ';
$form['submitted']['stelle']['#attributes']['disabled'] = 'disabled';
$unhide_components = array(
'einleitung_initiativ'
);
}
I am pretty much facing the same problem as this buddy here: https://www.drupal.org/node/2311199
I have a webform with 2 fields where users can upload a file to attach to the form. In the webform there is another field stelle
which gets populated via %get[stelle]
by webform. If it is set and a valid node id, a node gets loaded and the field stelle
is set to that node's title.
However, when a user picks a file and clicks upload the ajax processing seems to reset that form state, completely removing that parameter.
When I just choose a file and dont click the upload button (so that no AJAX happens) and submit the form, everything works fine.
I followed the instructions of the above link but I it seems to be buggy: the ajax response returns the complete form again, not just the field itself.
Here is my original module:
<?php
/**
* Altering the form! this will add class to the file upload/ remove buttons
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['submitted']['lebenslauf'])) {
// add css to the upload button
$form['submitted']['lebenslauf']['#process'] = array('mymodule_my_file_element_process');
}
$nid = $form['#node']->nid;
if (isset($form['submitted']['stelle']['#default_value']) && $form['submitted']['stelle']['#default_value'] !== '' && is_numeric($form['submitted']['stelle']['#default_value'])) {
$nid = $form['submitted']['stelle']['#default_value'];
$node = node_load($nid);
if ($node === false || (isset($node->type) && $node->type !== 'job')) {
$redirect_form = true;
}
else {
$type = $node->type;
if ($type == 'job') {
$form['submitted']['stelle']['#default_value'] = $node->title;
$form['submitted']['stelle']['#attributes']['disabled'] = 'disabled';
$form['submitted']['related']['#value'] = $nid;
$unhide_components = array(
'einleitung_standard'
);
}
}
}
else {
$form['submitted']['related']['#value'] = 'initiativ'; // this is always the case when using ajax
$form['submitted']['stelle']['#value'] = 'initiativ'; // this is always the case when using ajax
$form['submitted']['stelle']['#attributes']['disabled'] = 'disabled';
}
// ...
}
function mymodule_my_file_element_process($element, &$form_state, $form) {
$element = file_managed_file_process($element, $form_state, $form);
$element['upload_button']['#attributes'] = array('class' => array('button', 'light-grey'));
$prefix = '<label class="browse-slave">';
$prefix .= '<span class="button light-grey">' . t('Choose a file') . '</span>';
$element['upload']['#prefix'] = $prefix;
$element['upload_button']['#prefix'] = '</label>';
$element['remove_button']['#attributes'] = array('class' => array('button', 'light-grey'));
$element['remove_button']['#prefix'] = '</label>';
return $element;
}