How to create simple image slider using bootstrap carousel in Drupal

Image sliders are essential requirement of all websites. There are contributed modules for image sliders, view slide show is one among them. you can read more about views slide show here.

another module is slick (https://www.drupal.org/project/slick)

one of the draw back of these modules is difficulties facing during customizations. For example if you want to customize text on each  slide  with different html tags for each slider , it will be difficult  while using these modules. Here we are going to create an image slider without any modules.

Here we are going to create image slider using carousel.js in bootstrap. We will be creating backend content type, using which administrators can manage images and text in slider. We will create a view in order to render images and text.

We are doing this in below steps

  • Create hero Slider content type
  • Create a view with block display
  • Assign slider block to region
  • Customizing view template

Here we are assuming you already got bootstrap sub theme and you have a Bootstrap 3 base theme (https://www.drupal.org/project/bootstrap)

You can see a sample sub theme in above base theme. Enable this as default theme of your site.

Download  bootstrap from (https://getbootstrap.com/docs/3.4/getting-started/) and place bootstrap folder in your subtheme folder.

Make sure below js file enabled in libraries.yml. here I am using less version of bootstrap.

Next we are going through below steps.

Step 1 – Create Hero Slider content type

Here we have created  a content type hero slider  and adding image.

Banner body is the text on image and slider image is the individual image on slider. Sort order is used for ordering images in the slider.

Step 2- create a view with block display

Next, go to views under structure.

http://yourdomain.com/admin/structure/views

here we are creating a view named hero_slider which has a block display.

Show fields in display and add slider image field and body field. Also confirm format as unformatted list.

Here machine name of view block is hero_slider_block_1

Also for slider image, field select formatter as URL to image as shown below.

 

Step 3 – Assign slider block to region

As Next step, assign view block that we created in to a region in our front page

First, we will create region in YOUR_THEME.info.yml

Here I created  a Hero Slider region in info file. Then print this region in twig template.

We have below front page template.

C:\xampp\htdocs\YOUR-PROJECT\themes\custom\YOUR_THEME\templates\system\page–front.html.twig

Just after header tag we are printing our hero slider region.

Flesh the cache and go to block layout  page under structure.

 

As shown above click on place block button and select your block and click place block button again as below.

Then select region as below and unselect Display title check box.

Then clear cache.

Now if you add content for hero slider content type,  you can see the image URL and html body content in your front page. But not in a slider form.

Step-4 Customizing view template

Next, we have to make view block in to a slider.

For this first we need to override existing hero slider view block. Our view block mechine name is hero_slider_block_1. So our template will be  views-view-unformatted–hero_slider–hero_slider_block_1.html.twig

You can read more about view templates here.

Below provided a sample slider using Bootstrap.

Also we need to apply below java script in order to work the slider.

So here we are going override our view block with carousel elements and fields from the view.

So our final content of file views-view-unformatted–hero_slider–hero_slider_block_1.html.twig

Will be as below.

Please note ,below lines are printing image and body fields respectively.

{{file_url(row.content[‘#row’]._entity.field_slider_image.entity.uri.value)}}

{{row.content[‘#row’]._entity.body.value|raw }}

add below script in your java script file.

 

Flush the cache you can see The slider with caption on the slider image.

 


 

Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )
if you like this article Buy me a Coffee this will be an ispiration for me to write articles like this.

You may also like...