Block Visibility in Drupal
Inside the block admini interface, one can enter PHP code snippets in “Page visibility settings” section of the block configuration page.
While a page being generated, Drupal will run the PHP snippet to determine whether a block will be displayed.
Examples of some of the most common snippets follow; each snippet should return TRUE or FALSE to indicate whether the block should be visible for that particular request.
Displaying a Block to Logged-In Users Only
Only return TRUE when $user->id is not 0.
<?php global $user; return (bool) $user->uid; ?>
- Kiran's blog
- Add new comment
- Read more
- 230 reads
Drupal Hooks
Hooks allow modules to interact with the Drupal core.
Module system of Drupal is based on the concept of "hooks". A hook is actually a PHP
function that is named modulename_hookname(), where "modulename" is the name of the module (whose
filename will be modulename.module) and "hookname" is the name of the hook. Every hook have
a defined set of parameters and a specified result type.
- Kiran's blog
- Add new comment
- Read more
- 1708 reads
To check if the user is an admin in drupal
It is always possible to check if the user is logged in as admin using the drupal built in user_access() function, super administrator account doesn't have a role by default so you can check it using :
<?php
if(user_access('administer'))
{....your code goes here....}
?>
However, in case if you want to check whether the logged-in user has a certain role. For example, something like this if you have a role called 'moderator'
<?php
global $user;
if(is_array($user->roles) && in_array('moderator', $user->roles))
{... your code goes here ...}
?>
- Kiran's blog
- 1 comment
- 1869 reads
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.');
}
?>
- Techdipu's blog
- Add new comment
- 393 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.
- Techdipu's blog
- Add new comment
- Read more
- 590 reads













