views:

107

answers:

2

I am writing a module for my organization to cache XML feeds to static files to an arbitrary place on our webserver. I am new at Drupal development, and would like to know if I am approaching this the right way.

Basically I:

  1. Expose a url via the menu hook, where a user can enter in a an output directory on the webserver and press the "dump" button and then have PHP go to drupal and get the feed xml. I don't need help with that functionality, because I actually have a prototype working in Python (outside of Drupal)..
  2. Provide a callback for the form where I can do my logic, using the form parameters.

Here's the menu hook:

function ncbi_cache_files_menu() {
    $items = array();


    $items['admin/content/ncbi_cache_files'] = array(
        'title' => 'NCBI Cache File Module',
        'description' => 'Cache Guide static content to files',
        'page callback' => 'drupal_get_form',
        'page arguments' => array( 'ncbi_cache_files_show_submit'),
        'access arguments' => array( 'administer site configuration' ),
        'type' => MENU_NORMAL_ITEM,
    );

    return $items;
}

I generate the form in:

function ncbi_cache_files_show_submit() {

    $DEFAULT_OUT = 'http://myorg/foo';
    $form[ 'ncbi_cache_files' ] = array(
        '#type' => 'textfield',
        '#title' => t('Output Directory'),
        '#description' => t('Where you want the static files to be dumped.
            This should be a directory that www has write access to, and
            should be accessible from the foo server'),
        '#default_value' => t( $DEFAULT_OUT ),
        '#size' => strlen( $DEFAULT_OUT ) + 5,
    );

    $form['dump'] = array( 
        '#type' => 'submit',
        '#value' => 'Dump',
        '#submit' => array( 'ncbi_cache_files_dump'),
    );

    return system_settings_form( $form );
}

Then the functionality is in the callback:

function ncbi_cache_files_dump( $p, $q) {
    //dpm( get_defined_vars() );
    $outdir = $p['ncbi_cache_files']['#post']['ncbi_cache_files'];
    drupal_set_message('outdir: ' . $outdir );

}

The question: Is this a decent way of processing an arbitrary form in Drupal? I not really need to listen for any drupal hooks, because I am basically just doing some URL and file processing.

What are those arguments that I'm getting in the callback ($q)? That's the form array I guess, with the post values? Is this the best way to get the form parameters to work on?

Thanks for any advice.

A: 

You can process forms in two stages, validate and submit.

Validate is for when you want to validate some user provided and raise form errors if some user input was invalid (like an invalid url or email address)

Submit which is the one you use is called if a form passes all of its validations, so at that point if you made a proper validation you will know that the data supplied by the user is okay.

Your submit function should look like this:

function ncbi_cache_files_dump(&$form, &$form_state) {
    // $form: an array containing the form data
    // $form_state: data about the form, like the data inputted in the form etc.
    // code...
}
googletorp
A: 

I think you need 2 separate forms here:

  1. for setting the directory (the one you have now);
  2. for making a dump (another form that would use the configured path).

Also it seems logical to publish the previously saved path as the default value in the settings form (instead of a hard-coded path).

And in general you should check the form input data from the second parameter of the submit callback:

function ncbi_cache_files_dump(&$form, &$form_state) {
    $outdir = $form_state['values']['ncbi_cache_files'];
    // ...
}
Kniganapolke