1

I'm uploading a file, to process and display a message at the end. However, the messages that drupal_set_message is set to display are displayed sometimes and don't display sometimes, even, if the processing runs successfully in the background. The reload time restriction isn't a problem as it is set to multiple minutes and the processing hardly takes 10-60 seconds on average. Is there a way to force the page to show the messages at any cost? The drupal_set_messages that I am concerned about are the last two drupal_set_messages in the code. I've even tried using drupal_set_message(t('!shell_c', array('!shell_c'=>$shell_command)), 'status'); but the issue persists where the drupal_set_message messages are shown sometimes and not shown sometimes, rather erratically.

//Before this part of the code, there is function to create Menu ($items[])
function g_p_proc_form($form, &$form_state, $values) {
    $form['#attributes']['enctype'] = 'multipart/form-data';
    $form['filter'] = array (
        "#type" => "fieldset",
        "#title" => t("File Process <BR> "),
        "#collapsible" => TRUE,
    );
  // without this attribute, upload may fail on submit
    $form['#attributes']['enctype'] = 'multipart/form-data';
    $form['filter']['file'] = array(
        // '#title' => t('File processing'),
        '#type'  => 'file',
        '#name' => 'files[]',
        '#description' => t('Select Submit to start processing the file'),
        '#type'  => 'file'),
        '#upload_location' => 'public://',
        '#attributes' => array('multiple' => 'multiple'),
    );
    // echo '<script type="text/javascript">Drupal.behaviors.DSU_batch_report;</script>'; //christ
    if (isset($_SERVER['CONTENT_LENGTH']) && (int) $_SERVER['CONTENT_LENGTH'] > 'size based on my requirements') {
        drupal_set_message(t(" Warning message on file size"));));
        // unset($_SERVER['CONTENT_LENGTH']);
    }


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

    );


    // drupal_set_message(t("in fn"));

    return $form;
}

function g_p_proc_form_submit($form, &$form_state) {
    header(current page)
    try{
        $number_file = count($_FILES['files']['name']);
        $f_name = array();
        for ($i=0; $i<$number_file;$i++)
        {
            $file = file_save_upload($i, array('file_validate_extensions' => array('pdf PDF')), 'public://', FILE_EXISTS_REPLACE);
            $f_name[] = $_FILES['files']['name'][$i]; //[]
        }
            }catch (Exception $error){
        echo 'Exception: ',  $error->getMessage(), "\n";
    }
    $n_f = count($f_name);
    drupal_set_message(t('Number of files to process: !n_f', array('!number_f'=>$n_f)));
    $fn_name = 'g_p';
    $fn_call = $fn_name($f_name);
function g_p($f_name=array()){
    $curr_l = getcwd();
    chdir('My file processing PHP script location ');
    $n_f = count($f_name);
    $shell_command = shell_exec("file_processing_php_script_name.php");
    //header("Location: "Current file processing module menu site"); //Commented out
    drupal_set_message($shell_command); //print_r statements from the file_processing_php_script_name.php
    chdir($curr_l);
}
1
  • Sometimes a message can be "consumed" by a background request, e.g. as part of a 404 that was meant to deliver an image or css file. You can use the "Network" tab in your browser's developer tools to look for failing requests. Or use PHP debugging to find out when drupal_get_message() is called.
    – donquixote
    Aug 24, 2020 at 20:24

1 Answer 1

0

It sounds like you need to maintain a stable message storage solution until the operation is complete.

The D7 core message functions drupal_set_message() & drupal_get_messages() (the core function used when retrieving messages for rendering) are pushing/popping messages to session var, $_SESSION['messages'] These messages are designed around a single page request/received operation.

If there many different async requests going on with the same session (or other modules calling drupal_get_messages() with its default options), its likely $_SESSION['messages'] is being cleared out before rendering when you need it.

Try storing your messages somewhere else (e.g. another session variable) then call drupal_set_message() with your messages once the full operation is complete. If most messages are background ops, you should use consider watchdog() for logging background ops and then use drupal_set_message() for a "operation complete" message.

1
  • Thanks for your detailed answer, I tried to implement this but the issue still persists. Could it be because of caching? If so, is there a way for me to prevent this issue (please understand that I am new to Drupal/web development and don't know much to fix this)? Jul 14, 2020 at 1:56

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.