Friday, 18 February 2011

Learn To Program on drupal help

Message In Drupal

It's easy to make message in drupal. If your form function is data_form($form, &$form_state), so your submit function is data_form_submit($form, &$form_state). What's the point? The point is that you add "_submit" on the for function for your submit function. Look this  code

// form function
function data_form( $form, &$form_state){
    // implement codes
}

// form submit function
function data_form_submit( $form, &$form_state){
    drupal_set_message( t('Your data has been saved successfully') );
}

Thursday, February 17, 2011

Drupal Form

When you code for drupal form, it's rather feel complicated. But it's simple enough to build what you want.
The procedures are:
1. Build your hook_menu to call the page that contain the form. In the hook_menu implementation, you need supply at least
a. title
b. page callback - for form, this is default to drupal_get_form
c. page argument - the function name you write inside array. This function implements drupal form
d. access callback - set to TRUE so averyone can access it

2. Build your form function
a. Insert in the arguments of the function definition: $form and &$form_state
b. define what form elements you provide

The list of the form elements:
a. submit: '#type' => 'submit', '#value' => 'Save'
b. item - provide small description '#type' => 'item', '#title' => t('The description')
c. textfield: '#type' => 'textfield', '#title' => t('The title'),
For more info, see code repositories

Drupal Menu

The minimum requirements in implementing Drupal hook_menu is
1. title
2. page callback. If there's no default menu tabs or menu default has no page callback, this is used one
3. access callback d

Then if for the tab menu is this one
0. there is at leat two tab menus
1. type: MENU_DEFAULT_LOCAL_TASK or MENU_LOCAL_TASK
2. title
3. page callback
4. access callback
5. weight

Tuesday, February 15, 2011

Hook Menu With Tabs

This is the info file: luna.info
name = Luna Menu
description = Luna Menu Provide Demo For This One
core = 7.x
version = 1.2
package = Lunar

This is the module file: luna.module
<?php
/**
 * Implements hook_menu()
 */
 
function luna_menu(){
    $items = array();
    
    /**
     * The main menu in navigation
     */
    $items['lunar/main'] = array(
        'type' => MENU_NORMAL_ITEM,
        'title' => 'Tabs',
        'page callback' => 'hello',
        'page arguments' => array(t('Holla For You')),
        'access callback' => TRUE,   
    );
    
    /**
     * The first and default displaying tabs
     * Use MENU_DEFAULT_LOCAL_TASK 
     * Add the next ( second) of tabs
     */
    $items['lunar/main/default'] = array(
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'title' => 'Default Tabs',       
        'page callback' => 'holla',
        'weight' => 1,
    );

    /**
     * The second weight menu
     * use MENU_LOCAL_TASK
     * access callback is MUST
     */   
    $items['lunar/main/satu'] = array(
        'type' => MENU_LOCAL_TASK,
        'title' => 'Satu Dimensi',
        'page callback' => 'hey',
        'weight' => 2,
        'access callback' => TRUE,
    );

    $items['lunar/main/dua'] = array(
        'type' => MENU_LOCAL_TASK,
        'title' => 'Dua Dimensi',
        'page callback' => 'indonesia',
        'weight' => 3,
        'access callback' => TRUE,
   
    );
   
    return $items;
}

function hello($msg){
    return $msg;
}

function holla(){
    return t('Default Local Tabs');
}

function hey(){
    return t('Indonesia is a nice one');
}

function indonesia(){
    return t('Malaysia is a nice one for this one');
}

Hierarchical Hook Menu

This is an info file: luna.info
name = Luna Menu
description = Luna Menu Provide Demo For This One
core = 7.x
version = 1.2
package = Lunar

This is the module file: luna.module
<?php
/**
 * Implements hook_menu()
 */
 
function luna_menu(){
    $items = array();
   
    $items['lunar/main'] = array(
        'title' => 'Luna Main',
        'page callback' => 'hello',
        'page arguments' => array('Good Morning'),
        'access callback' => TRUE,
    );
    $items['lunar/main/satu'] = array(
        'title' => 'Luna Satu',
        'page callback' => 'ho',
        'page arguments' => array('Good Evening'),
        'access callback' => TRUE,   
    );
    $items['lunar/indonesia'] = array(
        'title' => 'Lunar Indonesia',
        'page callback' => 'li',
        'page arguments' => array('Holla For Indoensia'),
        'access callback' => TRUE,
    );
    $items['lunar/indonesia/satu'] = array(
        'title' => 'Lunar Satu Indonesia',
        'page callback' => 'lo',
        'page arguments' => array('Holli For Us All'),
        'access callback' => TRUE,
    );

    return $items;
}

function hello($msg){
    return $msg;
}

function ho($msg){
    return $msg;
}

function li($msg){
    $data = '';
    for($i=0;$i<10;$i++){
        $data .= $msg.' ';
    }
    return $data;
}

function lo($msg){
    return $msg;
}

Simple Menu Hooks

This is the info file: luna.info
name = Luna Menu
description = Luna Menu Provide Demo For This One
core = 7.x
version = 1.2
package = Lunar

This is the module file: luna.module
<?php
/**
 * Implements hook_menu()
 */
 
function luna_menu(){
    $items = array();
   
    $items['lunar/main'] = array(
        'title' => 'Luna Main',
        'page callback' => 'hello',
        'page arguments' => array('Good Morning'),
        'access callback' => TRUE,
    );
   
    return $items;
}

function hello($msg){
    return $msg;
}

ComboBox In Drupal Form

<?php

function data_menu(){
    $items = array();

    $items['learning/data'] = array(
        'title' => 'Hello Data',
        'page callback' => 'drupal_get_form',
                'page arguments' => array('hello'),
        'access callback' => TRUE,
        
    );    
    
    return $items;
}

function hello($form, &$form_state){
    $form = array();
    
    $form['hello'] = array(
        '#type' => 'fieldset',
        '#title' => 'Please fill the form below',
    );
    
    $form['hello']['years'] = array(
        '#type' => 'select',
        '#title' => 'Where you do live right now?',
        '#options' => array(
            1 => t('Indonesia'),
            2 => t('Malaysia'),
            3 => t('Filipine'),        
        ),
    );

    $form['hello']['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Login',
    );
    return $form;
}

Radios And Checkbox Of Drupal Form

<?php

function data_menu(){
    $items = array();

    $items['learning/data'] = array(
        'title' => 'Hello Data',
        'page callback' => 'drupal_get_form',
                'page arguments' => array('hello'),
        'access callback' => TRUE,
       
    );   
   
    return $items;
}

function hello($form, &$form_state){
    $form = array();
   
    $form['hello'] = array(
        '#type' => 'fieldset',
        '#title' => 'Please fill the form below',
    );
   
    $form['hello']['gender'] = array(
        '#type' => 'radios',
        '#title' => 'What is your gender?',
        '#options' => drupal_map_assoc(array(t('Male'),
            t('Female'))),
    );

    $form['hello']['fruit'] = array(
        '#type' => 'checkboxes',
        '#title' => 'Fruit You Like?',
        '#options' => drupal_map_assoc( array(t('Watermellon'),
            t('Apple'), t('Orange'), t('Papaya'))),
    );

    $form['hello']['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Login',
    );
    return $form;
}

Drupal Form With Fieldset

<?php

function data_menu(){
    $items = array();

    $items['learning/data'] = array(
        'title' => 'Hello Data',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('hello'),
        'access callback' => TRUE,
       
    );   
   
    return $items;
}

function hello($form, &$form_state){
    $form = array();
   
    $form['hello'] = array(
        '#type' => 'fieldset',
        '#title' => t('Please login to below'),

    );
    $form['hello']['username'] = array(
        '#type' => 'textfield',
        '#title' => t('username'),
        '#size' => 50,
    );
    $form['hello']['password'] = array(
        '#type' => 'password',
        '#title' => t('Password'),
        '#size' => 50,
    );
    $form['hello']['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Login',
    );
    return $form;
}

Simple Drupal Drupal Form

<?php
/**
  * Implements hook_menu()
  */
function data_menu(){
    $items = array();

    $items['learning/data'] = array(
        'title' => 'Hello Data',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('hello'),
        'access callback' => TRUE,
       
    );   
   
    return $items;
}

function hello($form, &$form_state){
    $form = array();
    $form['username'] = array(
        '#type' => 'textfield',
        '#title' => t('Username'),
        '#size' => 30,
    );
    $form['password'] = array(
        '#type' => 'password',
        '#title' => 'Password',
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Send',
    );
    return $form;
}

Drupal Menu With Arguments

<?php

function holla_menu(){
    $items = array();
    $items['learning/one'] = array(
        'title' => 'Holla For',
        'page callback' => 'hello',
        'page arguments' => array('Lady Gaga Of New York'),
        'access callback' => TRUE,
    );
    return $items;
}

function hello($args){
    return $args;
}

Hierarchical Menu In Drupal

<?php
/**
 * Implements hook_menu() 
 */

function data_menu(){
    $items = array();

    $items['learning/data'] = array(
        'title' => 'Hello Data',
        'page callback' => 'hello',
        'access callback' => TRUE,
    );

    $items['learning/data/satu'] = array(
        'title' => 'Data Satu',
        'page callback' => 'hello_satu',
        'access callback' => TRUE,

    );

    $items['learning/data/dua'] = array(
        'title' => 'Data dua',
        'page callback' => 'hello_dua',
        'access callback' => TRUE,

    );

    $items['learning/data/dua/a'] = array(
        'title' => 'Data A',
        'page callback' => 'hello_a',
        'access callback' => TRUE,
    );
   
    return $items;


}

function hello(){
    return t('hello boo. And Hello for you all in the home of Indonesia');

}

function hello_satu(){
    return t('Holla from satu learning package in the home of you all here');
}

function hello_dua(){
    return t('Holla from all here that we can do more for this one');

}

function hello_a(){
    return t('Holla from A to Z');
}

Drupal Menu

This is important case. When you write a drupal hook of menu, plase keep in mind for include your package to the first segment of uri.
package = learning
module = lola
function lola_menu(){
    $items = array();
    $items['learning/lola'] = array(
        'title' => 'Lola Main Menu',
        'page callback' => 'hello',
        'access callback' => TRUE,
    )
    return $items;
}

function hello(){
    // echo 'Hello For You All';
    // if you want the echo of text written as a part of page,
    // use below
    return t('Hello For You All');
}

No comments:

Post a Comment

Source base installation of php, mysql and apache in ubuntu/ linux

Compile and Install a LAMP(Linux/Apache/MySQL/PHP) Server from Source In the last post, I described the method to install a LAMP ser...