Creating a Drupal Subtheme to Override Templates and Blocks

Creating a subtheme in Drupal is an excellent way to customize the appearance and functionality of your website while preserving the integrity of the parent theme. In this tutorial, we'll walk you through the process of creating a subtheme and overriding specific templates and blocks.

Prerequisites:

Step 1: Create a Subtheme (Optional)

Creating a subtheme helps maintain the parent theme's structure and simplifies future updates. If you already have a subtheme or prefer to work with your theme directly, you can skip this step.

  1. In your Drupal installation, navigate to the themes directory, typically located at sites/all/themes.

  2. Create a new directory for your subtheme. For example, let's call it mytheme_sub.

  3. Inside the subtheme directory, create a mytheme_sub.info file. Replace mytheme_sub with your subtheme's name. Define your subtheme in this file:

name = My Theme Sub
base theme = mytheme
description = Subtheme of My Theme
core = 7.x
  1. Save the mytheme_sub.info file.

Step 2: Copy and Override Template Files

  1. Determine which template file you want to override. For example, let's override the page.tpl.php template, which controls the layout of your site's pages.

  2. Locate the template file you want to override in the parent theme's directory (e.g., sites/all/themes/mytheme/).

  3. Copy the template file to your subtheme's directory (e.g., sites/all/themes/mytheme_sub/).

  4. In your subtheme's directory, rename the copied template file to match Drupal's naming conventions. For example, if you're overriding page.tpl.php, name it page--mytheme-sub.tpl.php.

  5. Edit the copied template file in your subtheme's directory to make the desired customizations for your site's layout.

Step 3: Clear Drupal's Cache

After making changes to template files, clear Drupal's cache to apply the updates:

  1. Log in to your Drupal admin panel.

  2. Go to "Configuration" > "Performance."

  3. Click the "Clear all caches" button.

Step 4: Override Blocks (Top 20 Examples)

Here are 20 common blocks you might want to override in Drupal along with their respective template files:

  1. Main Content Block: Override block.tpl.php to customize the display of your main content.

  2. Header Block: Modify block--system.tpl.php to customize the site's header, including the site logo and name.

  3. Footer Block: Customize the footer content by editing block--system.tpl.php.

  4. Navigation Menu Block: Override block--menu.tpl.php to change the appearance and structure of navigation menus.

  5. User Login Block: Customize the user login form's appearance by editing block--user.tpl.php.

  6. User Registration Block: Modify the user registration form's display with block--user.tpl.php.

  7. Search Block: Customize the appearance of the search form by editing block--search-form.tpl.php.

  8. Recent Posts Block: Override block--views.tpl.php to control how recent posts are displayed.

  9. Categories Block: Customize the display of post categories with block--views.tpl.php.

  10. Tags Block: Modify the appearance of post tags using block--views.tpl.php.

  11. Comments Block: Control the display of recent comments with block--views.tpl.php.

  12. Archive Block: Customize the display of the content archive by editing block--views.tpl.php.

  13. Popular Content Block: Override block--views.tpl.php to control how popular content is presented.

  14. Related Content Block: Customize the display of related content using block--views.tpl.php.

  15. Recent Comments Block: Modify how recent comments are displayed by editing block--views.tpl.php.

  16. Blockquotes Block: Control the appearance of blockquotes in content by editing block.tpl.php.

  17. Social Media Icons Block: Override block.tpl.php to add or modify social media sharing icons.

  18. Advertisement Blocks: Customize the display of advertisements on your site by editing relevant block templates.

  19. Custom Content Blocks: Create custom blocks and override their templates for displaying specific content.

  20. Widget Blocks: Override widget blocks added by third-party modules using their respective template files.

To override a block, locate the corresponding block template file in your parent theme's directory, copy it to your subtheme's directory, and make your customizations.

Step 5: Clear the Cache Again

After overriding blocks or making changes to templates, remember to clear the cache again to ensure that Drupal loads the updated templates and blocks.

By following these steps, you can effectively create a Drupal subtheme, override templates, and customize blocks to tailor your website's appearance and functionality to your specific needs.