簡體   English   中英

Drupal自定義表單獲取並記錄當前用戶

[英]Drupal custom form get and record current user

我上網查找並編寫了一個自定義模塊。

我想添加一個表格,供人們向數據庫中添加一些信息。

我使用“數據”模塊創建一個名為“配置文件”的表。

我模塊的代碼是(addfood.module):

    <?php


/**
 * Implements hook_menu().
 */
function addfood_menu() {
  $items['food/add'] = array(
    'title' => '新增食物檔案',
    'page callback' => 'addfood_page',
    'access callback' => TRUE,
  );
  return $items;
}

/**
 * Implements hook_permission.
 */
function addfood_permission() {
  return array(
    'addfood module' => array(
      'title' => t('Addfood module permission'),
  ));
}

/**
 * Returns form render array.
 */
function addfood_form($form, &$form_state) {
  if (user_access('addfood module')) {
  //Allowed

  $form['name'] = array(
    '#title' => t('名稱'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['calorie'] = array(
    '#title' => t('卡路里'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#description' => t('填寫卡路里(單位:千卡)'),
  );
  $form['water'] = array(
    '#title' => t('水'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['protein'] = array(
    '#title' => t('蛋白質'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['saturated_fat'] = array(
    '#title' => t('飽和脂肪'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['trans_fat'] = array(
    '#title' => t('反式脂肪'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['carbohydrates'] = array(
    '#title' => t('碳水化合物'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['dietary_fiber'] = array(
    '#title' => t('膳食纖維'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['cholesterol'] = array(
    '#title' => t('膽固醇'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['sodium'] = array(
    '#title' => t('鈉'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['sugars'] = array(
    '#title' => t('糖'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['notes'] = array(
    '#title' => t('Please explain your what makes you a prime candidate for our beta test'),
    '#type' => 'textarea',
    '#resizable' => TRUE,
    '#description' => t('Beta spaces are limited, but let us know if there is a really good reason to let you in.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
  } else {
  //Access denied
  header('Location: ../user/login?destination=food/add');
  }
}

/**
 * Menu callback.
 */
function addfood_page() {
  return drupal_get_form('addfood_form');
}

/**
 * Submission handler for form_example -> Insert into database
 */
function addfood_form_submit($form, &$form_state) {
  $fe_id = db_insert('profile')
    ->fields(array(
      'name' => $form_state['values']['name'],
      'calorie' => $form_state['values']['calorie'],
      'water' => $form_state['values']['water'],
      'protein' => $form_state['values']['protein'],
      'saturated_fat' => $form_state['values']['saturated_fat'],
      'trans_fat' => $form_state['values']['trans_fat'],
      'carbohydrates' => $form_state['values']['carbohydrates'],
      'dietary_fiber' => $form_state['values']['dietary_fiber'],
      'cholesterol' => $form_state['values']['cholesterol'],
      'sodium' => $form_state['values']['sodium'],
      'notes' => $form_state['values']['notes'],
    ))
    ->execute();

  drupal_set_message(t('HO GYA Submit!!!!'));  

  return $form;
}
?>

如果我想記錄誰自動添加信息(通過獲取當前用戶名)。

有人知道該怎么做嗎? 謝謝。

圖是我的表架構。
(來源: libk.info

您可以使用全局用戶變量來獲取當前用戶。 您可以對其進行測試:

<?php
global $user;
print_r($user->name);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM