AHAH für gethemtes Formular
am 25.05.2009 - 14:12 Uhr in
Moin alle,
ich versuche mich gerade zum ersten mal an AHAH. Allerdings ist schon die Ausgangsposition nicht ganz einfach. Ich habe ein Formular mit einem eigenem Element, welches ich in eine Tabelle theme. Unter der Tabelle wird ein Drop-Down angezeigt. Wird ein Wert im Drop-Down ausgewählt, soll die Tabelle eine weitere Zeile bekommen. Was ich immerhin schon hinbekommen habe ist, das beim ändern des Drop-Down ein Ladebalken angezeigt wird. Das wars dann auch schon...
Das ist mein Form:
<?php
function cardgenerator_manage_game_layouts_form(&$form_state) {
$gameInfo = _get_games_info(arg(4));
$items['layout-wrapper'] = array(
'#tree' => false,
'#prefix' => '<div class="clear-block" id="layout-wrapper">',
'#suffix' => '</div>',
);
foreach ($gameInfo['card_types'] as $cardTypeId => $cardType) {
$items['layout-wrapper'][$cardType['_type']] = array(
'#type' => 'fieldset',
'#title' => $cardType['label'],
'#collapsible' => true,
'#collapsed' => false,
);
$items['layout-wrapper'][$cardType['_type']]['layouts'] = array(
'#type' => 'selected_layout_elements',
'#default_value' => $cardType['game_card_type_id'],
);
$sql = 'SELECT l.id,
l.name,
l.description,
l.card_size_x,
l.card_size_y
FROM mks_crdgen_layouts as l';
$result = db_query($sql);
$options[0] = t('-- Chose layout --');
while ($row = db_fetch_array($result)) {
$options[$row['id']] = $row['name'] . ' (' . $row['card_size_x'] . 'x' . $row['card_size_y'] . ')';
}
$items['layout-wrapper'][$cardType['_type']]['select'] = array(
'#type' => 'select',
'#id' => 'layout-select-'.$cardType['game_card_type_id'],
'#name' => 'layout-select-'.$cardType['game_card_type_id'],
'#options' => $options,
'#attributes' => array('class' => 'layout-select'),
'#ahah' => array(
'path' => '/layout/get-layout-infos',
'event' => 'change',
'progress' => array('type' => 'bar', 'message' => t('Please wait...')),
'wrapper' => 'result-wrapper',
'effect' => 'fade',
)
);
}
if (count($items) > 0) {
$items['submit'] = array(
'#type' => 'submit',
'#id' => 'submit',
'#name' => 'submit',
'#value' => t('Save'),
);
}
/*print_r($items);
echo '<br><br>';*/
return $items;
}
?>Und das die Theme-Funktion für mein Element:
<?php
function theme_selected_layout_elements($element) {
//print_r($element);
$layouts = _get_layouts_for_game_card_type($element['#default_value']);
//print_r($element);
if (count($layouts) == 0) {
$noLayoutsRow[] = array(
'data' => t('There are no layouts selected for this card type.'),
);
$rows[] = $noLayoutsRow;
} else {
foreach ($layouts as $layout) {
$weight = array(
'#type' => 'textfield',
'#id' => 'weight-' . $layout['id'],
'#name' => 'weight-' . $layout['id'],
'#value' => $layout['weight'],
'#attributes' => array('style' => 'display: none;'),
);
$row = array();
$row[] = array('data' => $layout['id']);
$row[] = array('data' => theme('image', file_directory_path().'/card_generator/layouts/'.$layout['id'].'.jpg', $layout['name'], $layout['name']));
$row[] = array('data' => $layout['name']);
$row[] = array('data' => $layout['description']);
$row[] = array('data' => theme('image', drupal_get_path('module', 'cardgenerator') . '/images/kreuz.gif', t('delete'), t('delete')));
$row[] = drupal_render($weight);
}
$rows[] = array('data' => $row, 'class' => 'draggable');
}
unset($row);
// Adding the drop-down-box
/*$row[] = array('data' => theme_layout_select($element['#value'], $element['#value']), 'colspan' => 5);
$rows[] = $row;*/
$head = array(t('Id'), t('Preview'), t('Name'), t('Description'), '', '');
$output = theme('table', $head, $rows, array('id' => 'card-type-' . $element['#default_value'] . '-table'));
drupal_add_tabledrag('card-type-' . $element['#default_value'] . '-table', 'order', 'sibling', 'weight', null, null, false);
return $output;
}
?>Und der AHAH-Callback:
<?php
function ajax_get_layout_info() {
// The form is generated in an include file which we need to include manually.
include_once 'modules/node/node.pages.inc';
// We're starting in step #3, preparing for #4.
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Step #4.
$form = form_get_cache($form_build_id, $form_state);
// adding selected layout
$form['layout-wrapper'][$cardType['_type']]['layouts'] = print_r($_POST, true);
form_set_cache($form_build_id, $form, $form_state);
// Preparing for #5.
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// Step #5.
drupal_process_form($form_id, $form, $form_state);
// Step #6 and #7 and #8.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
$output = drupal_render($form['layout-wrapper'][$cardType['_type']]['layouts']);
// Final rendering callback.
drupal_json(array('status' => TRUE, 'data' => $output));
}
?>Kann mir bitte jemand helfen? Ich weiß nicht so richtig wie ich weiter machen soll, geschweige denn ob ich überhaupt alles richtig gemacht habe (wohl eher nicht).
Beste Grüße
djdops
- Anmelden oder Registrieren um Kommentare zu schreiben

Habs geschafft
am 25.05.2009 - 16:20 Uhr
Ok, ich hab den Fehler selbst gefunden. Ich hab einen falschen Wrapper angegeben... Nun wird die Callback-Funktion aufgerufen.
<?php
function ajax_get_layout_info() {
// Searching in submitted data for added layouts
foreach ($_POST as $key => $value) {
if (strpos($key, 'layout-select') !== null) {
if ($value != 0) {
$gameCardTypeId = str_replace('layout-select-', '', $key);
$insert[] = '(' . $gameCardTypeId . ', ' . $value . ')';
}
}
}
// Bind new layout to game card type
if (count($insert) > 0) {
$sql = 'INSERT INTO mks_crdgen_game_card_type_layouts
(game_card_type_id, layout_id)
VALUES ' . implode(', ', $insert);
db_query($sql);
}
// We're starting in step #3, preparing for #4.
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Step #4.
$form = form_get_cache($form_build_id, $form_state);
// Preparing for #5.
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// Step #5.
drupal_process_form($form_id, $form, $form_state);
// Step #6 and #7 and #8.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
// Final rendering callback.
drupal_json(array('status' => TRUE, 'data' => drupal_render($form)));
}
?>
Hier trage ich die Auswahl des Drop-Down in die DB ein, und will dann das Formular einfach neu laden. form_get_cache() gibt dabei auch das gesammt Formular-Array zurück. drupal_rebuild_form() baut das Formular neu. Dabei wird merkwürdiger Weise nur der Submit-Button berücksichtigt. Das fieldset, mein eigenes Form-Elemen und das select-Element werden nicht geliefert. Kann mir jemand sagen woran das liegt?
Beste Grüße
djdops