Popular just now

ad1

Drupal 7: Create Forms in a Single Function

I really do not like using Drupals stucture for creating HTML forms automatically. It's too complicated to create and debug. I took the time out in a project to find an easier way. Here's the code for having the form print out directly when the method is called:





$arguments = array(
            'old_flags' => array(
                $school->fid => $old_school_id,
                $location->fid => $old_location_id),
            'new_flags' => array(
                $school->fid => $new_location_school_id,
                $location->fid => $new_location_id),
            'message' => t('Your accounts location has been updated.'),
            'process_message' => t('Notice! Your accounts school has been changed to match the new location. Other available locations for this school are listed below.'),
            'submission_path' => 'zsareads/tutor/tutor_action_confirm_submit',
            'redirect' => 'tutor/school/' . $new_location_school_id);

$this->tutor_action_confirm_form($arguments);

function tutor_action_confirm_form($args = array())
    {

        //exit('
' . print_r($args, 1) . '
'); // Arguments from url/get to post foreach($args as $key => $argument) { if(is_array($argument)) { foreach($argument as $item => $value) { $form['argument'][$key][]= array( '#type' => 'hidden', '#name' => $key . '[' . $item . ']', '#value' => $value ); } } else { $form['argument'][] = array( '#type' => 'hidden', '#name' => '' . $key, '#value' => $argument); } } // Submit $form['submit'] = array( '#type' => 'submit', '#value' => 'Confirm', ); $form_main = drupal_render($form); $variables['element']['#children'] = $form_main; // Action path form $variables['element']['#attributes'] = array( 'method' => 'post', 'action' => url($args['submission_path']) ); //return $form_main; return theme_form($variables); }

Warning! This does not allow the use of hooks in the typical "Drupal Way" of doing things. But it saves you so much headache in the long run.

No comments:

Post a Comment