Drupal Live Site Build (Part 1) – Project Set Up, Build Bootstrap Card Component using Layout Builder
Tags: drupal, bootstrap
Published: 2020-12-16 10:32:39

Get notified when new tutorials are published. [Subscribe to our newsletter](https://www.webwash.net/newsletter/).

{% youtube iwJW18NA_S4 %}

**Don’t forget to subscribe to our [YouTube channel](https://www.youtube.com/channel/UCKcUKWDTlwYnghpwLE_ltMQ) to stay up-to-date.**

Below are the show notes for the first video in a series on building a Drupal site using layout builder and Bootstrap. To help with the build process we’re going to follow a template and in each live stream session, we’ll build a component from the template.

And the plan is to have a semi-complete website after a few live streams.

In this first video, we’re focusing on the three-card components below the homepage carousel.

![00-2020-05-12_14-53-44](https://dev-to-uploads.s3.amazonaws.com/i/jny7bimxjiwsi7xn9znf.jpeg) 

We built the three-card components using a custom block type and layout builder. We’re not using paragraphs in this video.

So here are the show notes for the video.

💻 Get a copy of the built site from [GitHub](https://github.com/WebWash/drupal-bootstrap-live-demo-site).

## Project Set-Up

### Download Drupal

First go download Drupal using Composer.
```
composer create-project drupal/recommended-project FOLDER_NAME --no-interaction
```

### Set Up Lando

In this video, we used [Lando](https://lando.dev/) for our local environment.

```
lando init --source cwd --recipe drupal8 --name bs-build --webroot web --full
```
### Download Modules and Themes

Go download the following:

- [Display Suite](https://www.drupal.org/project/ds)
- [Field Group](https://www.drupal.org/project/field_group)
- [Bootstrap4](https://www.drupal.org/project/bootstrap4) (Drupal theme)
- [Drush](https://docs.drush.org/en/master/install/)

```
composer require drupal/ds drupal/field_group drupal/bootstrap4 drush/drush
```

### Install Modules

Install the following modules:

- Layout Builder (core)
- Media Library (core)
- Field Group
- Display Suite

```
drush en ds field_group media_library layout_builder
```

## Sub-theme

### Generate Sub-theme

The Bootstrap 4 theme allows you to create a sub-theme in two ways; using a script or via the settings form. We’re going to look at using it via the settings form.

1\. Install the Bootstrap4 theme

![01-2020-05-15_12-29-39](https://dev-to-uploads.s3.amazonaws.com/i/s2mvohcakyjzo6onhlr9.jpeg)

2\. Click on Settings, and scroll to the bottom and expand the “Subtheme” section.

3\. Enter in details into the form, then click on the Create link.

![02-2020-05-15_12-32-33](https://dev-to-uploads.s3.amazonaws.com/i/seplxizqzzexlkxhhlm2.jpeg)
 

NOTE: In the video, this step didn’t work because I got a fatal error because I didn’t have the `Symfony\Component\Filesystem\Filesystem` library. The only way I was able to get this library was by installing Drush using Composer, `composer require drush/drush`. 

### Configure Sub-theme

Once the sub-theme has been generated go and install and set as default.

Go to the theme settings page.

![03-2020-05-15_12-50-56](https://dev-to-uploads.s3.amazonaws.com/i/dh0pyupu101tnskszw20.jpeg)

Configure the settings like the image below then click on “Save configuration”.

![04-2020-05-15_12-51-27](https://dev-to-uploads.s3.amazonaws.com/i/7iub29ujt8hxmp9cyemc.jpeg)

### Configure Block Placement

You’ll need to add the correct blocks into the right regions or the site will look broken.

Arrange the blocks like the image below: 
 
![05-2020-05-15_12-53-00](https://dev-to-uploads.s3.amazonaws.com/i/oyeh09w77nxkaq3u2ejd.jpeg)
 

### Compile Sass in Sub-theme

We’ll use [Laravel Mix](https://laravel-mix.com/docs/master/basic-example) to compile Sass which I find the easiest to set up.

Before you begin, make sure you [download and install Node.js](https://nodejs.org/en/download/) and you can run the npm command.

1\. Go into your sub-theme and run the following command:

```
npm init -y
```

Just follow the prompts and you should end up with a package.json file.

2\. Then install Laravel Mix with the following command:

```
npm install laravel-mix cross-env --save-dev
```

3\. In the sub-theme create another file called webpack.mix.js and add the following to it:

```
let mix = require('laravel-mix');

mix.sass('scss/style.scss', 'css/');
mix.options({
  processCssUrls: false
});
```

4\. Open up package.json and replace the scripts section with the one below:

```
  "scripts": {
    "dev": "npm run development",
    "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "npm run development -- --watch",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },
```

This adds NPM scripts which you’ll need to use to compile the Sass. You’ll need to run these commands from within the sub-theme.

The two important scripts are:

```
npm run watch
```

Use this script to automatically compile when a file is changed. This should be used locally while developing a site.

```
npm run production
```

You should run this script when you’re ready to deploy to production. It’ll minify the CSS and JavaScript file to reduce the size.

NOTE: If you’re having problems setting it up then please refer to the [official documentation](https://laravel-mix.com/docs/master/installation).

### Twig Debugging

When you’re modifying templates it’s best to turn on Twig debugging so you can see where the templates are stored.

Following the steps in this link to [turn on Twig debugging](https://www.drupal.org/node/1906392).

## Drupal Site Building

### Display Suite Settings

Before we start the site building process, go to Display Suite and configure the following:

1\. Go to Structure, "Display Suite", click on Settings and check "Enable Field Templates".

![06-2020-05-15_14-04-09](https://dev-to-uploads.s3.amazonaws.com/i/5gucqu7jf62bzhb2tng3.jpeg)

2\. Click on the Classes tab and add the following into "CSS classes for regions":

```
card|Card
card-deck|Card Deck 
```

![07-2020-05-15_14-05-23](https://dev-to-uploads.s3.amazonaws.com/i/da62sh5410wehsmafa50.jpeg)

### Configure Image Media Type

We'll be using the Media field to handle the image on the Card component which we'll set up next.

But we need to make a few changes to the Image media type.

### Create Bare View Mode

Go to Structure, "Display Modes", "View modes" and create a Bare view mode for the Media entity.

To do so, just click on "Add new Media view mode". 

![08-2020-05-15_14-20-45](https://dev-to-uploads.s3.amazonaws.com/i/mogmsgy481i28ixhshat.jpeg)
 
### Configure Bare View Mode on Image Media Type

Then go ahead and configure the Bare view mode on the image media type.

1\. Go to Structure, "Media types" and click on "Manage display" on the Image row.

2\. Click on "Custom display settings" and enable Bare.

![09-2020-05-15_14-23-49](https://dev-to-uploads.s3.amazonaws.com/i/1283mee6i1st4lhgh38f.jpeg)

3\. Once enabled click on the Bare tab.

![10-2020-05-15_14-24-18](https://dev-to-uploads.s3.amazonaws.com/i/ss7ynal94qotz49khotz.jpeg)

4\. Select "Reset layout" as the Display Suite layout at the bottom.

![11-2020-05-15_14-25-26](https://dev-to-uploads.s3.amazonaws.com/i/oc8al2d42lmexoba834s.jpeg)

5\. Scroll up and make sure the Image field is the only field in the content region. Also, click on the cog-wheel and change the "Choose a Field Template" to "Full reset".

![12-2020-05-15_14-29-05](https://dev-to-uploads.s3.amazonaws.com/i/jjxk8a8g3zplf9sf2y9b.jpeg)

6\. Scroll down and click on Save.

The reason why we created a new view mode is that we need to remove all the markup that gets generated on fields. We only want the image element and that's it. We do not want the DIVs which wrap the fields. 
   
### Create Card Block Type

To handle the individual card component we'll create a custom block type called Card.

1\. Go to Structure, "Block layout", "Custom block library" and click on "Block types".

![13-2020-05-15_13-59-30](https://dev-to-uploads.s3.amazonaws.com/i/uzegs81fcrovshci49ul.jpeg)

2\. Click on "Add custom block type" and create the Card block type with the following fields:

- Description (field_description), Text (formatted, long)
- Image (field_image), Media (can't see the Media field? Install the Media module.)

![14-2020-05-15_14-15-06](https://dev-to-uploads.s3.amazonaws.com/i/lkq8kbrlripio71irgbv.jpeg)

- Title (field_title), Text (plain)

![15-2020-05-15_14-00-19](https://dev-to-uploads.s3.amazonaws.com/i/1ajy85ai280z4bcr3w6v.jpeg)

### Configure Card Formatters

While on the Card block type click on "Manage display".

![16-2020-05-15_15-51-48](https://dev-to-uploads.s3.amazonaws.com/i/ob2w7jphp3k1g4yqwawx.jpeg)

1\. Scroll to the bottom of the page and select "One column layout" from the "Layout for card in default".

![17-2020-05-15_15-52-37](https://dev-to-uploads.s3.amazonaws.com/i/rnsiafg77q31kwd1klgr.jpeg)

2\. In the field section, move Image to top, then click on the cog-wheel and select Bare as the "View mode" and "Full reset" as the field template.

![18-2020-05-15_15-53-41](https://dev-to-uploads.s3.amazonaws.com/i/faqfz1aj775swdbr92y5.jpeg)

3\. We need to create a DIV which will wrap the Title and Description, this will be called the "Card body". We'll create a field group for this.

Click "Add field group" at the top and select "HTML element", give the label "Card body":

- Element: div
- Extra CSS classes: `card-body` (this is very important, make sure you enter in the right class)    

![19-2020-05-15_15-56-00](https://dev-to-uploads.s3.amazonaws.com/i/8ong8qrmgr2unuzfey5m.jpeg)

Place the "Card body" field group below the Image field.

4\. Place the Title and Description field as child element inside of "Card body".

![20-2020-05-15_15-59-32](https://dev-to-uploads.s3.amazonaws.com/i/q2haeiclhaz5ihg1z582.jpeg)

5\. Click on the Title field cog-wheel and select Expert as the field template. Check "Field item" and enter h5 into Element and card-title into Classes. 
 
![21-2020-05-15_16-01-34](https://dev-to-uploads.s3.amazonaws.com/i/john899r6aedcj1morbl.jpeg)

6\. Click on the Description cog-wheel and select "Full reset" from the field template.
 
![22-2020-05-15_16-02-18](https://dev-to-uploads.s3.amazonaws.com/i/go1wz3c0ql7covcmhchg.jpeg)

Once everything is complete make sure the formatters are configured like below:

![23-2020-05-15_16-03-14](https://dev-to-uploads.s3.amazonaws.com/i/ew1xaaup865nq34g45r8.jpeg)

7\. Last but not least, scroll down to "Custom classes" and make sure you select the Card option in "Class for layout". (NOTE: Make sure you've added the "Card" class in the Display Suite settings.)

![24-2020-05-17_20-12-02](https://dev-to-uploads.s3.amazonaws.com/i/5q4kut74g38yuutslul7.jpeg)

## Layout Builder

### Enable Layout Builder "Basic page" Content Type

Make sure you've enabled the Layout Builder module because we'll use it to display the cards on a page.

1\. Go to Structure, "Content types" and click on "Manage display" on the "Basic page" row.

2\. Scroll to the bottom and click on "Custom display settings" and enable "Full content".
 
![25-2020-05-17_16-21-54](https://dev-to-uploads.s3.amazonaws.com/i/z8ap6x1t5x0cv0abco79.jpeg)

3\. Click on the "Full content" tab and check:

- "Use Layout Builder"
- "Allow each content item to have its layout customized." 

![26-2020-05-17_16-23-30](https://dev-to-uploads.s3.amazonaws.com/i/vlruexc6r526ch0key1q.jpeg)

### Create Page Content

Now go to Content, "Add content", "Basic page".

Create a test page then click on "Layout".

### Create Card Row Section in Layout Builder

1\. Click on "Add section" and select "One column layout" as the layout. 
 
![27-2020-05-17_16-27-55](https://dev-to-uploads.s3.amazonaws.com/i/o37zeccswuy3deo9pp0a.jpeg)

2\. From the "Configure section" click on "Custom classes" and select "Card Deck" from the "Class for layout". (NOTE: Make sure you've added the "Card Deck" class in the Display Suite settings.)

![28-2020-05-17_16-28-41](https://dev-to-uploads.s3.amazonaws.com/i/d9dyfopwu8pulzrflb0w.jpeg)

### Add Cards to Section

Once the section has been added, go ahead and add the cards.

1\. Click on "Add block" in the section.

2\. Click on "Create custom block" on the right.

![29-2020-05-17_16-31-12](https://dev-to-uploads.s3.amazonaws.com/i/vbnimexxmwrrkslgn7lf.jpeg)

3\. Then click on Card.

4\. Fill out the form and click on "Add block". 

![30-2020-05-17_16-31-59](https://dev-to-uploads.s3.amazonaws.com/i/pa2vxq9xlim7cayol5j9.jpeg)

## Override Block Template

For the cards to float next to each other correctly.

We need to override a single block template.

1. Copy the block template from `/core/themes/classy/templates/block/block.html.twig` and paste it into `/themes/custom/ww_bootstrap4/templates/block/block--inline-block--card.html.twig`.

Make sure you change the file name from `block.html.twig` to `block--inline-block--card.html.twig`.

Change it from this:

```
<div{{ attributes.addClass(classes) }}>
  {{ title_prefix }}
  {% if label %}
    <h2{{ title_attributes }}>{{ label }}</h2>
  {% endif %}
  {{ title_suffix }}
  {% block content %}
    {{ content }}
  {% endblock %}
</div>
```
To this:

```
{{ title_prefix }}
{% if label %}
  <h2{{ title_attributes }}>{{ label }}</h2>
{% endif %}
{{ title_suffix }}
{% block content %}
  {{ content }}
{% endblock %}
```

All we're doing is removing the wrapping DIVs in the template so the card deck is aligned correctly.

## Summary

In this first video, we have focused on the project set up and built a Card component using custom blocks.