Menu icon
RedCrackle
Menu icon
Services
01Design Transformation
About UsCase StudiesBlogContact Us

<

Blog post

How To Create A Block In Drupal 7?

Neerav Mehta

Founder & CEO

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:

  1. hook_block_configure: Define a configuration form for a block.
  2. hook_block_info: Define all blocks provided by the module.
  3. hook_block_info_alter: Change block definition before saving to the database.
  4. hook_block_list_alter: Act on blocks prior to rendering.
  5. hook_block_save: Save the configuration options from hook_block_configure().
  6. hook_block_view: Return a rendered or renderable view of a block.
  7. hook_block_view_alter: Perform alterations to the content of a block.
  8. 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

  1. Log in to the website.
  2. Navigate to "Structure" -> "Blocks" -> "Add Block"
  3. 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.

Note: "Pages Visibility Settings" and "Content Types Visibility Settings" do not work together.

Neerav Mehta

Neerav Mehta

Founder & CEO

Neerav Mehta is the Founder & CEO of Red Crackle. With sterling qualities, Neerav’s technological acumen is firing a generation of progressive companies on the digital path. With an undergraduate degree in Electrical Engineering from India's most prestigious institution IIT Bombay and having spent seven years developing and contributing to the launch of AMD's innovative line of computer products, Neerav founded Red Crackle where he is lauded for his dynamic and innovative genius.

View all posts

>

Read Next

10 Tips For Entrepreneurs In 2015

10 Tips For Entrepreneurs In 2015

Learn more

10 Ways To Increase Productivity At Work

10 Ways To Increase Productivity At Work

Learn more

30 best WordPress widgets for your site

30 best WordPress widgets for your site

Learn more

Let’s get you started!

Contact Us

>

RedCrackle

Explore

About Us

Services

Contact Us

Our address

5346 Gerine Blossom Dr,

San Jose, CA 95123

USA

Socials

Twitter
LinkedIn

© 2023 RedCrackle. All rights reserved.