Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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 Aug 28 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 Aug 28 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 Aug 28 at 9:32
    
thanks, works now... –  Alex Aug 28 at 9:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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