How To Test If a User Is Logged In For Drupal
The correct way of testing if a user is logged in is to test whether $user->uid is 0:
global $user;
if ($user->uid) {
$output = t('User is logged in!');
else {
$output = t('User is an anonymous user.');
}
This approach is often used when defining a block that shows one thing to
logged-in users and something else to anonymous users:
<?php
global $user;
if ($user->uid) {
return t('You are currently logged in!');
}
else {
return t('You are not currently logged in.');
}
?>
- Add new comment
- 362 reads
How To Connect Multiple Databases in Drupal
Sometimes we need to connect to third-party or legacy databases,and it would be great to use Drupal’s database API for doing this.
In the settings.php file, $db_url can be either a string (as it usually is) or an array composed of multiple database connection strings. Here’s the default syntax, specifying a single connection string:
$db_url = 'mysql://username:password@localhost/databasename';
But while using an array, the key is a shortcut name you will refer to while activating the data-base connection, and the value is the connection string itself.
- Add new comment
- Read more
- 536 reads
How To Enable a Block During Module Installation In Drupal
Many often, you may require a block to show up automatically when a module is installed. This is fairly straightforward, and is achieved through a query that inserts the block settings directly into the blocks table. The query goes within hook_install(), located in your module’s .install file.
Here is an example of the user module enabling the user login block when Drupal is being
installed (see modules/system/system.install):
db_query("INSERT INTO {blocks} (module, delta, theme, status) VALUES
('user', 0, '%s', 1)", variable_get('theme_default', 'garland'));
- Add new comment
- Read more
- 480 reads
How To Define New Block Regions in Drupal
In Drupal regions are just areas in themes where blocks need be placed. You can assign blocks to regions and organize them within the Drupal administrative interface at Administer ➤ Site building ➤ Blocks.
The default regions used in themes are left sidebar, right sidebar, header, and footer,
although you can create as many regions as you want. Once declared, they’re made available to your page template files (for example, page.tpl.php) as a variable.
- 2 comments
- Read more
- 1205 reads













