How to give a naming convention of the twig in Drupal8 views?
Hello, today we are going to discuss the naming convention of the twig in views. Before going to views, let’s discuss a brief of Drupal8, which is a very popular CMS (Content Management System) in web development.
Drupal8 is an open-source, flexible, and secure CMS that’s become increasingly progressive. It allows us to integrate the popular modules very easily such as Google reCAPTCHA, Authenticator, Carousels, SiteStudio, etc.
Views module (which is now the part of Core), we can easily fetch content from the database of your site and present it to the user as lists, posts, fields, galleries, tables, maps, graphs, menu items, blocks, reports, etc. You can also implement the filters easily to show the sorted content.
1. Let’s start with View Creation
Go to Manage => Structure => Views => click on +Add view
After Creating the View, you need to save some view properties like show content, fields, block settings, add view pager if needed
2. Need to create Page or Block for Content data
Once you have saved the view page data, the page will be redirected to the view Display configuration section having page and block. Here you have multiple configurations like title, fields, content, filter criteria, sort criteria.
Like this, you need to set up one display type Block and uses an unformatted list and add content data so that we can use those field values in our custom twig templates. If you want to add a pager, like full pager, min pager so you just going to the Pager section and set the pager type, shown below…
Then save the configuration that you have added. Now you are good to proceed to the further step where we use those values in our custom twig template. Let’s begin the actual topic discussion now one by one.
3. Firstly, Go through the inbuild views template names before make own
We have some parameters (machine names) like that we gave at the time of creation.
For example: VIEW_ID, BLOCK_ID, FIELD_ID.
There are inbuild default core files in the Drupal,
In suggestions, you can see that by default these files were render
views-view — block.html.twig
These are the files that render by default to show the data that you added but, these are looking very simple or plain (without any styling). So, copy these files from core “YOUR_PROJECT\core\modules\views\templates” into your theme “YOUR_THEME/templates/views”.
4. Renaming the custom view twig template files
To get to know more information for renaming the customer twig templates then read the full article here.