Barrio
This chapter describes how to work with the Drupal Barrio theme module for Bootstrap 5.
Table of contents
- Introduction
- Installing
- Create a subtheme
- Working with the SASS subtheme
- Colours
- Theme development
- Final word
Drupal projects discussed in this chapter: Barrio, Barrio SASS SK.
Introduction
Note: These notes were created as part of me evaluating Barrio in 2021. They are not complete and may contain errors. Use with caution.
Barrio is the most used Bootstrap 5 theme for Drupal.
It provides an extensive GUI for configuring in the theme settings including the Bootstrap components, layout, colours and fonts. This GUI-based approach may not suit developers who want to have total the look and feel of a site through the CLI, working directly with SCSS files and twig templates.
It comes with a custom subtheme starter kit that uses a CDN to load Bootstrap. This means that the variables used for the Bootstrap colour utility classes cannot be changed. To change these, you need to load Bootstrap locally, and recompile Bootstrap css from scss. For this, there exists a companion project (Barrio SASS Starter Kit) to have the scss locally and scripting rebuilds with gulp.
Theming in Boostrap 5 is accomplished by Sass variables, Sass maps, and custom CSS. Source: Get started with Bootstrap.
Barrio Bootstrap 4/5Installing
To download, run the following via command line inside your siteroot:
$ composer require 'drupal/bootstrap_barrio:^5.5'
This downloads the Barrio theme project and places it in
the themes/contrib
folder It contains two themes: The
main theme, named Bootstrap Barrio and the basic
structure of a subtheme, named Bootstrap Barrio
Subtheme. You will see both if you navigate
to .
Create a subtheme
The main theme is not intended to be used on its own. For instance it does not contains any css so it will appear unthemed. To be able to use it, always create a custom subtheme.
Two alternative ways of creating a subtheme is provided:
- CDN subtheme – can not recompile css.
- SASS subtheme – can recompile css.
Both are described in the following sections.
CDN subtheme
The theme comes with a script to create a custom subtheme.
If
you are not running Apple macOS, the supplied script to
create a subtheme will produce the error message: “sed: can't read :
No such file or directory”. To fix this, edit the script and
change the text:
“sed -i '' -e
” → “sed -i -e
”.
Source: SO.
Also make sure that the subtheme does not already exist.
Run the script:
$ pwd …/web/themes/contrib/bootstrap_barrio $ chmod a+x scripts/create_subtheme.sh $ ./scripts/create_subtheme.sh +------------------------------------------------------------------------+ | With this script you could quickly create barrio sub-theme | | In order to use this: | | - bootstrap_barrio theme (this folder) should be in the contrib folder | +------------------------------------------------------------------------+ The machine name of your custom theme? [e.g. mycustom_barrio] my_barrio Your theme name ? [e.g. My custom barrio] My Barrio # Check the themes/custom folder for your new sub-theme. gisle@karde:/var/www/guided.roztr.org/web/themes/contrib/bootstrap_barrio$ $ cd ../../custom/my_barrio/
Some finishing touches for the CDN subtheme:
- To make navbar-toggler-icon visible on small screens. Navigate to and click “Settings” for the subtheme. Expand and click “Settings” for the subtheme. Change “Navbar link color” “Default” to “Dark”. Click “Save configuration”.
- Fix path of logo in
color/preview.html
to a valid one. - Add newline to the end of
color/preview.html
. - Change description in
my_barrio.info.yml
. - Delete the line “# Information added by Drupal.org packaging script” … and everything below it in
my_barrio.info.yml
.
The URLs for the CDNs are defined
in my_barrio.libraries.yml
as follows:
bootstrap_cdn: js: /libraries/popper.js/dist/umd/popper.min.js: { weight: -19 } /libraries/bootstrap/dist/js/bootstrap.min.js: { weight: -20 } css: component: //cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css: {}
The use of bootstrap_cdn
is specified in
my_barrio.info.yml
as follows:
libraries: - my_barrio/bootstrap_cdn - my_barrio/global-styling
This screendump shows how the newly created custom subtheme initally looks like:
The theme does not look ready to use. Contrast is too low, and the branding in the navbar and the footer has an awkward placement of the elements.
To test the responsiveness of the subtheme and how it looks on a
small screen, in Firefox
press Ctrl+Shift+M
. After some contrent has been created,
this should produce something like the screenshot below:
The subtheme's regions are in my_barrio.info.yml
. They are just copied from barrio.info.yml
:
regions: top_header: 'Top header' top_header_form: 'Top header form' header: 'Header' header_form: 'Header form' 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' featured_bottom_first: 'Featured bottom first' featured_bottom_second: 'Featured bottom second' featured_bottom_third: 'Featured bottom third' footer_first: 'Footer first' footer_second: 'Footer second' footer_third: 'Footer third' footer_fourth: 'Footer fourth' footer_fifth: 'Footer fifth'
You may view their placement by navigating to
and click “Demonstrate block regions (My Barrio)”.Note that two of the regions that are defined (“Page top” and “Page bottom”) are missing from the page template.
A prototype for the page template is in the
subtheme's templates
directory,
named “_page.html.twig
”. To experiment with it, copy
to “page.html.twig
” and rebuild the cache.
A lot of settings can be set in the theme's GUI by navigating to
. The vertical tab has the following elements:- Layout:
- Container:
Let you set the
fluid-container
class. This will make the main container not being contrained to 1140 px, but be 100 % (i.e. take up all availble space.) - Region: Let you set custom classes for the regions.
- Sidebar position: Both, left or right.
- Sidebar first layout: Width.
- Sidebar second layout: Width.
- Container:
Let you set the
- Components
- Buttons: Default button size. Outline buttons.
- Navbar: Constrain navbar width to container. Navbar colours. Classes.
- Messages: Messages widget.
- Form: Widgets for forms (Radio, Checkbox, Select, File).
- Affix: Components to make sticky.
- Scroll Spy: Bootstrap Scrollspy.
- Fonts & Icons: Set up Google font icons, bootstrap icons and icon set.
- Colors: Set up colours for system messages and tables.
Additional settings are:
- Page element display: Checkboxes for elements to diplay
- Logo image: Path to custom logo image.
- Favicon: Path to custom favicon.
The video WebWash: Getting Started with Bootstrap 4 using Barrio in Drupal 8 (2018-10-16 21:37) on YouTube still provides a good introduction, but some parts are already outdated.
SASS subtheme
There are several methods available to work with Bootstrap locally. The one described below makes use of the Barrio SASS Starter Kit to create subtheme with SASS sources.>
First download the project using composer:
$ composer require 'drupal/bootstrap_sass:^5.0'
This places it in the themes/contrib
folder.
Then Bootstrap SASS starter kit contains a bash script that
createa a subtheme in the themes/custom
folder:
$ cd web/themes/contrib/bootststrap_sass $ bash ./scripts/create_subtheme.sh +------------------------------------------------------------------------+ | With this script you could quickly create bootstrap_sass sub-theme | | In order to use this: | | - bootstrap_sass theme (this folder) should be in the contrib folder | +------------------------------------------------------------------------+ The machine name of your custom theme? [e.g. mycustom_bootstrap_sass] my_bootstrap Your theme name ? [e.g. My custom bootstrap_sass] My Bootstrap # Check the themes/custom folder for your new sub-theme.
Some finishing touches for the SASS subtheme:
- Change description in
my_bootstrap.info.yml
. - Remove the line “# Information added by Drupal.org packaging script” … and everything below it in
my_bootstrap.info.yml
. - If present, remove the line “bootstrap_barrio/file: false” from
my_bootstrap.info.yml
. (See: Library from basetheme should not be loaded from subtheme).
You should now be able to install and set as default the subtheme named My Bootstrap.
There are descriptions about how to to this manually in articles from OpenSenseLabs.com: How to create Barrio sub-theme in Drupal 8 using SaSS Barrio Starterkit and Axelerant.com: Setting up A Custom Bootstrap Theme With Drupal 8. The latter also provides an example about customizing colours.
Navigate to
, locate “My bootstrap” and click “Install and set as default”.When you return to the front page, site will appear unthemed. This is because the theme is not built yet.
To build the theme, go to the root directory of the subtheme and do the following:
$ pwd …/themes/custom/my_bootstrap $ npm install … found 0 vulnerabilities
This will install the dependencies in the local folder.
To re-create .css
after changing
the .scss
, use gulp. This will process the
tasks specfied gulpfile.js
.
If you are able to use BrowserSync, edit the
sample gulpfile.js
supplied to make the proxy point to
your domain.
If you are in a headless environment you shall not be able to use BrowserSync, and you wil get the following error:
[Browsersync] Couldn't open browser (if you are using BrowserSync in a headless environment, you might want to set the open option to false)
You do this by editing the sample gulpfile.js
.
Add the line “open: false,
” as follows:
// Static Server + watching scss/html files function serve () { browserSync.init({ proxy: 'http://yourdomain.com', open: false, }) gulp.watch([paths.scss.watch, paths.scss.bootstrap], styles).on('change', browserSync.reload) }
Source: [#3162844]@Drupal.org.
Gulp is supposed to be run in the backround. It will monitor any changes you make to the SASS source files and regenerate the CSS. To see the changes, rebuild the cache after you see the “Finished 'styles'” message.
$ gulp & [14:36:52] Using gulpfile …/themes/custom/my_bootstrap/gulpfile.js [14:36:52] Starting 'default'... [14:36:52] Starting 'styles'... [14:36:57] Finished 'styles' after 4.75 s [14:36:57] Starting 'js'... [14:36:57] Finished 'js' after 10 ms [14:36:57] Finished 'default' after 4.77 s … [14:43:28] Finished 'styles' after 9.1 s [Browsersync] 2 files changed (bootstrap.min.css, style.min.css) [14:43:28] Finished 'styles' after 9.19 s $ drush cr
The theme appears unthemed if the setting for library is “None”. To fix this. navigate to
and in the pulldown menu for “Load library”, replace “None” with “Local”).If you now visit the front page, it looks like this:
IMHO, the theme does not look ready to use. Contrast is too low, and the branding in the navbar and the footer has an awkward placement of the elements.
The regions are roughly the same as those in the base theme. There are some differences as the regions in the blue navbar, colours and to constraints, but I haven't drilled down yet.
Directories in the custom subtheme.
- assets: Do not exist by default. Put your own asset files as required for your project.
- config: Contains configuration defaults in
“
install/my_bootstrap.settings.yml
” (some back setting that can be changed through the GUI) and “schema/my_bootstrap.schema.yml
” (region and sidebar settings). - css: Generated css files. Will be replaced when you run gulp.
- images: Custom subtheme images.
- js: Required javascript files copied on gulp process.
- scss: Complete rewrite of Bootstrap Barrio css files on scss format including Bootstrap variables.
- templates: Custom template overrides. Initially almost empty.
Files in the scss
directory, which is the main place to customize the subtheme.
Changes from 5.0.0 to 5.0.2 indicated.
- [
barrio.scss
: List of imports from thecomponents
subdirectory. Gone in 5.0.2.] import.scss
: Contains the first level of import files.mixins.scss
: empty in 5.0.2style.scss
: About 60 lines setting css for main regions.typography.scss
: Personalization of typography settings.variables.scss
: Contains Bootstrap variables most suitable for personalization.
By default the import of material design bootstrap is commented out
in import.scss
:
//material design bootstrap //@import "../node_modules/mdbootstrap/scss/mdb-free.scss";
Importing it change the subtheme for the force. It changes the primary colour and chops off left edge of content. I just left it commented out for now.
To customize the style, you should edit variables.scss
and add custom styling to style.scss
.
Defining primary and accent colours as Bootstrap variables will generate the whole colour schema using Google Material Design colour management concept.
For example, changing the variable $accent-shade
from #0079C0
to #7e57c2
will change the
background colour of the navbar.
The array $theme-colors
defines the colours use for the Bootstrap 4
colour utility classes.
Background:
- Drupal.org: Installaton
Working with the SASS subtheme
I am not using the CDN subtheme. The rest of this chapter is assuming you are working with the SASS subtheme.
Most of the configuration of the Barrio SASS subtheme is done by the GUI. This section starts out by giving an overview of the GUI for the SASS subtheme, the rest goes through various objectives (e.g. replacing the logo), and how to resolve them.
GUI settings overview
A lot of settings can be set in the theme's GUI by navigating to
. The vertical tab has the following elements:- Layout:
- Container:
Let you set the
fluid-container
class. This will make the main container not being contrained to 1140 px, but be 100 % (i.e. take up all availble space.) - Region: Let you set custom classes for the regions.
- Sidebar position: Both, left or right. Also: Content offset.
- Sidebar first layout: Collapse, width and offset..
- Sidebar second layout: Width and offset.
- Container:
Let you set the
- Components
- Buttons: Default button size. Outline buttons.
- Images: Apply img-fluid style to all content images. This is essential to create a responsive website.
- Navbar structure: Constrain navbar width to container. Navbar colours. Classes.
- Navbar behaviour: Flyout style mainmenu. Sliding navbar.
- Tabs (local tasks): Tab style.
- Messages: Messages widget.
- Form: Widgets for forms (Radio, Checkbox, Select, File).
- Affix: Components to make sticky. Partly same settings as .
- Scroll Spy: Bootstrap Scrollspy.
- Fonts & Icons: Set up Google font icons, bootstrap icons and icon set.
- Colors: Set up colours for system messages and tables.
Additional settings are:
- Page element display: Checkboxes for elements to diplay
- Logo image: Path to custom logo image.
- Favicon: Path to custom favicon.
- Load library: Pulldown menu that allow you to pick various CDN preset palettes or “Local”. The latter let you do this yourself.
Replacing the logo
Uploading an .svg
logo through the subtheme's GUI
doesn't work (invalid file type). But placing it in or
below public://
works. The screenshot below shows a
logo with the path public://unmanaged/guided.svg
being used instead of the default.
Remove RSS icon
Copy core/themes/stable/templates/misc/feed-icon.html.twig
into the templates
directory. Edit the file to remove
all markup, then rebuild the cache.
Show ISO date in byline
Change the default medium date.
Adjust page layout
Out of the box, the width of the non-fluid main container with no sidebars are 1140 pixels, with 1110 remaining after gutters are removed..
To remove the sidebars, navigate to
to set sidebar position. The options are “Left”, “Both sides” and “Right”. There is no option for “None”, but navigating to let you remove the “Search” and “Tools” block from the sidebars and (for example) place them in the “Header” region. The result will be a full width page with no sidebars.Templates
The SASS subtheme comes with the following two templates:
block--system-branding-block.html.twig
file-link.html.twig
All other templates are inherited. To work with page layout, copy these from the parent theme:
page.html.twig
node.html.twig
image.html.twig
Other templates may be copied from core:
feed-icon.html.twig
Changing the navbar
#bar_sassregions
???
There are two ribbons in the navbar. They contain the following regions:
- Navbar top (default background red)
- Secondary menu
- Top header
- Top header form
- Navbar (default background blue)
- Header
- Primary menu
- Header form
To change the settings for both, navigate to
, and click “Settings” for the subtheme. ExpandHere is a list of the settings that exists. The initial settings are in square brackets, my preferred settings, if different, in curly brackets.:
- Navbar width container: [Unchecked] Check to make navbar inside container. Uncheck to make navbar fluid width. {Checked}
- Navbar toggle size: [Large] Unclear what this does.
- Navbar top is navbar: [Checked] Check if navbar top .navbar class should be added.
- Navbar top position: [Normal] Position for Navbar top. Can be bottom, fixed or sticky.
- Navbar top link color [Dark]: {Dark}
- Navbar top background color [Secondary]: {Dark}
- Navbar position [Normal]: Position for Navbar. Can be bottom, fixed or sticky.
- Navbar link color [Dark]: {Light}
- Navbar background color [Primary]: {Light}
- Custom classes for Navbar Top ['']:
- Custom classes for Navbar ['']:
For both navbars, the background colour can be set to:
- Default (white)
- Primary (blue -
#0079C0
) - Secondary (red -
#ff4e2e
) - Light ($gray-100 -
#f8f9fa
) - Dark ($gray-800 -
#343a40
) - White (
#fff
) - Transparent.
For both navbars, the link colour can be set to:
- Default (blue)
- Light ($gray-800 -
#343a40
) - Dark ($gray-100 -
#f8f9fa
)
For the link settings, “Dark” provides light colour ($gray-100) suitable for a dark theme). “Light” vice versa ($gray-800).
Responsive images
To make all images responsive. navigate to
and check “Apply img-fluid style to all content images”.Note
that the Bootstrap 3 class “img-responsive
” is gone. See
SO: No img-responsive in Bootstrap 4.
Manage flex
GBS: Flex.
Colours
Some colours are defines by a combination of style.scss
and variables.scss
as follows.
Backgrounds:
body { background: $white; } // white - body .bg-inverse { background: $primary-shade; } // red-orange - unknown use .site-footer {background: $accent-shade; } // dark blue - footer
Anchors:
a { color: $accent-shade; } // dark blue .menu--main li a { color: $primary-shade; } // red-orange .menu--account li a { color: $primary-shade; } // red-orange.bg-secondary
Here are the colours defined:
--blue: #007bff; --indigo: #6610f2; --purple: #6f42c1; --pink: #e83e8c; --red: #dc3545; --orange: #fd7e14; --yellow: #ffc107; --green: #28a745; --teal: #20c997; --cyan: #17a2b8; --white: #fff; --gray: #6c757d; --primary: #0079C0; --secondary: #ff4e2e; --success: #28a745; --info: #17a2b8; --warning: #ffc107; --danger: #dc3545; --light: #f8f9fa; --gray-dark: #343a40; --dark: #343a40;
Theme development
The numbers refer to the video tutorials linked below.
- Install Barrio and create a subtheme as descibed above.
- Install and activate Devel (
composer require 'drupal/devel:^5.0'
) (#2). - Navigate to and disable CSS & JS aggregation (#2).
- Activate Twig debug as decribed below(#2).
To activate Twig debug, do:
$ cd sites/default $ cp default.services.yml services.yml
Edit services.yml
and in the “twig.config” section,
change “debug: false” to “debug: true”. Clear the cache. When you
now inspect pafe source, you'll find injected comments that will help
you in theme debugging.
- Install and activate Views Bootstrap (
composer require 'drupal/views_bootstrap:^5.5@alpha'
) (#3). - Create a View list of content.(#3).
- Format items as Bootstrap cards (#3).
- .
The main source for this section is the Youtube channel Drupal up. It provide a series of videos about Drupal 8 theme development. Most of it are still relevant for Drupal 10. Those I've viewed are linked below.
- Generating a Barrio sub-theme super quickly (4:45) 1 Dec 2019 – outdated
- Setup for Drupal Barrio theming developing (5:58) 11 Dec 2019
- Creating a Bootstrap Cards list with the help of Bootstrap Views module (7:18) 18 Dec 2019
- X () 2019
- X () 2019
- X () 2019
- X () 2019
- X () 2019
- X () 2019
- X () 2019
Drupal up: Setting up responsive images in Drupal 8 (Youtube 11:47)
Final word
[TBA]
Last update: 2021-01-28 [gh].