By TheGorf on
I'm completely stumped by what's going on here. I have a very simple module that absolutely will NOT update the admin page. If I put the update commands (variable_set()) into the _validate function it works fine. If i put it into the _submit function I get nothing. It's like the _submit function doesn't even get called like http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6 says it should. The module tutorial (http://drupal.org/node/206761) has little to nothing to explain what to do after the validation step. Anyone care to offer some insight into why this doesn't work?
<?php
/**
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function pnwhs_mm_help($path, $arg) {
$output = ''; //declare your output variable
switch ($path)
{
case "admin/help#pnwhs_mm":
$output = '<p>'. t("Displays the next coming monthly meeting.") .'</p>';
break;
case 'admin/settings/modules#description':
$output = t('Configure upcoming monthly meeting information.');
break;
case 'admin/settings/pnwhs_mm':
$output = t('<p>Each monthly meeting is set month by month. Once the date of the current meeting is past the system automatically resets and displays "to be announced" until this date is reset.</p>');
break;
}
return $output;
} // function pnwhs_mm_help
/**
* Valid permissions for this module
*/
function pnwhs_mm_perm()
{
return array('Change monthly meeting status');
} // function pnwhs_mm_perm()
/**
* Implementation of hook_menu().
*/
function pnwhs_mm_menu() {
$items = array();
$items['admin/settings/pnwhs_mm'] = array(
'title' => 'PNWHS Monthly Meeting',
'description' => 'Edit information about the next meeting.',
'page callback' => 'drupal_get_form',
'page arguments' => array('pnwhs_mm_admin_settings_form'),
'access arguments' => array('Change monthly meeting status'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implementation of hook_block
* @param string $op one of "list", "view", "save" and "configure"
* @param integer $delta code to identify the block
* @param array $edit only for "save" operation
**/
function pnwhs_mm_block($op='list', $delta=0) {
if ($op == "list") {
// Generate listing of blocks from this module, for the admin/block page
$block = array();
$block[0]["info"] = t('PNWHS Monthly meeting');
return $block;
}
else if ($op == 'view')
{
$block_content = '<p>Next Meeting<br />'.'!</p><p>'.variable_get('pnwhs_mm_short_speaker', 'BOB!').'</p>';
$block_content .= '<p>Herp of the month:<br />'.variable_get('pnwhs_mm_short_hotm', 'BOB!').'</p>';
$block_content .= '<p>'.l('More info', 'MonthlyMeeting').'</p>';
// set up the block
$block['subject'] = 'Monthly Meeting';
$block['content'] = $block_content;
return $block;
}
}
/**
* Menu breadcrumb admin settings form.
*
* @return
* The settings form used by Menu breadcrumb.
*/
function pnwhs_mm_admin_settings_form()
{
cache_clear_all();
$default = "To be announced!";
$form['pnwhs_mm_speaker'] = array(
'#type' => 'textarea',
'#title' => t('Main Speaker'),
'#default_value' => variable_get('var_pnwhs_mm_speaker', $default),
'#cols' => 60,
'#rows' => 5,
'#description' => t('The primary speaker for this coming months meeting.')
);
$form['pnwhs_mm_short_speaker'] = array(
'#type' => 'textfield',
'#title' => t('Main Speaker - Short'),
'#default_value' => variable_get('var_pnwhs_mm_short_speaker', $default),
'#size' => 60,
'#maxlength' => 160,
'#description' => t('A short description of the speaker that appears on the sidebar.')
);
$form['pnwhs_mm_hotm'] = array(
'#type' => 'textarea',
'#title' => t('Herp of the Month'),
'#default_value' => variable_get('var_pnwhs_mm_hotm', $default),
'#cols' => 60,
'#rows' => 5,
'#description' => t('Description of the Herp of the Month.')
);
$form['pnwhs_mm_short_hotm'] = array(
'#type' => 'textfield',
'#title' => t('HotM - Short'),
'#default_value' => variable_get('var_pnwhs_mm_short_hotm', $default),
'#size' => 60,
'#maxlength' => 160,
'#description' => t('A short description of the HotM that appears on the sidebar.')
);
$form['pnwhs_mm_announce'] = array(
'#type' => 'textarea',
'#title' => t('Announcements'),
'#default_value' => variable_get('var_pnwhs_mm_announce', $default),
'#cols' => 60,
'#rows' => 5,
'#description' => t('Additional announcements for this meeting.')
);
return system_settings_form($form);
}
function pnwhs_mm_admin_settings_form_validate($form, &$form_state)
{
if ($form_state['values']['pnwhs_mm_short_hotm'] == '') {
form_set_error('pnwhs_mm_short_hotm', t('A short description for Herp of the Month must be entered.'));
}
if ($form_state['values']['pnwhs_mm_short_speaker'] == '') {
form_set_error('pnwhs_mm_short_speaker', t('A short description for the speaker must be entered.'));
}
}
function pnwhs_mm_admin_settings_form_submit($form, &$form_state)
{
print "TESTTESTTESTFUCKINGPRINTSOMETHING!!!!!!!!!!!!!!!!!!!!!";
variable_set('var_pnwhs_mm_speaker', $form_state['values']['pnwhs_mm_speaker']);
variable_set('var_pnwhs_mm_short_speaker', $form_state['values']['pnwhs_mm_short_speaker']);
variable_set('var_pnwhs_mm_hotm', $form_state['values']['pnwhs_mm_hotm']);
variable_set('var_pnwhs_mm_short_hotm', $form_state['values']['pnwhs_mm_short_hotm']);
variable_set('var_pnwhs_mm_announce', $form_state['values']['pnwhs_mm_announce']);
}
Comments
Submit handler
Just before you return your form array you are passing it through system_settings_form(). This might be replacing your #submit handler.
Using system_settings_form() will avoid you needing to have your own submit handler as it will use its submit handler to set all your variables.
To see it in action change your #default_values like so the variable key matches the field key like so
http://api.drupal.org/api/function/system_settings_form/6
Alternatively just return $form instead of return system_settings_form($form);
Oh ho! I see! Very nice,
Oh ho! I see! Very nice, thank you sir!
Using return
Using
return system_settings_form($form)removes the necessity for a submit function, the reason being that all values are saved to the variables table. For example, let's look at this:What this will do is add a new variable to the variables table with the name of the form element. In this case, the form element was called 'enable_something', as was defined in the line
$form['enable_something']. As such, the value set in this element can be retrieved anywhere on the site using the following line:Contact me to contract me for D7 -> D10/11 migrations.