3

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;
}

1 Answer 1

3

Problem is not with managed file upload, but with your code. You simply ignored the fact that form building process may be invoked many times, if AJAX comes into play.

Simplest way is to add item to $form_state, store the node id there, and if it is set, use it instead of whatever is provided (or not) after AJAX call.

3
  • i tried that but it does not seem to work, also. i get an ajax alert with a really long message. wait a second ill post the code
    – Alex
    Commented Aug 28, 2014 at 9:23
  • by the way: what would be to correct way to add the field to the form state? still the form_alter hook?
    – Alex
    Commented Aug 28, 2014 at 9:26
  • @Alex yes, you can use $form_state in form alter hooks. That's why it is passed by reference. You are allowed to read and write it as you need.
    – Mołot
    Commented Aug 28, 2014 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.