0
votes

Can anyone advice how best to set a temporary variable with scope to be used between hooks?

I have a custom module with the purpose of checking and warning if an attempted file upload matches an existing file on the system.

I am using hook_form_alter to identify the specific upload form and hook_file_validate to check if the file was previously uploaded.

My problem is those two hooks don't share a common parameter - I need to pass information from hook_form_alter back to hook_validate.

The information I want the functions to share is a simple boolean which should be destroyed immediately the file upload is done/dismissed, so using variable_set to persist it to the database is overkill. I don't believe a session or cookie approach is best either.

UPDATES:

Globals approach:

function duplicate_uploads_warning_init(){
    $GLOBALS['processed'] = 'testing';
}

function duplicate_uploads_warning_file_validate($file){
    drupal_set_message("PROCESSED[file-validate]: {$GLOBALS['processed']}", 'warning');
}

function duplicate_uploads_warning_form_alter(&$form, &$form_state, $form_id){
    if( $form_id == 'file_entity_add_upload' ){
        drupal_set_message("PROCESSED[form-alter]: {$GLOBALS['processed']}", 'error');
        $GLOBALS['processed'] = 'tested';
        $form['#validate'][] = 'duplication_validate';
    }
}

The code above sets GLOBALS[processed] in the init hook and that value in immediately confirmed in the hook_form_alter.

However the attempt to reassign the value to tested fails. The reassigned value is what I hoped to see in hook_file_validate but I still get the initial value of testing.

Hook_form_alter validation approach:

I tried adding a custom validation function but the upload of the image still took place where I intend to stop it. My code is as follows:

function duplication_validate($form, &$form_state) {
    $data = duplicates($form_state['complete form']['upload']['#file']->filename);
    if( sizeof($data) > 0 ){
        form_set_error('test', 'testing validation');
        return false;
    }
}

I can confirm my $data variable has content and the sizeof test returns greater than 0.

2
you can use php's global variables or $_GLOBAL variable to store global values. - Sagar
using a global variable is not a good way. Why are you using hook_file_validate ? Cant you just add your own custom validation function in the hook_form_alter? - 2pha

2 Answers

0
votes

using variable_set to persist it to the database is overkill

I don't agree they are overkill (you can easily delete variables), and as @2pha have mentioned globals are not recommended. I think you could use variable_set($name, $value), variable_get($name), and variable_del($name) without worrying unless you need to super-optimise your sites database queries. If you really don't want to use the variable_* functions, then maybe cache_set() and cache_get() might work because you can give it a temporary status.

Edit, variables approach:

function duplicate_uploads_warning_init(){
    $account = \Drupal::currentUser();
    variable_set($account->id() . '_processed', 'testing');
}

function duplicate_uploads_warning_file_validate($file){
    $account = \Drupal::currentUser();
    $processed_var = variable_get($account->id() . '_processed');
    drupal_set_message("PROCESSED[file-validate]: {$processed_var}", 'warning');
}

function duplicate_uploads_warning_form_alter(&$form, &$form_state, $form_id){
    if( $form_id == 'file_entity_add_upload' ){
        $account = \Drupal::currentUser();
        $processed_var = variable_get($account->id() . '_processed');
        drupal_set_message("PROCESSED[form-alter]: {$processed_var}", 'error');
        variable_set($account->id() . '_processed', 'tested');
        $form['#validate'][] = 'duplication_validate';
    }
}

… and in a success or #submit callback function run variable_del($account->id() . '_processed') to delete the variable.

0
votes

maybe $GLOBALS['processed'] was initialized with the static keyword elsewhere....?