Posted by altrugon on October 20, 2010 at 5:26pm
I'm trying to add AHAH functionality inside a hook_form_alter() to be able to add an icon next to the block titles.
Here is how all this procedure works:
- User Goes to block edit form.
- Inside the "Nice Titles" field set the user first select a style for the icon to be used from a select box.
- The selection of the icon style fire AHAH.
- A set of radio buttons is created (the AHAH result) where there is one icon per radio button.
- The user select one icon from all the available for the selected style.
- After the block form is completed the user submit the form.
Here is the form alter function:
function nice_titles_form_alter(&$form, &$form_state, $form_id) {
global $user;
if (user_access('add nice titles', $user)) {
$is_node = substr($form_id, -10) == 'node_form'; // Check if we are procesing a node
// edit block, add block, create and/or edit node
if ($form_id == 'block_admin_configure' || $form_id == 'block_add_block_form' || $is_node) {
$form['#cache'] = TRUE; // Make sure our form is going to be cached
// get icon's style
$icon_styles = variable_get('nice_titles_styles', array());
// Nice title for block or node?
if ($is_node) {
$nt_type = 'node'; // Nice titles type
$nt_id = $form['nid']['#value']; // Nice titles id
}
else {
$nt_type = 'block'; // Nice titles type
$nt_id = $form['module']['#value'] . '-' . $form['delta']['#value']; // Nice titles id
}
// Get the selected Nice Title from the db or from the $form_state
if (isset($form_state['values']['style'])) {
try {
$nt = array(
'path' => $form_state['values']['style'],
'icon' => 0,
'height' => $icon_styles['sprites'][$form_state['values']['style']]['height'],
'width' => $icon_styles['sprites'][$form_state['values']['style']]['width'],
);
} catch (Exception $e) {
//echo 'Caught exception: ', $e->getMessage(), "\n";
$nt = NULL;
}
}
else {
$all_nice_titles = variable_get('nice_titles' . $nt_type, array());
try {
$nt = $all_nice_titles[$nt_id];
} catch (Exception $e) {
//echo 'Caught exception: ', $e->getMessage(), "\n";
$nt = NULL;
}
}
$sprites_names = array('' => "---");
foreach ($icon_styles['sprites'] as $k => $v) {
$sprites_names[$k] = $v['name'];
}
// build the form
$form['nice_titles'] = array(
'#type' => 'fieldset',
'#title' => t('Nice Titles'),
'#weight' => -17,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#attributes' => array('class' => 'nice-titles'),
);
$form['nice_titles']['style'] = array(
'#type' => 'select',
'#title' => t('Style'),
'#default_value' => $nt['path'],
'#options' => $sprites_names,
'#description' => t('Choose the icon group style that you want to use.'),
'#weight' => 0,
'#submit' => array('nice_titles_save_submit'), // If no javascript action.
'#ahah' => array(
'path' => 'nice_titles/js',
'wrapper' => 'nice-titles-style-icons',
'method' => 'replace',
'effect' => 'fade',
),
);
// Build the Icons array
$radios = _nice_titles_get_style_icons($nt);
$form['nice_titles']['icons'] = array(
'#type' => 'radios',
'#title' => t('Icons'),
'#default_value' => $nt['icon'],
'#options' => $radios['#options'],
'#description' => $radios['#description'],
'#weight' => 1,
//'#theme' => 'nice_titles_icons',
'#prefix' => '<div id="nice-titles-style-icons">',
'#suffix' => '</div>',
);
$form['#submit'][] = 'nice_titles_save_submit';
}
}
return $form;
}So what is happening right now is that the AHAH procedure works fine and the icons get changed every time the user select a different style, however when I submit the form I ended up with a black form just with the "Nice Titles" fieldset displayed.
Can somebody tell me how to solve this puzzle?
Thanks a lot in advance and specially to katbailey for her help and her AHAH post.