Hands-On Drupal Theming Exercises

This course contains a series of hands-on exercises designed to give you practical experience working with Drupal themes. Use this as an opportunity to practice the skills you’ve developed via other courses on this site.

Tutorials in this course
Categories
Drupal 8, 9, and 10
More information

Set up a local development environment to practice Drupal theme development. In order to practice theme development, either on your own or following our Hands-On: Theming guide, you'll need a Drupal site up and running on your computer.

By the end of this tutorial, you should be able to:

  • Install Drupal on your computer so you can modify files with a code editor of your choice.
  • Generate dummy content so that you have different kinds of pages to theme.
Categories
Drupal 8, 9, and 10
More information

It's time to create the bare-bones structure for a new theme on your site. You should try to complete this exercise based on the information you've learned in this chapter. The video included will walk you through the implementation of this exercise if you need some help. In this exercise you will need to:

  • Create an info file that describes a custom theme to Drupal with the regions listed below (we're going to name ours "reboot").
  • Enable, and view, a bare-bones custom theme.

Regions:

'Header' (header)
'Primary menu' (primary_menu)
'Secondary menu' (secondary_menu)
'Page top' (page_top)
'Page bottom' (page_bottom)
'Highlighted' (highlighted)
'Featured top' (featured_top)
'Breadcrumb' (breadcrumb)
'Content' (content)
'Sidebar first' (sidebar_first)
'Sidebar second' (sidebar_second)
'Footer first' (footer_first)
'Footer second' (footer_second)

Note: At the end of this exercise, you'll find a video walk-through of the solution.

Categories
Drupal 8, 9, and 10
More information

You can accomplish a lot without needing to change any markup by adding CSS and JavaScript files to your theme. In this exercise you'll create 2 asset libraries and add them to your theme. Specifically, we want to pull in the CSS and JavaScript from the popular Bootstrap framework so that we can make use of its layout utility classes later on. We'll also add a custom CSS file that contains global styles for our site, like setting the page background color.

If you want to try and complete this on your own first you'll need to:

  • Add the Bootstrap CSS and JavaScript files to your theme.
  • Define an asset library using a THEMENAME.libraries.yml file in your theme.
  • Tell Drupal to attach your asset library so that the CSS and JavaScript files it represents are included in the page.

Once that's done your site won't look all that different. But if you view the page source, or look closely, you should see that the Bootstrap files are included along with any CSS rules you placed into your custom style sheet.

Note: Since this course if focused on teaching the Drupal aspects of theme development, and not on writing CSS, we use the Bootstrap CSS. If you don't need it for your project though you can skip it, or add the CSS framework that you want to use instead.

You should try to complete the exercise steps on your own and use the video to help guide you if you get stuck.

Note: At the end of this exercise, you'll find a video walk-through of the solution.

More information

Drupal has a few handy settings you can tweak to make developing themes a little more intuitive and a lot more awesome. This includes disabling some of the caching that Drupal does so you don't need to clear the cache as frequently. We'll also turn off CSS and JS aggregation in a settings file (overriding anything set on the Performance configuration form) so you can see your changes to .js and .css files with just a page refresh. Finally, turning on the Twig debug service will give us some additional contextual information about the templates being used when we view the source code of any page on our site. Walk through the exercise steps, and if you get stuck you can watch the video to see how we implemented this.

If you want to try and complete this on your own you'll need to:

  • Make changes to your site's settings.php file. Hint: check out the example.settings.local.php file included with core.
  • Modify configuration via a services.yml file. Hint: check out the default.services.yml file included with core.

Once this is done, if you view the source of any page on your Drupal site you should see extra HTML comments with helpful information about which template file was used to generate a section of markup.

Example:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'node' -->
<!-- FILE NAME SUGGESTIONS:
   * node--view--frontpage--page-1.html.twig
   * node--view--frontpage.html.twig
   * node--2--teaser.html.twig
   * node--2.html.twig
   * node--page--teaser.html.twig
   * node--page.html.twig
   * node--teaser.html.twig
   x node.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/icecream/templates/node.html.twig' -->

Note: At the end of this exercise, you'll find a video walk-through of the solution.

Categories
Drupal 8, 9, and 10
More information

In order to change Drupal's default markup you need to override template files. The page template controls the overall layout of your theme, including the placement of regions. You should complete the exercise just following the written instructions and you can use the video to help if you get stuck.

If you want to try and do this on your own first you'll need to:

  • Override the currently used page.html.twig template file.
  • Modify the content of the file to include the regions defined in the theme's .info.yml file.
  • Wrap the regions in the page template file with HTML markup using CSS classes from Bootstrap to achieve the example layout below.

Illustration of layout with header, menu, breadcrumb, and higlighted regions all full width, content and both sidebars as 3 columns, and one column for each of the two footer regions.

Note: At the end of this exercise, you'll find a video walk-through of the solution.

Categories
Drupal 8, 9, and 10
More information

The available dynamic tokens or variables vary from template to template. Each "page" is built from and uses many different templates. In this exercise, we'll continue our template overriding practice by overriding the node template. We'll override it and rename it in such a way that it will only affect article nodes on our Drupal site. We'll practice the process of overriding the template, inspecting the available variables, and customizing the markup. Along the way, we'll practice using the Twig filter without. You should work on the exercise steps below first, and you can refer to the video if you need some help.

If you want to try and accomplish this on your own first you'll need to:

  • Override the node.html.twig template file and target only Article nodes.
  • Modify the template so that the content of the image field of a node is output wrapped in a <div> independent of the rest of the node's content.

Example:

Screenshot showing image field of an article node displayed above the body content with a red border around it to indicate that it is in its own container.

Note: At the end of this exercise, you'll find a video walk-through of the solution.

Categories
Drupal 8, 9, and 10
More information

In this exercise, we'll continue our template overriding practice by overriding the main menu template. We'll override it and rename it with a file name suggestion so that it will only affect the main menu component of our Drupal site. We'll consult the Bootstrap documentation and add the classes from the base nav component into our overridden main menu template file. Along the way, we'll utilize a variety of methods for adding CSS classes to HTML selectors including using attributes.addClass() and set. You should work on the exercise steps below first, and you can refer to the video if you need some help.

If you want to try and accomplish this on your own first you'll need to:

  • Override the menu.html.twig template file, using a file name suggestion to target only the main menu.
  • Add CSS classes from the base nav component in Bootstrap.

Note: At the end of this exercise, you'll find a video walk-through of the solution.

More information

In this exercise, we'll continue our template overriding practice by overriding the image field template. We'll consult the Bootstrap documentation and add a responsive image class that will apply to any images uploaded by a user to the field_image field. Once again, we'll add this class to the classes array in the set Twig tag. You should work on the exercise steps below first, and you can refer to the video if you need some help.

If you want to try and accomplish this on your own first you'll need to:

  • Override the image field template file.
  • Add values to the classes array with set.

Note: At the end of this exercise, you'll find a video walk-through of the solution.

Categories
Drupal 8, 9, and 10
More information

In this exercise, we'll practice using the t fiter in a Twig template. As a best practice, all hard-coded text in a template should be translatable. Simple text (containing no dynamic tokens) can be be passed through the t filter to achieve this objective. Along the way, we'll also use a basic conditional if statement with Twig. You should work on the exercise steps below first, and you can refer to the video if you need some help.

If you want to try and accomplish this on your own first you'll need to:

  • Add a <div class="label label-warning"> element to the article node template file that contains the string "This node is not published".
  • Use the t filter to make the hard-coded text translatable.
  • Use basic logic statements with Twig to check if the node is published or not.

Note: At the end of this exercise, you'll find a video walk-through of the solution.

Categories
Drupal 8, 9, and 10
More information

Preprocess functions allow you change existing variables, or add new variables, for a template file using PHP code. In this final exercise for the course, you'll define a PHP function that implements a preprocess hook and define a new variable using PHP and make it available for use within a specific template file. Specifically we want to create a new variable named {{ today }} that contains the current date and gets passed to the page.html.twig template file. You should work on the exercise steps below first, and you can refer to the video if you need some help.

If you want to try and accomplish this on your own first you'll need to:

Learning objectives:

  • Create a THEMENAME.theme file.
  • Write a proprocess PHP function that adds variables to a page template file.

Note: At the end of this exercise, you'll find a video walk-through of the solution.

This course appears in the following guides:
Theming
Learn to control the look, feel, and markup of a Drupal site in a theme.

Frontend Theming