Archive for April, 2010

base path and module path

to get theme path use

drupal_get_path('theme', 'theme_name')

for module path use

drupal_get_path('mobule', 'module_name')

to get base url

$GLOBALS['base_path']

Creating Drupal Module – Part 2

now I will cover the mymodule.module part

there is three hooks we use alot in backend those are

insert_hook, update_hook, delete_hook

each hook add node, delete node, and update node

insert_hook

starting clear we can implement hook insert by name modulename_insert

function modulename_insert($node) {
  db_query("INSERT INTO {mytable} (nid, extra)
    VALUES (%d, '%s')", $node->nid, $node->extra);
}


function modulename_update($node) {
//if the node is set for revision insert instead of delete
if ($node->revision) {
 modulename_insert($node);
 }
 else
{
 db_query("UPDATE {mytable} SET extra = '%s' WHERE nid = %d",
    $node->extra, $node->nid);
}
}
function modulename_delete(&$node) {
  db_query('DELETE FROM {mytable} WHERE nid = %d', $node->nid);
}

Creating Drupal Module Part 1 – Install Package

first of all we should know that the name of installing any function should be modulename.install and we wont need

<?php
function mymodule_schema() {
$schema['mytable1'] = array(
// specification for mytable1
);
$schema['mytable2'] = array(
// specification for mytable2
);
return $schema;
}

function mymodule_install() {
// Create my tables.
drupal_install_schema('mymodule');
drupal_set_message(t('The tables created successfully'));
}

function mymodule_uninstall() {
// Drop my tables.

drupal_uninstall_schema('mymodule');
drupal_set_message(t('The tables were removed'));

}
?>

sample of table schema array

$schema['distribution_center'] = array(
‘fields’ => array(
‘vid’ => array(
‘type’ => ‘int’,
‘not null’ => TRUE,
‘description’=> “Log ID”
),
‘nid’=>array(
‘type’=>’int’,
‘not null’=>FALSE,
),
‘fname’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘lname’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘address’=>    array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘country’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘city’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘phone’=>array(
‘type’=>’text’,
‘not null’=>FALSE,
),
‘mobile’=>array(
‘type’=>’text’,
‘not null’=>FALSE,
),
‘email’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
’site’=>array(
‘type’=>’text’,
‘not null’=>TRUE,
),
‘center_attr’=>    array(
‘type’=>’text’,
‘not null’=>FALSE,
),
),
‘primary key’ => array(‘vid’)
);

Logging in Drupal

$msg = ‘No content from %url.’;
$vars = array( ‘%url’ => $url );
watchdog(‘module_name’, $msg, $vars, WATCHDOG_WARNING);

There are eight different constants that can be passed to this function:
WATCHDOG_EMERG: The system is now in an unusable state.
WATCHDOG_ALERT: Something must be done immediately.
WATCHDOG_CRITICAL: The application is in a critical state.
WATCHDOG_ERROR: An error occurred.
WATCHDOG_WARNING: Something unexpected (and negative) happened, but
didn’t cause any serious problems.
WATCHDOG_NOTICE: Something significant (but not bad) happened.
WATCHDOG_INFO: Information can be logged.
WATCHDOG_DEBUG: Debugging information can be logged.

Return top

INFORMATION

Change this sentence and title from admin Theme option page.