第一个Drupal模块 - 一些问题

时间:2010-12-04 00:24:24

标签: drupal forms module

我正在浏览我的第一个Drupal模块并提出几个问题。下面的代码是有效的,但肯定是正在进行中的工作。

  • 我想在用户提交表单后推出一个回复页面, 不只是“drupal_set_message”或其他形式,而是整个页面可能显示来自SQL查询的响应

  • 我还想在页面顶部添加一个标签条,以便用户可以在几个类似的选项中进行选择。

感谢您的帮助或建议。

<?php
/*
* module: my_first_module_v1.module
*
* This module module allows site users to ask a question
*
* This module uses these Drupal API and functions:
* hook_block - Declare a block or set of blocks.
* hook_help - Provide online user help.
* hook_menu - Define menu items and page callbacks.
* hook_form - Display a node editing form.
* hook_validate - Verify a node editing form.
* hook_submit - This is a hook used by node modules. It is called after validation has succeeded and before insert/update.
* It is used to for actions which must happen only if the node is to be saved. Usually, $node is changed in
* some way and then the actual saving of that change is left for the insert/update hooks.
*
* Again, note that I am pretty new with Drupal, so I can make no guarentees
* as for quality or fitness of code.
*/

/************************************
* implementation of Drupal hook_block to declare a Drupal block
*/

function msc_admin_01_block($op = 'list', $delta = 0, $edit = array())
{
// The $op parameter determines what piece of information is being requested. (list/configure/save/view)
// permitted values of $op are:
// # 'list': A list of all blocks defined by the module.
// # 'configure': Configuration form for the block.
// # 'save': Save the configuration options.
// # 'view': Process the block when enabled in a region in order to view its contents

if ($op == 'list') {

$blocks[0] = array(
'info' => t('My first module block 1'), // required value - this shows up in your list of blocks
'cache' => BLOCK_CACHE_PER_ROLE, // flags describing how the block should behave with respect to block caching.
'path' => 'my_first_module_v1', // path
'status' => TRUE, // enabled
'page callback' => 'msc_admin_01_block_2', // path - this has to be unique
'weight' => 0, // relative order on page
'region' => 'left', // default region on the page
'visibility' => 1, // permit the block to be displayed for a given user.
);

$blocks[1] = array(
'info' => t('my first module block 2'),
'cache' => BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE, // caching instructions
'path' => 'msc_admin_01_block', // path
'status' => TRUE,
'weight' => 0, // relative order on page
'page callback' => 'msc_admin_01_block_2', // path - this has to be unique
'visibility' => 0,
'region' => 'left', // default region on the page
'pages' => 'node/*',
);

return $blocks;
}
// If $op is "configure", we need to provide the administrator with a
// configuration form. The $delta parameter tells us which block is being
// configured. Here we have added a configuration option asking for the number of item permitted.
else if ($op == 'configure') {
if ($delta == 0)
{
$form['items'] = array(
'#type' => 'select',
'#title' => t('Number of items'),
'#default_value' => variable_get('mymodule_block_items', 0),
'#options' => array('1', '2', '3'),
);
return $form;
}

elseif ($delta == 1)
{
// $form['items'] = array(
// '#type' => 'select',
// '#title' => t('Number of items'),
// '#default_value' => variable_get('mymodule_block_items', 0),
// '#options' => array('1', '2', '3','4', '5', '6'),
// );
return $form;
}
}

else if ($op == 'save') {
if ($delta == 0)
variable_set('mymodule_block_items', $edit['items']);
elseif ($delta == 1)
variable_set('mymodule_block_items', $edit['items']);
}

else if ($op == 'view') {
return $block;
break;
}
}

/************************************
* Implementation of hook_help().
*/
function msc_admin_01_help($path, $arg) {
switch ($path) {
case 'my_first_module_v1':
return '<p>'. t('<strong>If you have any questions about our organization, please contact us by using this form.</strong>') .'</p>';
}

}

/************************************
* Implementation of hook_menu()
* Note: The hook_menu appears to only run (or maybe applied only once)
* when you enable an module. If any changes are made in this
* function after enabling a menu, they will not be applied.
*/
function msc_admin_01_menu(){
$items = array();

$items['my_first_module_v1'] = array(
'title' => t('3rd module V1'), // this shows up in menu
'menu_name' => 'menu-code-test-menu', // name of menu to add this module
'page callback' => 'msc_admin_01_form',
'access arguments' => array('access content'),
);

return $items;
}

/************************************
* note that the function is the name of the module, but the form name can have any name
*/

function msc_admin_01_form() {
return drupal_get_form('my_form_name_3');
}

/************************************
* Here we define elements in a form in an array called $array
* Note that the name of form is "my_form_name_3"
*/
function my_form_name_3($form_state) {

// add external css style shhet
drupal_add_css(drupal_get_path('module', 'my_first_module_v1') .'/my_first_module_v1.css');

$type = node_get_types('type', $node);

// Short question. (Title)
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#default_value' => $node->title,
'#required' => TRUE,
'#weight' => 1,
'#description' => t('What is your name'),
);

// Detailed question (textarea)
$form['detailed_question'] = array(
'#type' => 'textarea',
'#title' => t('What is your question'),
'#default_value' => $node->detailed_question,
'#weight' => 2,
'#rows' => 3,
'#description' => t('What is your question today?'),
);

$form['email_address'] = array(
'#type' => 'textfield',
'#title' => t('Your email address'),
'#default_value' => $object['email_address'],
'#weight' => 4,
'#size' => 60,
'#maxlength' => 128,
//'#validate' => array('is_valid_email_3' => array('email_address')),
'#description' => t('Enter email address if you want a reply to your question or a copy of your question.'),
);

// Hide the default language (neutral/eng/french) tag
$form['language'] = array(
'#type' => 'hidden',
);

// hide the preview button
unset($form['buttons']['preview']);

// redirect to front page after submit
if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
$form['buttons']['submit']['#submit'][] = '';
}

// Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms.

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('submit'),
'#weight' => 7,
);

return $form;
}

/************************************
* Form Validation:
* implements hook_validate
* Verify a node editing form.
* This is a hook used by node modules. It is called to allow the module to verify that the
* node is in a format valid to post to the site. Errors should be set with form_set_error().
*
* Note that this hook uses form name not module name (eg: my_form_name_3_validate)
*/

function my_form_name_3_validate($node, &$form) {

// some validation goes here

}

/************************************
* Implementation of hook_submit() to send user to a new page is form submission is successful
* This is a hook used by node modules. It is called after validation has succeeded and before
* insert/update. It is used to for actions which must happen only if the node is to be saved.
* Usually, $node is changed in some way and then the actual saving of that change is left for
* the insert/update hooks.
*/

function my_form_name_3_submit($form, &$form_state) {
drupal_set_message(t('<br>Thank you for your question<br>'));
echo $form_id;

}

/**
* Updates the faq node question text in the 'faq_questions' table.
*
* @param $node
* The node object.
*/
function msc_admin_01_update($node) {

if ($node->revision) {
faq_insert($node);
}
else
{
// some updating goes here
}
}

/**
*
* @param $node
* The node object.
**/
function msc_admin_01_insert($node) {

// some inserting goes here

}

/************************************
* Function to check for valid email address
*/
function validate_email_3($email) {
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
return TRUE;
}
else{
return FALSE;
}
}

1 个答案:

答案 0 :(得分:2)

嘿那里。要将用户发送到新页面,只需在处理表单后使用drupal_goto('path / to / page'),您可以将用户推送到您创建的menu_callback样式页面,或者您创建的节点,任何内容都可以。

关于标签。请参阅Drupal API中的hook_menu

我们正在寻找: - MENU_LOCAL_TASK:默认情况下,本地任务呈现为制表符。 - MENU_DEFAULT_LOCAL_TASK:每组本地任务都应提供一个“默认”任务,该任务在单击时链接到与其父项相同的路径。

所以它会像

'type' => MENU_DEFAULT_LOCAL_TASK,

其他菜单项将标记为MENU_LOCAL_TASK,请确保它们共享部分路径/部分/路径。 / section / path2等等。