drupal|April 10, 2020|2 min read

Drupal 8 - How to add custom class to a drupal table view

TL;DR

Override the views-view-table.html.twig template and add custom CSS classes like Bootstrap table classes to the table element.

Drupal 8 - How to add custom class to a drupal table view

Introduction

Suppose you have a view, and you have configured your display as a table. Drupal provides no way to configure a css class for the table. And, it shows an ugly table. I was using bootstrap css, and they have some really awesome table classes. Lets see how we can add that custom class to a table view.

See the earlier table:

Drupal table view ugly

Provide a class to table view

So, there is no way on the drupal configuration to do that. We need to do little bit of twig file tweak. First, we need to see from which twig file, the output is being rendered.

Enable twig debug

  • Goto your /sites/default/services.yml
  • Search for are which says
debug: false
  • Change it to true
  • save file
  • clear the cache

Now refresh the page, and inspect the html in chrome. You will see from where the html is coming. In my case, it was showing the view name as:

/core/themes/classy/templates/views/views-view-table.html.twig

Copy that twig file in your theme’s template directory. And, edit that file. You will see a section on top where classes are being set, see below:

  set classes = [
    'views-table',
    'views-view-table',
    'cols-' ~ header|length,
    responsive ? 'responsive-enabled',
    sticky ? 'sticky-enabled',
  ]

In bootstrap, the simple class for the table is: table Simply add that class in the list, and nothing else. See changes below:

  set classes = [
    'views-table',
    'views-view-table',
    'table',
    'cols-' ~ header|length,
    responsive ? 'responsive-enabled',
    sticky ? 'sticky-enabled',
  ]

Save the file. Now, clear the cache. And, refresh your page. You should see the expected changes in your html.

See the bootstrap version of the table view

Drupal table view

Related Posts

Drupal 8 - How to hide help link About text formats and text format guidelines

Drupal 8 - How to hide help link About text formats and text format guidelines

Problem In drupal textarea field, it was always a pain to see the two links…

Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…

Troubleshoot AWS Lambda unknown error!

Troubleshoot AWS Lambda unknown error!

After 2 days, there was my demo. I deployed my nodejs code on lambda function…

Implement a command line shell by using Command Dispatcher in Python

Implement a command line shell by using Command Dispatcher in Python

Lets implement a command shell by using a command dispatcher. The objective is…

Python 3 - Format String fun

Python 3 - Format String fun

This post is dedicated for cases where we intend to append a variable value in a…

How to mock a constructor - Junit test case development issues

How to mock a constructor - Junit test case development issues

While writing JUnit test cases, we encounter cases like we want to initialize a…

Latest Posts

System Design Patterns for Managing Long-Running Tasks

System Design Patterns for Managing Long-Running Tasks

Introduction Some operations simply can’t finish in the time a user is willing…

System Design Patterns for Real-Time Updates at High Traffic

System Design Patterns for Real-Time Updates at High Traffic

The previous articles in this series covered scaling reads and scaling writes…

System Design Patterns for Handling Large Blobs

System Design Patterns for Handling Large Blobs

Introduction Every non-trivial application eventually needs to handle large…

Explaining SAGA Patterns with Examples

Explaining SAGA Patterns with Examples

In a monolith, placing an order is a single database transaction — deduct…

System Design Patterns for Scaling Writes

System Design Patterns for Scaling Writes

In the companion article on scaling reads, we covered caching, replicas, and…

Serverless vs Containers — The Decision I Keep Revisiting

Serverless vs Containers — The Decision I Keep Revisiting

Every time I start a new service, I have the same argument with myself. Lambda…