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.
Set up a local development environment to practice Drupal theme development exercises in our course, Hands-On Theming Exercises for Drupal.
By the end of this tutorial, you should be able to:
- Install Drupal on your computer, so you can edit files in your theme.
- Generate dummy content, so that you have different kinds of pages to theme.
It's time to create the bare-bones structure for a new theme on your site. You should try to complete this exercise based what you've learned from the tutorial prerequisites listed below. The video at the end of this tutorial will walk you through the implementation of this exercise if you need some help. In this exercise, we'll:
- 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.
By the end of this exercise, you should feel comfortable starting a theme using several methods.
To add CSS or JavaScript files or libraries to your site, you can attach them as asset libraries in your theme. In this exercise, you'll create 2 asset libraries and attach them globally via your theme's info file. In this tutorial, we'll 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 is focused on teaching the Drupal aspects of theme development, and not on writing CSS, we're using the Bootstrap CSS. Feel free to use the framework or library of your choice if you don't want to use Bootstrap.
You should try to complete the exercise steps on your own and use the video to help guide you if you get stuck.
At the end of this exercise, you'll find a video walk-through of the solution.
Drupal has a few handy settings you can tweak to make developing themes a little more intuitive and a lot more awesome. In this tutorial, we'll practice manually setting up our environment for theme development by:
- Disabling some caches
- Turning off CSS and JS aggregation
- Turning on the Twig debug service
By the end of this tutorial, you'll have practiced setting up your environment for theme development.
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 practice the exercise following the written instructions below. Use the video walk-through to help if you get stuck.
In this exercise, we'll:
- 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.
By the end of this tutorial, you'll gain practice creating a custom layout in a page template file.
The available dynamic tokens or variables vary from template to template. Each page is built from a set of templates.
In this exercise, we'll:
- Override and name the node template file so that it will only affect Article nodes on our Drupal site.
- Inspect the available variables.
- Customize the markup.
- Use the Twig filter
without
.
We recommend that you try to work through the exercise yourself, and refer to the video if you need help.
In this exercise, we'll practice theming by overriding the main menu template. We'll:
- Override and rename the main menu template so that it will only affect the main menu component of our Drupal site.
- Add Bootstrap classes from the base nav component into our overridden main menu template file.
- Add CSS classes to HTML selectors using
attributes.addClass()
andset
methods.
We recommend that you work on the exercise steps below first. You can refer to the video if you need some help.
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.
In this exercise, we'll practice using the t
filter 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 passed through the t
filter to achieve this objective. Along the way, we'll also use a basic conditional if
statement with Twig. We recommend that you try following the exercise's steps first, and refer to the video if you need help.
Preprocess functions allow you to change existing variables, or add new variables, for a template file using PHP code. In this exercise, you'll:
- Define a PHP function that implements a preprocess hook
- Create a new variable named
{{ today }}
that contains the current date and gets passed to the page.html.twig template file.
We recommend that you try the exercise's steps first, and refer to the video if you need help.