Problem mit dem Schreiben von Formulardaten in Datenbank
am 15.12.2011 - 12:50 Uhr in
Hallo,
ich bin bei der Entwicklung meines ersten Drupal-Moduls und habe Probleme. Ich habe die entsprechenden Kapitel in "Pro Drupal Development" von VanDyk gelesen, aber irgendwas klappt bei mir nicht so, wie es soll ;)
Folgender Code in socialwall.module:
function socialwall_menu(){
$items['admin/settings/socialwall'] = array(
'title' => 'Social wall settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('socialwall_admin_settings_general'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'socialwall.admin.inc',
);
$items['admin/settings/socialwall/general'] = array(
'title' => 'General',
'access arguments' => array('administer site comfiguration'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items['admin/settings/socialwall/updates'] = array(
'title' => 'Node updates',
'access arguments' => array('administer site configuration'),
'page callback' => 'drupal_get_form',
'page arguments' => array('socialwall_admin_settings_updates'),
'type' => MENU_LOCAL_TASK,
'file' => 'socialwall.admin.inc',
'weight' => 1,
);
$items['admin/settings/socialwall/statusupdates'] = array(
'title' => 'Status updates',
'access arguments' => array('administer site configuration'),
'page callback' => 'drupal_get_form',
'page arguments' => array('socialwall_admin_settings_statusupdates'),
'type' => MENU_LOCAL_TASK,
'file' => 'socialwall.admin.inc',
'weight' => 2,
);
return $items;
}Ich habe also mehrere Tabs mit Einstellungen.
Folgender Code in socialwall.admin.inc:
<?php
// $Id$
/**
* @file
* Administration page callbacks for the socialwall module.
*/
/**
* Form builder. Configure social wall.
*
* @ingroup forms
* @see system_settings_form().
*/
function socialwall_admin_settings_general(){
(...)
}
/**
* Validate the form of the "general" page
* Number of updates must be a number
*/
function socialwall_admin_settings_general_validate($form, $form_state){
(...)
}
/**
* Handle submission of the genereal settings form
* and saving of the data to the database.
*/
function socialwall_admin_settings_general_submit($form, $form_state){
(...)
}
function socialwall_admin_settings_updates(){
// Get an array of node types with internal names as keys and
// "friendly" names as values
$types = node_get_types('names');
$form['socialwall_test'] = array(
'#type' => 'textfield',
'#title' => t('Test name'),
'#default_value' => variable_get('socialwall_pagename', 'Wall'),
'#description' => t('Here you can enter the name under which the wall will be available'),
'#size' => 20,
'#maxlength' => 25
);
$form['socialwall_updates_users'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the settings for the wall for users'),
'#options' => array(1=>'Publish when new user is created', 2=>'Publish when user is changed', 3=>'Users can like entries', 4=>'Users can comment on entries'),
);
// Create form for each node type which is available
foreach($types as $currentkey => $currentvalue){
$form['socialwall_updates_'.$currentkey] = array(
'#type' => 'checkboxes',
'#title' => t('Select the settings for the wall for '.$currentvalue),
'#options' => array(1=>'Publish when new '.$currentvalue.' is created', 2=>'Publish when '.$currentvalue.' is changed', 3=>'Users can like entries', 4=>'Users can comment on entries'),
);
}
return system_settings_form($form);
}
/**
* Handle submission of the updates settings form
* and saving of the data to the database.
*/
function socialwall_admin_settings_updates_submit($form, $form_state){
$name = $form_state['values']['socialwall_test'];
db_query("INSERT INTO {socialwall_settings} (nodetype) VALUES ('%s')", $name);
}
function socialwall_admin_settings_statusupdates(){
(...)
}
/**
* Validate the form of the "status updates" page
* Maximal length of status updates must be a number
*/
function socialwall_admin_settings_statusupdates_validate($form, $form_state){
(...)
}Ich habe zwei Probleme:
1. $form['socialwall_test'] habe ich eingefügt, um zu schauen, ob ich überhaupt Sachen in die Datenbank schreiben kann. Dem scheint aber nicht so zu sein. Jedenfalls sollte ja, wenn ich submit drücke function socialwall_admin_settings_updates_submit aufgerufen werden und darin die db_query. In der Datenbank tut sich aber nichts. Alle anderen Felder außer nodetype in der Datenbank dürfen NULL sein. Mir kommt es so vor, als ob die Funktion gar nicht aufgerufen wird.
2. Ich weiß nicht genau, in welcher Form ich die Ergebnisse von checkboxes bekomme. Bekomme ich zum Beispiel bei $form['socialwall_updates_users'], wenn ich "Publish when new user is created" und "Users can like entries" anklicke ein array mit 1 und 3 zurück?
Würde mich über Hilfe freuen! Vielen Dank schonmal!
- Anmelden oder Registrieren um Kommentare zu schreiben

Ok. Problem 1 habe ich selber
am 15.12.2011 - 13:16 Uhr
Ok. Problem 1 habe ich selber lösen können...ich darf halt nicht return_system_settings($form) benutzen, sondern einen submit-button definieren und dann einfach return $form. Ich dachte die Funktion hätte einfach den Zweck diese Knöpfe einzufügen, aber so wie es aussieht macht sie noch mehr ;)
Das andere Problem
am 15.12.2011 - 15:01 Uhr
Das andere Problem mittlerweile auch gelöst ;)