Archive for the ‘Drupal’ Category

Get Alias Path In Drupal

I’ve added this to the module (also to template.php)  and its working well

function get_url_alias( $src ){
if ( $query = db_query( "SELECT * FROM {url_alias}
WHERE src='%s'", $src ) ){
if ( $rs = db_fetch_object( $query ) ){
return $rs->dst;
}
else {
return $src;
}
}
else {
return $src;
}
}

important variables to be added to page.tpl.php

importent variables to be added to page.tpl.php

$base_path: The base path of the Drupal installation. At the very least, this will always
default to / if Drupal is installed in a root directory.

$breadcrumb: Returns the HTML for displaying the navigational breadcrumbs on the
page.

$closure: Returns the output of hook_footer() and thus is usually displayed at the bottom
of the page, just before the close of the body tag. hook_footer() is used to allow
modules to insert HTML or JavaScript at the end of a page. Note that drupal_add_js()
will not work in hook_footer().

$content: Returns the HTML content to be displayed. Examples include a node, an
aggregation of nodes, the content of the administrative interface, and so on.

$css: Returns an array structure of all the CSS files to be added to the page. Use $styles
if you are looking for the HTML version of the $css array.

$directory: The relative path to the directory the theme is located in; for example,
themes/bluemarine or sites/all/themes/custom/greyscale. You’ll commonly use this
variable in conjunction with the $base_path variable to build the absolute path to your
site’s theme:
<?php print $base_path . $directory ?>
will resolve to
<?php print ‘/’ . ’sites/all/themes/custom/greyscale’ ?>

$feed_icons: Returns RSS feed links for the page. RSS feed links are added via
drupal_add_feed().

$footer: Returns the HTML for the footer region, including the HTML for blocks
belonging to this region. Do not confuse this with hook_footer(), which is a Drupal
hook that lets modules add HTML or JavaScript that will appear in the $closure variable
just before the closing body tag

$footer_message: Returns the text of the footer message that was entered at Administer
ä Site configuration ä Site information.

$front_page: The output of url() with no parameters; for example, /drupal/.
Use $front_page instead of $base_path when linking to the front page of a site, because
$front_page will include the language domain and prefix when applicable

$head: Returns the HTML to be placed within the <head></head> section. Modules
append to $head by calling drupal_set_html_head() to add additional markup.

$head_title: The text to be displayed in the page title, between the HTML
<title></title> tags. It is retrieved using drupal_get_title().

$header: Returns the HTML for the header region, including the HTML for blocks
belonging to this region.

$is_front: TRUE if the front page is currently being displayed

$left: Returns the HTML for the left sidebar, including the HTML for blocks belonging
to this region.

$logged_in: TRUE if the current user is logged in; FALSE otherwise.

$logo: The path to the logo image, as defined in the theme configuration page of
enabled themes. It’s used as follows in Drupal’s default page template:
<img src=”<?php print $logo; ?>” alt=”<?php print t(‘Home’); ?>” />

$messages: This variable returns the HTML for validation errors, success notices for
forms, and other messages as well. It’s usually displayed at the top of the page.

$mission: Returns the text of the site mission that was entered at Administer ä Site configuration
ä Site information. This variable is only populated when $is_front is TRUE.

$primary_links: An array containing the primary links as they have been defined at
Administer ä Site building äMenus. Usually $primary_links is styled through the
theme(‘links’) function as follows:
<?php
print theme(‘links’, $primary_links, array(‘class’ => å
‘links primary-links’))
?>

$secondary_links: An array containing the secondary links as they have been defined at
Administer ä Site building äMenus. Usually $secondary_links is styled through the
theme(‘links’) function as follows: and are placed beneath primary links as they are the sub links for them
<?php
print theme(‘links’, $secondary_links, array(‘class’ => å
‘links primary-links’))
?>

$site_name: The name of the site, which is set at Administer ä Site configuration ä Site
information. $site_name is empty when the administrator has disabled the display on
the theme configuration page of enabled themes.

$site_slogan: The slogan of the site, which is set at Administer ä Site configuration ä
Site information. $site_slogan is empty when the administrator has disabled the display
of the slogan on the theme configuration page of enabled themes.

$styles: Returns the HTML for linking to the necessary CSS files to the page. CSS files
are added to the $styles variable through drupal_add_css().

$tabs: Returns the HTML for displaying tabs such as the View/Edit tabs for nodes. Tabs
are usually at the top of the page in Drupal’s core themes

$title: The main content title, different from $head_title. When on a single node view
page $title is the title of the node. When viewing Drupal’s administration pages,
$title is usually set by the menu item that corresponds to the page being viewed

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.

How to add paging in Drupal

this article is taken from http://www.sononix.com/node/270 just placed here for reminder purposes

//This is numbers per page
$num_per_page = 5;

//actual query

$query = “SELECT n.nid, n.created FROM {node} n WHERE n.type = ‘halloffame’ AND n.status = 1 ORDER BY n.created DESC”;

//the count query should be similar to the query above
$count_query = “SELECT COUNT(*) AS row_count FROM {node} n WHERE n.type = ‘halloffame’ AND n.status = 1 ORDER BY n.created DESC”;

//pager_query function
$result = pager_query($query, $num_per_page, 0, $count_query, $user_load->uid);

//dont forget

while ($node = db_fetch_object($result)) {
$output .= node_view(node_load(array(‘nid’ => $node->nid)), 1);
}

$output .= theme(‘pager’, NULL, 5, 0);

print $output;

?>

Return top

INFORMATION

Change this sentence and title from admin Theme option page.