Drupal 9's core frontend theme is Bartik. Additionally, six backend themes are available. There is no doubt that they are great and versatile. Nevertheless, custom theme development is for you if you want your website to stand out from the rest. How to create a custom theme simply in Drupal 9? Our blog contains the answers you need to do this, so keep reading.
Why Drupal 9 for custom theme development?
Drupal 9 is extremely flexible in composing different elements and creating the best user digital experience. In a word, you or the development team will be able to implement any vision of your site with maximum accuracy on Drupal. Now let's highlight the advantages and disadvantages of developing custom themes.
Pros of custom theme development
1. Quick load
A custom theme is often much faster than a regular free theme. The theme's responsiveness reduces the loading time of the site as a whole and has a positive effect on SEO positions.
2. High security
It is more secure to use custom themes. The custom theme contains only secure code, making it impossible for the information to leak or any vulnerability.
3. Flexibility
Themes designed specifically for you are highly flexible, making it easy to update and change if necessary.
Cons of custom theme development
Cost
A custom theme requires investment compared to a free one. The cost of creating is not as high as many people think, but still not free.
Step-by-step guide on how to build a Drupal 9 theme
Let's take a look at the Drupal 9 custom theme creation. Defining it in your-theme-name.info.yml is the most critical part of this process. This file contains all metadata, libraries, block regions, and other necessary information.
Step #1. Give a name to the future custom theme
Name your theme. So as not to repeat existing names, it is recommended to use still-unknown names. For convenience, keep names short, no more than 50 characters.
Step #2. Create your own theme in the folder
Next, we created a theme in the `web/themes/custom` folder. We'll name the folder as our theme is called.
Step #3. Create the info.yml file
Then create an info.yml file. Your info.yml file will contain the primary key values. Your code should look something like this:
|
|
Step #4. Create a libraries.yml file
The next step is to create a libraries.yml file to specify and place the libraries you need for your site to function correctly. There will be CSS and JS. We name the library glo-libraries.
|
|
Step #5. Link libraries.yml file with the new theme
We're almost halfway there, don't give up. Now that we've created the libraries.yml file, we need to link it to our theme. To implement this, we add it to the info.yml file.
|
|
Step #6. Inherit the Base Theme
A base theme allows you to choose whether to create an entirely new theme or a subtheme that inherits the resources of an existing theme. Inheriting the Drupal core theme is the next and mandatory step. As a result, info.yml will contain the base theme's key.
|
|
Step #7. Set the "regions" for the new theme
The next logical step is to define regions for the theme. All details should be listed under the "regions" key. In the default configuration, the "regions" are already set. All changes you make to these settings will be your responsibility. *Branding here is a region identifier. The branding here is the name of the region.*
|
|
Step #8. Override page.html.twig
You must include new region settings on the page.html.twig file if you have set up regions for your theme.
|
|
Now we've rendered our new regions on the page.
Step #9. Find your custom theme in the Uninstalled themes section
To see the result of your actions, i.e., your custom theme, go to the Uninstalled themes section. Click the "Install and set as default" button to install it. Then go to the "Block layout" section. You will find the option "Demonstrate block regions (Custom Theme)." Follow this link to see all of the themes's regions.
Step #10. Add CSS
Finally, we got to the last step. You should apply CSS styles for each region based on your design. style.css should include CSS according to your requirements.
|
|
The Drupal 9 custom theme is ready!
Create your custom theme today
Creating your own theme in Drupal 9 is a difficult path, but worth all the effort. A ready-made theme is easier to use than a custom one. However, custom theme development allows your organization to tailor themes specifically to its requirements. We hope this mini-guide helps you get started developing Drupal 9 themes for your company. Or you can let the Meant4 team handle it! Drop us a line here.
Written by:
Oleksandr Bazyliuk
Senior PHP Developer
Oleksandr Bazyliuk, a real Drupal ninja with over 10 of experience as a Senior PHP Developer. Specializing in all Drupal versions, Oleksandr seamlessly integrates custom modules and themes, wielding PHP with precision to craft dynamic and high-performance web applications. His code is a work of art, reflecting a commitment to excellence and a knack for solving complex technical challenges
Read more like this:
Ewelina Zwolenik
CSS Flexbox vs CSS Grid explained
Jakub Bednarski
Embracing Modern Web Design with Tailwind
Oleksandr Dekhteruk
Everything You need to know about Drupal hosting
Oleksandr Dekhteruk
How to build an eCommerce website with Drupal
Got idea?Let’s talk about your project
Schedule a call with us