There are two ways to create a block in Drupal:
- Using a custom module
- Using Drupal GUI
Create a block using a custom module
We use Drupal hooks to create a block from custom module. In Drupal 6, hook_block() was used. This function has been redefined in Drupal 7 as the following 8 functions:
- hook_block_configure: Define a configuration form for a block.
- hook_block_info: Define all blocks provided by the module.
- hook_block_info_alter: Change block definition before saving to the database.
- hook_block_list_alter: Act on blocks prior to rendering.
- hook_block_save: Save the configuration options from hook_block_configure().
- hook_block_view: Return a rendered or renderable view of a block.
- hook_block_view_alter: Perform alterations to the content of a block.
- hook_block_view_MODULE_DELTA_alter: Perform alterations to a specific block.
Here we will develop a basic block (assuming that you already know how to create a custom module in Drupal 7) and hence will use only "hook_block_info()" and "hook_block_view()".
/** * Implements hook_block_info(). */ function mymodule_block_info() { $blocks['mymodule-blockname'] = array( 'info' => t('Administrative block title'), 'cache' => DRUPAL_NO_CACHE, ); return $blocks; } /** * Implements hook_block_view(). */ function mymodule_block_view($delta = '') { $block = array(); switch ($delta) { case 'mymodule-blockname': $block['subject'] = t('Block Title'); $block['content'] = t('Hello World!'); break; } return $block; }
Create a block using Drupal GUI
- Log in to the website.
- Navigate to "Structure" -> "Blocks" -> "Add Block"
- Fill the empty fields and the necessary information:
- Block Title: This is the title shown to the user. If you do not want to show any Title, write <none>
- Block Description: This is a brief description of the Block visible on block listing page.
- Block Body: Write the content of the block here.
- Region Settings: Blocks are placed into regions. Place your block on a region such as Header, Content, Footer, etc. Note: You can navigate to Structure > Blocks and click on “Demonstrate block regions” to view the available regions as per your theme. The regions are theme specific. You can place the block in any of the enabled theme.
- Visibility Settings: You can tell Drupal on which page the block will be visible. The visibility settings can be set for Pages, Content Types, Roles and Users.
Per Page Block Visibility Settings
All pages except those listed/Only the listed pages
In both cases, you can specify the pages by their paths (as shown above). The * is a wildcard character. For eg: as per the above setting, the block will be visible for the path http://www.sitename.com/content and http://www.sitename.com/content/xyz. For frontpage, <front> can be used.
Pages on which this PHP code returns TRUE
Here you can write PHP code between <?php ?>.
if ($condition) { // Condition is true. return TRUE to show the block. return TRUE; } else { // Condition is false. Return FALSE to hide the block. return FALSE; }
Note: "Pages Visibility Settings" and "Content Types Visibility Settings" do not work together.