Wednesday 23 March 2011

Drupal 6 Template

Example from wysiwyg module

/**
* Implementation of hook_theme().
*
* @see drupal_common_theme(), common.inc
* @see template_preprocess_page(), theme.inc
*/
function wysiwyg_theme() {
return array(
'wysiwyg_profile_overview' => array(
'arguments' => array('form' => NULL),
),
'wysiwyg_admin_button_table' => array(
'arguments' => array('form' => NULL),
),
'wysiwyg_dialog_page' => array(
'arguments' => array('content' => NULL, 'show_messages' => TRUE),
'file' => 'wysiwyg.dialog.inc',
'template' => 'wysiwyg-dialog-page',
),
);
}

in wysiwyg.dialog.inc

echo theme('wysiwyg_dialog_page', $callback($instance));


/**
* Template preprocess function for theme_wysiwyg_dialog_page().
*
* @see wysiwyg_dialog()
* @see wysiwyg-dialog-page.tpl.php
* @see template_preprocess()
*/
function template_preprocess_wysiwyg_dialog_page(&$variables) {
// Construct page title
$head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'Drupal'));
//code process here
}

in file wysiwyg-dialog-page.tpl.php
you can ouput any variable processed in template function
example


This make you able to customize layout for any page, menu on your site
cheer

Friday 11 March 2011

How to extend a form in Drupal

It's so easy to extend a exists form in drupal,

we just use hook_form_alter to do that

function example_form_alter(&$form, &$form_state, $form_id) {

if ( $form_id == 'user_register_form') {

$form['js_example_form']['fullname'] = array(

'#type' => 'textfield',

'#title' => t('Full Name'),

'#element_validate' => array('example_fullname_validate'),

);

$form['#submit'][] = 'example_fullname_submit';
//$form['#validate'][] = 'example_fullname_validate';

}

}

Create a method for validate new form element value

function example_fullname_validate($form, &$form_state) {
$fullname = $form_state['values']['fullname'];
if (empty($fullname)) {
form_set_error('fullname', 'Fullname can not be empty.');
}
}

Save value of fullname

function example_fullname_submit($form, &$form_state) {
$fullname = $form_state['values']['fullname'];
//save fullname here
}


http://www.chromaticsites.com/blog/drupal-tutorial-form-overrides-and-element-specific-validations/

How to create your own hook in drupal

Today I will show you how you can create your own module hook in drupal 7 that is so easy to do
First you must create a api file for the module which you wanna create a hook

YOUR_MODULE_NAME.api.php

create hook method in this file

function hook_YOUR_MODULE_NAME_item_update($item) {

//nothing defined here

}

then in your module file create a method which update data for item

YOUR_MODULE_NAME.module

function YOUR_MODULE_NAME_item_update() {

//your code to update

module_invoke_all('MODULE_NAME_item_update', $item);

}

in orther module you can create function to implement this method

NEW_MODULE.module

function NEW_MODULE_item_update($item) {

//do something with item

}