GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Showing posts with label Drupal. Show all posts
Showing posts with label Drupal. Show all posts

Tuesday, March 29, 2011

Drupal - Skills In Drupal

There are some fundamental skills in drupal:
1. Theming
2. Module Creation:
  a. Menu
  b. Form
  c. Ajax
  d. Database
Share/Bookmark

Thursday, March 24, 2011

Drupal - Manu

It's so simple to make a menu in Drupal, just like this below:
<?php

function mac_menu(){
    $item = array();
    $item['mac/main'] = array(
        'title' => 'Mac Menu',
        'page callback' => 'main',
        'access callback' => TRUE,
    );
    return $item;
}

function main(){
    return t('Hello. This is mac main menu');
}
Share/Bookmark

Friday, March 11, 2011

Drupal - Expose Page Variables

To expose all available of the page variables, type:
   var_dump($page);
in the page.tpl.php
for example you can display the page content by typing this below in page.tpl.php
<?php print $page['content']['system_main']['nodes'][3]['body']['#object']
            ->body['und'][0]['value'];?>
Share/Bookmark

Drupal - Render Variables

To render a variable from template file to specific tpl tile, you need to declare the variable first in template file
<?php

function ipod_process_page(&$variables){
    $variables['content'] = array(
        '#markup' => '<div id="content">Content Here</div>';
        '#weight' => 100,
    );
}

Then on page.tpl.php, do
<div id="page">
    <?php print render($content);?>
</div>
Share/Bookmark

Drupal - Theming

If you want a passing variable value from template file to specific template file, do like this:
For example, your theme name is ipod
template.php
<?php
function ipod_process_page(&$variables){
     $variables['name'] = 'Lady Gaga';
}

So in the page.tpl.php, you would print
<div id="page">
    <?php print $name ? $name : 'No name';?>
</div>
Share/Bookmark

Monday, March 7, 2011

Drupal Module - Creating Table

This file is used as a default table creation when you create a module. May be sometime your module need a sole of table, and this install file will act as the table creator.

Name the file: phone.install

<?php

function phone_install(){
   
    db_insert('dataa')
        ->fields(array(
            'nama' => 'luna maya',
            'age' => 27))
        ->execute();       
}


function phone_schema(){

  $schema['dataa'] = array(
    'description' => 'Data for storing the data',
    'fields' => array(
        'id' => array(
            'type' => 'serial',
            'not null' => TRUE,
            'description' => 'Primary ID',
        ),
        'nama' => array(
            'type' => 'char',
            'length' => 50,
            'not null' => TRUE,
            'default' => 'no name',
        ),
        'age' => array(
            'type' => 'int',
            'not null' => TRUE,
            'default' => 0,
        ),
    ),
    'primary key' => array('id'),
    'indexes' => array(
        'nama' => array('nama'),
        'age' => array('age'),
    ),
 
  );

   
    return $schema;
}

Share/Bookmark

Thursday, February 24, 2011

Drupal - Database Handling

SELECT * FROM data
drupal>>db_select('data','d')
         ->fields('d')
         ->execute()
         ->fetchAll()

SELECT * FROM data WHERE name = 'Lady Gaga'
drupal>> db_select('data','d')
         ->fields('d')
         ->condition('name','Lady Gaga')
         ->execute()
         ->fetchAll()

SELECT * FROM data WHERE age >= 30
drupal>> $query = db_select('data','d')->fields('d')->condition('age',30,'>=')->execute()->fetchAll()

SELECT * FROM data WHERE age >=30 OR name = 'Lady Gaga'
drupal>> db_select('data','d')
         ->fields('d')
         ->condition(db_or()
                     ->condition('age',30,'>=')
                     ->condition('name','Lady Gaga'))
         ->execute()
         ->fetchAll();

SELECT * FROM data WHERE age>=30 AND id>5
drupal>> db_select('data','d')->fields('d')->condition(db_and()->
          
Share/Bookmark

Drupal - Show Result Of Tables

<?php

function ipod_menu(){
    $items = array();
   
    $items['ipod'] = array(
        'title' => 'iPod',
        'page callback' => 'show_data',
        'access callback' => TRUE,
    );
   
    return $items;
}

function show_data(){
    $query = db_select('data','d')->fields('d')->execute()->fetchAll();
    $output = '<table><th>No</th><th>Name</th><th>Age</th><th>City</th></tr>';
    $i = 1;
    foreach($query as $key){
        $output .= '<tr>';
        $output .= '<td>'.$i++.'</td>';
        $output .= '<td>'.$key->name.'</td>';
        $output .= '<td>'.$key->age.'</td>';
        $output .= '<td>'.$key->city.'</td>';
        $output .= '</tr>';
    }
    $output .= '</table>';
   
    return $output;
}

Share/Bookmark

Wednesday, February 23, 2011

Drupal - Theming The Table

To render a table in Drupal, use
function get_table(){
    $header = array(t('Name'),t('City'),t('Country'));
    $rows = array(
        array(t('Bill Gates'), t('Redmond'), t('Americas')),
        array(t('Luna Maya'), t('Denpasar'), t('Indonesia')),
        array(t('Aura Kasih'), t('Jakarta'), t('Indonesia')),
    );
    $output = theme('table',array('header' => $header,
                                  'rows'   => $rows));
    return $output;
}
Share/Bookmark

Tuesday, February 22, 2011

Drupal - Theming The List

Below is the code on how to use theme for list

<?php

/**
 * Implements hook_menu()
 */

function luna_menu(){

    $items = array();
   
    $items['luna/main'] = array(
        'title' => 'Main Menu',
        'page callback' => 'intro',
        'access callback' => TRUE,
        'expanded' => TRUE,
    );
   
    $items['luna/main/list'] = array(
        'title' => 'List',
        'page callback' => 'get_list',
        'access callback' => TRUE,
    );
   
    return $items;

}

function intro(){
    return t('This menu demos to you on how to handle with the theme hook menu');
}

function get_list(){

    $list = array();

    $list[] = t('Passing The University');
    $list[] = t('Getting The Job');
    $list[] = t('Moving To Americas');
    $list[] = t('Building The Business');
   
    $render_array['get_list'] = array(
        '#theme' => 'item_list',       // calling item list
        '#title' => t('Title Demos'),
        '#items' => $list,
        '#type' => 'ol',               // or "ul"
    );
   
    return $render_array;
}


Share/Bookmark

Drupal Page Callback With Arguments

If you want to get the value of  the arguments, please look like this code

 <?php

/**
 * Implements hook_menu()
 */
 
function luna_menu(){
    $items = array();
   
    $items['luna/main'] = array(
        'title' => 'Main Menu',
        'page callback' => 'description',
        'access callback' => TRUE,
    );
   
    $items['luna/po/%'] = array(
        'title' => 'Hello For',
        'page callback' => 'four',
        'page arguments' => array(2),
        'type' => MENU_CALLBACK,
        'access arguments' => array('access arguments page'),
    );
   
    return $items;

}

function description(){
    return array('#markup' => t('The link for this one: '
         .'<a href="@link">About Us</a>',
          array('@link'=>url('luna/po/5'),
          array('absolute'=>TRUE))       ));
}

function four($first){
    return t('You get the info with this number: @number', 
           array('@number'=>$first+10));
}
Share/Bookmark

Drupal Page Callback

If you want a link to direct for a page, do it with that menu callback hook function. You access the page without no menu navigation link.

<?php

/**
 * Implements hook_menu()
 */
 
function luna_menu(){

    $items = array();
   
    $items['luna/main'] = array(
        'title' => 'Main Menu',
        'page callback' => 'description',
        'access callback' => TRUE,
    );
   
    $items['luna/po/%'] = array(       
        'page callback' => 'four',
        'page arguments' => array(2), // start from 0/1/2/3/etc
        'type' => MENU_CALLBACK,      // no link in nav menu
        'access arguments' => array('access arguments page'),
    );
   
    return $items;
}

function description(){
    return 'Get the other for this one: <a href="po/4">About Us</a>';
}

function four(){
    return 'Hello. We are currently active in the what we do for the next year. We build a custom enterprise software for oil company.';
}
Share/Bookmark

Sunday, February 20, 2011

Drupal Database Access

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => lady gaga
            [age] => 25
            [city] => new york
        )

    [1] => stdClass Object
        (
            [id] => 2
            [name] => luna maya
            [age] => 27
            [city] => denpasar
        )

)
The above is the result of this function
function get_data(){
 $output = '';
 $select = db_select('data_siswa','c')->fields('c')->execute()->fetchAll(); 
 return print_r($select); 
}
 
To get the data as a table, use this one:
function introo(){
 $output = '<table>';
 $output .= '<tr><th>Name</th><th>Age</th><th>City</th></tr>';
 
 $select = db_select('data_siswa','c')->fields('c')
                                    ->execute()->fetchAll();
      foreach($select as $key){
  $output .= '<tr><td>'.$key->name ."</td><td>";
                $output .=  $key->age. "</td><td>". $key->city ."</td></tr>";
 }
 
 $output .= "</table>";
 
 return $output; 
} 

Share/Bookmark

Friday, February 18, 2011

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') );
}
Share/Bookmark

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
Share/Bookmark

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

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
Share/Bookmark

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');
}

Share/Bookmark

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;
}
Share/Bookmark

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;
}
Share/Bookmark

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

Share/Bookmark