开发者

first Drupal module - some questions

I am grokking my way through my first Drupal module and have a few questions. Code below is functional but definately a work in progress.

  • I would like to put out a response page after user has submitted form, not just a "drupal_set_message" or another form, but a whole page perhaps showing response from SQL query

  • I would also like to add a tab strip along the top of the page, so user can select between several similar options.

Thanks for your help or advice.

<?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 'f开发者_JS百科aq_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;
}
}


Hey there. To send the user to a new page just use drupal_goto('path/to/page') after you process your form, you can push the user to a menu_callback style page that you make, or a node that you create, anything goes.

In regards to the tabs. Please refer to hook_menu in the Drupal API.

We're looking for this: - MENU_LOCAL_TASK: Local tasks are rendered as tabs by default. - MENU_DEFAULT_LOCAL_TASK: Every set of local tasks should provide one "default" task, that links to the same path as its parent when clicked.

so it would be something like

'type' => MENU_DEFAULT_LOCAL_TASK,

The other menu items would be marked MENU_LOCAL_TASK, make sure they share a section path /section/path. /section/path2 and so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜