0

I built a custom form that works well, however I updated my custom module to create a custom content type and add the form via hook_node_view().

Everything works, until I access the $node object in the form (e.g. to set default values).

Scenario 1: The node is created but we do not access $node object to set default values. In this case everything works.

Scenario 2: The node is created and we do access $node object to set values in the $form. In this case ajax breaks.

See below for code:

function quickform_node_info(){
    $t = get_t();
    $items = array(
        'quickform' => array(
            'name' => $t('MAGNIFI QuickForms'),
            'base' => 'quickform',
            'description' => $t('Create Artist, Photo, Music, and Record Label Forms'),
            'help' => '',
            'has_title' => 1,
            'title_label' => $t('Record Label Name'),
            'locked' => 0,
        )
    );
    drupal_alter('node_info', $items);
    return $items;
}

function quickform_node_view($node, $view_mode, $langcode){
    $form = drupal_get_form('quickmulti_form', $node);
    $node->content['quickmulti_form'] = array(
        $form
    );
}

function quickmulti_form($form, &$form_state, $this_node = false) {
    #$this_nod

    $form = array();
    #include this entire file whenever this form is included
    #form_load_include($form_state, '.php',        'quickform','quickform.module');
    $form['#tree'] = TRUE;
    $_SESSION["anonymous-tracker"]=time();
    $form['#attributes']['enctype'] = 'multipart/form-data';

    $form['headingtext1'] = array(
        '#markup' => t('PROMOTE ON MAGNIFI'),
        '#prefix' => '<h1>',
        '#suffix' => '</h1>',
    );

    $form['headingtext2'] = array(
        '#markup' => t('Join over 17,000 artists that are leveraging the MAGNIFI platform to connect with concert-going fans. MAGNIFI is 100% focused on connecting music to concert dates and sales! '),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
    );

    $form['headingtext3'] = array(
        '#markup' => t('Fill out the below form to get started.'),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
    );

    $form['contact_info']=array(
        '#type'=>'fieldset',
        '#title'=>t('Contact Info'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE
        );

    $form['contact_info']['first_name'] = array(
        '#type' => 'textfield', 
        '#title' => t('First Name'),
        '#size' => 255,
        '#maxlength' => 255,
        #'#required' => TRUE, 
    );

    $form['contact_info']['last_name'] = array(
        '#type' => 'textfield', 
        '#title' => t('Last Name'),
        '#size' => 255,
        '#maxlength' => 255,
        #'#required' => TRUE, 
    );

    $form['contact_info']['email1'] = array(
        '#type' => 'textfield', 
        '#title' => t('Email'),
        '#size' => 255,
        '#maxlength' => 255,
        #'#required' => TRUE, 
    );

    $form['artist_info']=array(
        '#type'=>'fieldset',
        '#title'=>t('Your Artists'),
        '#description'=>t('All fields are required'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#prefix' => '<div id="artists-wrapper">', 
        '#suffix' => '</div>',
        '#tree' => TRUE,
    );

    $form['artist_info']['tos_c'] = array(
        '#type' => 'checkbox', 
        '#title' => t('I Accept'),
        '#size' => 255,
        '#maxlength' => 255,
        #'#required' => TRUE, 
    );

    $form['artist_info']['tos_p'] = array(
        '#markup' => t("I certify that (1) I am authorized to submit this webform as the Artist named herein or on behalf of this Artist; (2) this Artist controls all rights in the compositions, recordings and images authorized in this webform for MAGNIFI's service, or this Artist has the necessary permissions from all rightsholders to so authorize; (3) I understand and agree as this Artist or on behalf of this Artist to the MAGNIFI Artist Terms of Use Agreement, and warrant that the compositions, recordings, images and other content & information provided in this webform are not in violation of the MAGNIFI Artist Terms of Use Agreement."),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
    );

    // artists_count =  added by internal user, defaults to 1
    // next_artists = for end user to add more
     if(!empty($this_node->field_artist_names['und'][0])){
        $form_state['artists_count'] = count($this_node->field_artist_names['und']);
        $form_state['next_artists'] = $form_state['artists_count'] + 1;
    }elseif (empty($form_state['artists_count'])){ 
        $form_state['artists_count'] = 1; 
        $form_state['next_artists'] = $form_state['artists_count'] + 1;
    }

    $total_artists = $form_state['artists_count'];

    for ($x = 0; $x < $total_artists; $x++){
        // Grab node information
        if(!empty($this_node->field_artist_names['und'][$x])){
                $this_artist = $this_node->field_artist_names['und'][$x];
            }
        $form['artist_info'][$x] = array(
            '#type' => 'container',
            '#prefix' => "<div id='artist{$x}'>", #should be different for each artist
            '#suffix' => '</div>',
            '#tree' => TRUE,
        );

        $form['artist_info'][$x]['artist'] = array(
            '#type'=>'fieldset',
            '#title'=>t('Artist @num', array('@num' => $x+1)),
            #'#description'=>t('All fields are required'),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
        );

        $form['artist_info'][$x]['artist']['account_name'] = array(
            '#type' => 'textfield', 
            '#title' => t('Artist @num Name', array('@num' => $x+1)),
            '#size' => 255,
            '#maxlength' => 255,
            #'#required' => TRUE, 
            '#default_value' => (!empty($this_artist) ? $this_artist : ''), 

        );

        $form['artist_info'][$x]['artist']['music_text'] = array(
            '#markup' => t('List titles in the same order as music files, one per line.'),
            '#prefix' => '<p>',
            '#suffix' => '</p>',
        );

        $form['artist_info'][$x]['artist']['titles'] = array(
            '#type' => 'textarea', 
            '#title' => t('Album/Track Titles'),
            #'#size' => 255,
            #'#maxlength' => 255,
            #'#required' => TRUE, 
        );

        $form['artist_info'][$x]['artist']['music'] = array(
            '#type' => 'plupload',
            #'#title' => t('Upload files'),
            '#name' => 'music[]',
            '#description' => t('Submit high-quality music files.'),
            '#autoupload' => FALSE,
            '#autosubmit' => FALSE,
            '#submit_element' => '#submit',
            '#upload_validators' => array(
                'file_validate_extensions' => array('mp3 wav aiff aac m4a ogg flac'),
                'plupoad_validator' => array(),
            ),
            '#plupload_settings' => array(
                'runtimes' => 'html5, html4',
                'chunk_size' => '20mb',
                'unique_names' => TRUE,
                'max_file_size' => file_upload_max_size() . '100000000',
                'prevent_duplicates' => TRUE,

            ),
            '#event_callbacks' => array(
                #'FilesAdded' => 'Drupal.mymodule.filesAddedCallback',
                'UploadComplete' => 'quickjoin_form_submit',
            ),
        );

        $form['artist_info'][$x]['artist']['photos'] = array(
            '#type' => 'plupload',
            #'#title' => t('Upload files'),
            '#name' => 'photos[]',
            '#description' => t('Submit high-quality photos.'),
            '#autoupload' => FALSE,
            '#autosubmit' => FALSE,
            '#submit_element' => '#submit',
            '#upload_validators' => array(
                'file_validate_extensions' => array('png jpg jpeg'),
                'plupoad_validator' => array(),
            ),
            '#plupload_settings' => array(
                'runtimes' => 'html5, html4',
                'chunk_size' => '20mb',
                'unique_names' => TRUE,
                'max_file_size' => file_upload_max_size() . '100000000',
                'prevent_duplicates' => TRUE,

            ),
            '#event_callbacks' => array(
                #'FilesAdded' => 'Drupal.mymodule.filesAddedCallback',
                'UploadComplete' => 'quickjoin_form_submit',
            ),
        );


    }#end artist_count loop

    $next_artist = $form_state['artists_count'];

    $form['artist_info'][$next_artist] = array(
        '#type' => 'hidden',
        '#title' => "add another artist",
        '#name' => "artist{$next_artist}",
        '#prefix' => "<div id='artist{$next_artist}'>", #should be different for each artist
        '#suffix' => '</div>',
    );

    if($form_state['next_artists'] == $form_state['artists_count']){
        $x = $form_state['next_artists'];
        $form_state['next_artists']++;

        $form['artist_info'][$x] = array(
            '#type' => 'container',
            '#prefix' => "<div id='artist{$x}'>", #should be different for each artist
            '#suffix' => '</div>',
            '#tree' => TRUE,
        );

        $form['artist_info'][$x]['artist'] = array(
            '#type'=>'fieldset',
            '#title'=>t('Artist @num', array('@num' => $x)),
            #'#description'=>t('All fields are required'),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
        );

        $form['artist_info'][$x]['artist']['account_name'] = array(
            '#type' => 'textfield', 
            '#title' => t('Artist @num Name', array('@num' => $x)),
            '#size' => 255,
            '#maxlength' => 255,
            #'#required' => TRUE,   
        );

        $form['artist_info'][$x]['artist']['music_text'] = array(
            '#markup' => t('List titles in the same order as music files, one per line.'),
            '#prefix' => '<p>',
            '#suffix' => '</p>',
        );

        $form['artist_info'][$x]['artist']['titles'] = array(
            '#type' => 'textarea', 
            '#title' => t('Album/Track Titles'),
            #'#size' => 255,
            #'#maxlength' => 255,
            #'#required' => TRUE, 
        );

        $form['artist_info'][$x]['artist']['music'] = array(
            '#type' => 'plupload',
            #'#title' => t('Upload files'),
            '#name' => 'music[]',
            '#description' => t('Submit high-quality music files.'),
            '#autoupload' => FALSE,
            '#autosubmit' => FALSE,
            '#submit_element' => '#submit',
            '#upload_validators' => array(
                'file_validate_extensions' => array('mp3 wav aiff aac m4a ogg flac'),
                'plupoad_validator' => array(),
            ),
            '#plupload_settings' => array(
                'runtimes' => 'html5, html4',
                'chunk_size' => '20mb',
                'unique_names' => TRUE,
                'max_file_size' => file_upload_max_size() . '100000000',
                'prevent_duplicates' => TRUE,

            ),
            '#event_callbacks' => array(
                #'FilesAdded' => 'Drupal.mymodule.filesAddedCallback',
                'UploadComplete' => 'quickjoin_form_submit',
            ),
        );

        $form['artist_info'][$x]['artist']['photos'] = array(
            '#type' => 'plupload',
            #'#title' => t('Upload files'),
            '#name' => 'photos[]',
            '#description' => t('Submit high-quality photos.'),
            '#autoupload' => FALSE,
            '#autosubmit' => FALSE,
            '#submit_element' => '#submit',
            '#upload_validators' => array(
                'file_validate_extensions' => array('png jpg jpeg'),
                'plupoad_validator' => array(),
            ),
            '#plupload_settings' => array(
                'runtimes' => 'html5, html4',
                'chunk_size' => '20mb',
                'unique_names' => TRUE,
                'max_file_size' => file_upload_max_size() . '100000000',
                'prevent_duplicates' => TRUE,

            ),
            '#event_callbacks' => array(
                #'FilesAdded' => 'Drupal.mymodule.filesAddedCallback',
                'UploadComplete' => 'quickjoin_form_submit',
            ),
        );

    }

    //Add another file button
    $form['artist_info']['add_more_artists'] = array(
        '#type' => 'submit',
        '#name' => "add_more_artists{$next_artist}",
        '#value' => t('Add Another Artist'),
        '#submit' => array('artists_add_more'), #increment $form_state['artists_count']
        '#ajax' => array(
            'callback' => 'artists_add_more_callback', #function that returns elements
            'wrapper' => "artist{$next_artist}", #replace this wrapper with the returned elements
            'method' => 'append',
        ),
    );

    $form['submit'][] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );

    $form['#submit'][] = 'quickjoin_form_submit';
    return $form;
}

function artists_add_more($form, &$form_state) {
    $form_state['artists_count']++;
    $form_state['rebuild'] = TRUE;
}

function artists_add_more_callback($form, &$form_state) {
    $this_artist = $form_state['artists_count'];
    $form_state['rebuild'] = TRUE;
    return $form['artist_info'][$this_artist];
}
  • 1
    When you say ajax breaks, what do you mean exactly? Also are there any errors in your watchdog or server logs? – rooby Aug 5 '15 at 4:30
  • found the error: I was accessing '$this_node->field_artist_names['und'][$x]' instead of '$this_node->field_artist_names['und'][$x]['value'] – noah Aug 5 '15 at 4:59
  • wow, what a way to waste a day ;) – noah Aug 5 '15 at 5:00
  • by the way, what was happening is that it was inserting empty <div></div> elements instead of adding a new artist field set – noah Aug 5 '15 at 5:01
  • If you add that as an answer and select it as the correct answer it will mark this question solved. – rooby Aug 5 '15 at 6:09
0

I found the error : I was accessing $this_node->field_artist_names['und'][$x] instead of $this_node->field_artist_names['und'][$x]['value'].

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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