2010-12-04 6 views
0

Je me fraye un chemin à travers mon premier module Drupal et j'ai quelques questions à poser. Le code ci-dessous est fonctionnel mais certainement un travail en cours.premier module Drupal - quelques questions

  • Je voudrais mettre une page de réponse après que l'utilisateur a rempli le formulaire, pas seulement un « drupal_set_message » ou une autre forme, mais une page entière peut-être montrant réponse de requête SQL

  • Je voudrais Aime également ajouter une bande d'onglets en haut de la page, de sorte que l'utilisateur peut choisir entre plusieurs options similaires.

Merci pour votre aide ou conseil.

<?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; 
} 
} 

Répondre

2

Hé là. Pour envoyer l'utilisateur à une nouvelle page, utilisez simplement drupal_goto ('chemin/vers/page') après avoir traité votre formulaire, vous pouvez pousser l'utilisateur vers une page de style menu_callback que vous créez, ou un noeud que vous créez.

En ce qui concerne les onglets. Veuillez vous référer au hook_menu dans l'API Drupal.

Nous recherchons ceci: - MENU_LOCAL_TASK: Les tâches locales sont affichées en tant qu'onglets par défaut. - MENU_DEFAULT_LOCAL_TASK: Chaque ensemble de tâches locales doit fournir une tâche "par défaut", qui lie au même chemin que son parent lorsqu'il est cliqué.

il serait donc quelque chose comme

'type' => MENU_DEFAULT_LOCAL_TASK,

Les autres éléments du menu seraient marqués MENU_LOCAL_TASK, assurez-vous qu'ils partagent un chemin section/section/chemin./section/path2 et ainsi de suite.

+0

merci pour le pointeur. Malheureusement web est un peu court sur des exemples d'utilisation de l'API Drupal. –

+0

bien laissez-moi savoir si vous avez besoin d'aide! :) –

+0

que diriez-vous de petit fichier de module avec un exemple fonctionnel? Merci! – robert