If you’re planning to use Bootstrap on your Drupal 8 site, the first obvious thing to do is download and set up the Bootstrap theme. Then, during the site building process, there will come the point where you need to create a few layouts. These layouts could be used for content types with Display Suite, or for custom pages using Panels.
Implementing layouts using the Bootstrap grid system is simple thanks to the Bootstrap Layouts module.
Read our “Getting Started with Bootstrap in Drupal 8” tutorial to learn how to use Bootstrap in Drupal 8.
Bootstrap Layouts is a module that ships a bunch of prebuilt layouts using the grid system in Bootstrap. Best of all, these layouts can be used between Display Suite and Panels, or any module which supports the Layout Discovery module.
The layouts are configurable through Drupal’s administrative UI. For example, you can adjust the width of a two column layout by choosing grid CSS classes from a multi-select field.
All this can be done without overriding a template or writing a custom preprocess hook.
In this tutorial, you’ll learn how to use Bootstrap Layouts with two popular modules: Display Suite and Panels.
Getting Started
Before we begin, go download and install the Bootstrap Layouts theme.
If you use Composer, run the following:
$ composer require drupal/bootstrap_layouts
Or Drush,
$ drush dl bootstrap_layouts
Other Requirements
First, make sure you’re using Bootstrap on your Drupal site. This could be via the Bootstrap theme or another theme which implements Bootstrap.
Read our “Getting Started with Bootstrap in Drupal 8” tutorial to learn how to use Bootstrap in Drupal 8.
Second, you’ll need to use it via a module which implements layouts using the Layout Discovery module. So far, two popular modules which use it are Display Suite and Panels.
Implement Layout using Display Suite
To implement this using Display Suite go ahead and install the module.
If you’re new to Display Suite then read our “Using Display Suite in Drupal 8: How to Customize Content Pages” tutorial with a video.
1. Go to the “Manage display” page of a content type. You can access it by going to Structure, “Content types” and click on “Manage display” from the Operations drop-down.
2. From the “Layout for article in default” tab, select a layout under Bootstrap.
3. Once selected you should see a preview of the layout.
Don’t forget to click on Save.
Change Column Width
One thing I love about Bootstrap Layouts is that you can change the column width without overriding the template.
The preview of layout shows it as having two columns, each 50% wide. Or in “Bootstrap speak”, 6 columns wide. You can change the width of the column by choosing a different class from the Classes select box from a region tab.
Implement Layout using Panels
Now that you know how to configure Bootstrap layouts using Display Suite, let’s look at using it with Panels and Page Manager.
If you’ve never used Panels or Page Manager, then check out our tutorial “How to Build Custom Pages Using Page Manager and Panels in Drupal 8“.
I’m going to assume you’ve already created a Panels page and will be switching the layout to one of Bootstrap Layouts’.
1. Go to Structure, Pages and edit an existing page.
2. Go to the Layouts section in the variant.
3. From the Layout drop-down select a Bootstrap layout, then click on “Change Layout”.
4. After clicking on “Change Layout” another link will appear below Layout on the left called “Layout Regions”. From this page you must add the existing blocks into the new regions. Once completed click on “Update and save”.
I must admit, it’s easy to miss this new page, but it’s a required step to changing layouts.
5. Now you should see a new link below Layout called “Layout Settings”.
From this page you can change the layout settings such as the grid CSS classes for each region. Same as when using Display Suite.
Summary
Bootstrap Layouts makes it really easy to build layouts because it comes with many prebuilt. But the most impressive feature is the ability to change the grid classes from the interface without having to override a template.
FAQs
Q: After clicking on “Change Layout” in Panels for a second time I get an error “You must select a different layout if you wish to change layouts.”. But nothing changes.
After clicking on “Change Layout” a new link will appear called “Layout Regions” below Layouts. You must reassign blocks into the new layout regions. This new link can easily be missed after clicking “Change Layout.
Layouts are amazing, but if you still modify your node template files while working with layouts, you have to deal with an nonsense deep array structure ($content[‘_field_layout’][‘REGIONNAME’]…) – this is the only thing holding me back using it. Someone changes the layout and BOOM.
You should also take a look on the UI-Patterns module (https://www.drupal.org/project/ui_patterns), which has a layout integration also.
Hi Thomas,
Thanks for the tip.
I keep hearing about UI-Patterns I’ll check it out.
Cheers,
Ivan
How can I create a custom layout based on a bootstrap layout like it used to on Display Suite.
Hi Krishna,
I don’t think I understand your question, however, Bootstrap Layouts can be used in Display Suite for Drupal 8. So you don’t have to create a custom layout.
Cheers,
Ivan