2

I am new to drupal, currently learning about example module and its sub module, I already created the form using custom modules, in that form I wish to add progress bar for submit button, i referred many sites but, i can't get any clear idea, what i did for submit button is,

function custom_form_menu() {
$items = array();
$items['think/form'] = array(
'title' => t('Entry Free'),
'page callback' => 'custom_form_form',
'page arguments' => array('ajax_example_progressbar_form'),
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
'file' => 'ajax_example_progressbar.inc',
);
return $items;
}

and for submit button,

custom_form_my_form($form , &$form_state) {
$form['first'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#description' => "Please enter your first name.",
'#maxlength' => 6,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}

So, now how can I add progress bar in this submit button, when the form is submitted.

1

Progress bar example:

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Import'),
    '#ajax' => array(
        'callback' => 'import_submit_callback',
        'wrapper' => 'import-form',
        'progress' => array(
            'type' => 'bar',
            'message' => t('Importing..'),
            'url' => 'import/progress', //url to fetch current progress percentage
            'interval' => '1000' //progress bar will refresh in 1 second
        )
    ),
);

Then create menu like this for url 'import/progress' :

$items['import/progress'] = array(
    'title' => t('Import progress'),
    'page callback' => 'import_progress',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
);

Then output json data indicating current status like this in that page callback function:

function import_progress() {
    $progress = array(
        'message' => t('Starting import...'),
        'percentage' => -1,
    );

    $completed_percentage = variable_get('import_status'); // calculate progress percentage here

    if($completed_percentage){
        $progress['message'] = t('Importing...');
        $progress['percentage'] = $completed_percentage ;
    }

    drupal_json_output($progress);
}

Further reading: https://www.drupal.org/node/1401966

  • import_progress() do not display anything – code.rider Aug 31 '16 at 10:27

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.