Drupal obsługa formularza w module
Fragmenty kodu będą dotyczyły konkretnego przykładu (nie należy zwracać uwagi na pola formularza itp.) Najpierw należy stworzyć sam formularz w funkcji (nazwa funkcji: nazwamodułu_nazwafun):
function mysettings_points() { $form = array(); global $user; $mode = arg(3); $timestamp = format_date(time(), 'custom', 'Y-m-d H:i O'); $uid = (int)arg(4); $txn_user = user_load(array('uid' => $uid)); $form['nr_karty'] = array( '#type' => 'textfield', '#title' => t('Nr karty'), '#size' => 30, '#maxlength' => 60, ); /*$form['txn_user'] = array( '#type' => 'textfield', '#title' => t('User Name'), '#size' => 30, '#maxlength' => 60, '#default_value' => !empty($txn_user->name) ? $txn_user->name : '', '#autocomplete_path' => 'user/autocomplete', '#description' => t('User Name for the user you want the !points to affect', userpoints_translation()), );*/ $form['points'] = array( '#type' => 'textfield', '#title' => t('Points'), '#size' => 10, '#maxlength' => 10, '#default_value' => isset($txn->points) ? $txn->points : 0, '#description' => t('Number of !points to add/subtract from the user. For example, 25 (to add !points) or -25 (to subtract !points).', userpoints_translation()), ); $form['time_stamp'] = array( '#type' => 'hidden', '#title' => t('Date/Time'), '#default_value' => $timestamp, '#size' => 30, '#maxlength' => 30, '#description' => t('Date and time of this transaction, in the form YYYY-MM-DD HH:MM +ZZZZ'), ); if (module_exists('taxonomy')) { $form['tid'] = array( '#type' => 'select', '#title' => t('Category'), '#options' => userpoints_get_categories(), '#description' => t('Category to apply these !points to', userpoints_translation()), ); } $form['approver_uid'] = array( '#type' => 'hidden', '#value' => $user->uid, ); $form['operation'] = array( '#type' => 'hidden', '#value' => $user->name, ); $form['status'] = array( '#type' => 'hidden', '#value' => USERPOINTS_TXN_STATUS_PENDING, ); $form['mode'] = array( '#type' => 'hidden', '#value' => $mode, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); return $form; }Teraz funkcja odpowiedzialna za obsługę odebrania żądania z formularza (_submit w nazwie funkcji).
function mysettings_points_submit($form, &$form_state) { global $user; if($form_state['values']['form_id'] != 'mysettings_points') { return; } if($form_state['values']['nr_karty']=='') { drupal_set_message('Podaj nr karty'); $form_state['redirect'] = 'site'; return; } $sql = 'SELECT uid FROM profile_values WHERE fid=1 AND value='.$form_state['values']['nr_karty']; $results = db_query($sql); $ident = intval(db_result($results)); $txn_user = user_load($ident); if($txn_user->uid==0) { drupal_set_message('Nieprawidłowy nr karty'); $form_state['redirect'] = 'site'; return; } $params = array( 'points' => $form_state['values']['points'], 'uid' => $txn_user->uid, 'operation' => $user->name, 'tid' => $form_state['values']['tid'], 'time_stamp' => strtotime($form_state['values']['time_stamp']), ); userpoints_userpointsapi($params); drupal_set_message('Punkty przypisane'); $form_state['redirect'] = 'site'; }Wyświetlenie formularza w templacie:
echo drupal_get_form('mysettings_points');