Drupal 6 Form API - Passing Values

190 Views Asked by At

I'm trying to write a custom module in Drupal 6 and trying to add a simple-ish form in the admin area using the form API.

I've got the form showing but can't seem to get the from to populate itself again when submitting and then reloading the page. I tried passing in a second parameter of &$form_state to my hook_form function but it results in an error about a missing 2nd parameter.

I'm sure this is something simple but I can't quite figure it out, here's my .module code:

/*
* Implements hook_init().
*/

function movehub_featured_links_block_init() {

    drupal_add_css(drupal_get_path('module', 'movehub_featured_links_block') . '/assets/css/movehub-featured-links-block-style.css');

}

/**
* Implements hook_theme().
*/

function movehub_featured_links_block_theme() {

    return array(
        'movehub_featured_links_block_manage' => array(
            'template' => 'movehub-featured-links-block-admin-manage'
        ),
        'movehub_featured_links_block_add' => array(
            'template' => 'movehub-featured-links-block-admin-add',
            'arguments' => array('link_id' => null)
        ),
    );

}

function movehub_featured_links_block_manage() {

    return theme('movehub_featured_links_block_manage');

}

function movehub_featured_links_block_add_form(&$form) {print_r($form);

    $form = array();

    $form['#attributes'] = array('enctype' => 'multipart/form-data');

    $form['link_title'] = array(
        '#type' => 'textfield',
        '#title' => t('Link Title')
    );

    $form['image'] = array(
        '#type' => 'file',
        '#title' => t('Image')
    );

    $form['image_hover'] = array(
        '#type' => 'file',
        '#title' => t('Hover Image')
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Add!')
    );

    return $form;

}

/**
* Implements hook_menu().
*/

function movehub_featured_links_block_menu() {

    $items = array();

    $items['admin/featured_links'] = array(
        'title' => 'Featured Links',
        'description' => 'Add, view and edit featured links..',
        'page callback' => 'movehub_featured_links_block_manage',
        'page arguments' => array('movehub_featured_links_block_admin'),
        'access arguments' => array('administer movehub_featured_links_block settings'),
        'type' => MENU_NORMAL_ITEM,
    );

    $items['admin/featured_links/manage'] = array(
        'title' => 'Manage',
        'description' => 'Add, view and edit featured links.',
        'page callback' => 'movehub_featured_links_block_manage',
        'page arguments' => array('movehub_featured_links_block_admin'),
        'access arguments' => array('administer movehub_featured_links_block settings'),
        'type' => MENU_NORMAL_ITEM,
    );

    $items['admin/featured_links/add'] = array(
        'title' => 'Add',
        'description' => 'Add a new featured link.',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('movehub_featured_links_block_add_form'),
        'access arguments' => array('administer movehub_featured_links_block settings'),
        'type' => MENU_CALLBACK
    );

    return $items;

}

function movehub_featured_links_block_add_form_submit($form, &$form_state) {



}

function movehub_featured_links_block_add_form_validation($form, &$form_state) {



}
2

There are 2 best solutions below

0
On BEST ANSWER

Managed to find the very simple error that caused me this issue - namely that I had used the wrong hook name when validating.

I had used:

movehub_featured_links_block_add_form_validation

It should be:

movehub_featured_links_block_add_form_validate

The key part being the use of hook_validate instead of hook_validation. Rookie error.

0
On

In your form function add the '#default_value' for each field. (See Drupal 6 Form API #default_value for more info)

Your field definition in the form will then look something like this:

$form['link_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Link Title'),
    '#default_value' => 'My custom link title',
);

The Form API should then populate it with the given default value when first loading the form. Doing it in this way, if the user fails validation and is served the form back to correct their mistakes, the form values should contain whatever updated text they previously entered. Drupal populates the form using the form_state when refreshing the form to display, so you shouldn't have to worry about that side of things if you just set the #default_value.