Drupal自定义表单获取并记录当前用户

问题描述 投票:0回答:1

我在互联网上查询并编写了一个自定义模块。

我想添加一个表单,以便人们将一些信息添加到数据库中。

我使用“数据”模块创建一个名为“个人资料”的表。

我的模块代码是(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

mysql forms module drupal-7
1个回答
1
投票

您可以使用全局用户变量来获取当前用户。您可以对其进行测试:

<?php
global $user;
print_r($user->name);
?>
© www.soinside.com 2019 - 2024. All rights reserved.