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 Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Introduction Assume you have a drupal website and using cloudflare. You are…

Drupal - How to rename column of a content type

Drupal - How to rename column of a content type

Introduction You already have a content type with one or more fields in it…

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…

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Problem Statement I’ve been using image styles, and heavily used “Scale and crop…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…