How to work with Twig Templates in Drupal?

On24th Jun 2022, 2022-12-01T08:00:00+05:30 Read Pause Resume Stop
How to work with Twig Templates in Drupal?

In this article, we are going to see how a developer can work with Twig templates, and how to override templates (if needed) and create template suggestions with hooks, so that templates could be easily managed.

Basically drupal allows people to override the existing twig templates, so that people can fully have control on the html generated via the custom theme.

First thing, we need to make sure we override the templates, for this to do we have to enable the twig debugging for your local drupal application. And it's done by modifying some lines of code within your services.yml file which will be located at sites/default/services.yml, as shown below.

parameters:
  twig.config:
    debug: true

By default, you can see the value set to false, you need to set it to true.

Once you do this and rebuild the caches, you can see the existing template suggestion on inspecting the web page locally.

Now you have to write the code to alter the template suggestions, which will give you more control to create the twig template files.

And Drupal provides hooks to do this, and hook_theme_suggestions_HOOK_alter is the hook which does the purpose.

Let’s take an example to alter the template suggestions of the taxonomy term page, and with the below code you can do it.

use Drupal\taxonomy\TermInterface;
/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function mytheme_theme_suggestions_taxonomy_term_alter(&$suggestions, $variables) {
  $elements = $variables['elements'];
  if (isset($elements['#taxonomy_term']) && isset($elements['#theme']) && isset($elements['#view_mode'])) {
    $term = $elements['#taxonomy_term'];
    if ($term instanceof TermInterface) {
      $suggestions[] = $elements['#theme'] . '__' . $term->bundle() . '__' . $elements['#view_mode'];
      $suggestions[] = $elements['#theme'] . '__' . $term->id() . '__' . $elements['#view_mode'];
    }
  }
}

Once you write this code in your .theme file, and rebuild the caches. You can see the template suggestions on inspect of the page.

Similarly you can do for all the other pages or entities like User, Node, Media, Paragraph or a View.

Now reading the data to the templates will be a crucial or difficult part, in case you have not built your configuration at its best. What this means is,

  1. For example, people should make sure to use the form modes and view modes of the content type, and importantly only the needed data should be passed to the form modes or view modes.
  2. Also, if you want to render just the Image URL, make sure the field from the view mode is returning just the Image URL.
  3. Don't preprocess the data, it would be a very rare case where you need to use preprocess hooks in your .theme file.
  4. If you are using a data preprocessor, it means your application is not well planned, which means you are good to make some corrections in the application.
  5. And one must make sure not to load any data from the database on the .theme file or twig file, because you are already on the rendering layer.

To conclude, the approach or inputs provided in the article will help drupal people to understand how they can work with twig templates.

28,000OFF
LG 139 cm (55 inches) 4K Ultra HD Smart LED TV 55UR7500PSC (Dark Iron Gray)

LG 139 cm (55 inches) 4K Ultra HD Smart LED TV 55UR7500PSC (Dark Iron Gray)

43,990 71,990
Screen Size 55 Inches
Brand LG
Display Technology LED
Resolution 4K
Refresh Rate 60 Hz
Special Feature WebOS 23 with User Profiles | Filmmaker Mode, HDR 10 & HLG | Game Optimizer, ALLM, HGIG Mode | Unlimited OTT Apps | AI Brightness Control, 4K Upscaling & AI Sound (Virtual Surround 5.1 up-mix)WebOS 23 with User Profiles | Filmmaker Mode, HDR 10 & HLG | Game Optimizer, ALLM, HGIG Mode | Unlimited OTT Apps | AI Brightness Control, 4K… See more
Included Components 1 UHD 4K TV, 1 Warranty Card, 1 Remote Control, 2 Batteries, ‎‎1 Table Top Stand, 1 User Manual
Connectivity Technology Bluetooth, Wi-Fi, USB, HDMI
Aspect Ratio 16:9
Product Dimensions 23D x 123.5W x 78H Centimeters

Customers appreciate the TV's value for money and sound quality. They find the picture quality good, with realistic colors and AI-powered audio. However, some report issues with lags while using apps and loading videos. The remote control is not user-friendly and lacks bass. Opinions differ on installation ease and functionality.

Movies & TV
19,910OFF
Samsung 138 cm (55 inches) D Series Crystal 4K Vivid Ultra HD Smart LED TV UA55DUE70BKLXL (Black)

Samsung 138 cm (55 inches) D Series Crystal 4K Vivid Ultra HD Smart LED TV UA55DUE70BKLXL (Black)

44,990 64,900
Screen Size 55 Inches
Brand Samsung
Display Technology UHD
Resolution 4K
Refresh Rate 50 Hz
Special Feature Crystal Processor 4K | 4K Upscaling | UHD Dimming | Motion Xcelerator
Included Components 1 Unit TV, 1 Remote Control, 1 User Manual, 1 Power Cable
Connectivity Technology Wi-Fi, USB, Ethernet, HDMI
Aspect Ratio 16:9
Product Dimensions 6D x 123.4W x 71.1H Centimeters

Customers say: The TV has good quality, good picture quality, and is a good value for the price. However, some customers have reported issues with it not working, display issues, and mixed opinions on performance, sound quality, and color accuracy.

Movies & TV
16,500OFF
Mi 108 cm (43 inches) X Series 4K Ultra HD Smart Google TV L43M8-A2IN (Black)

Mi 108 cm (43 inches) X Series 4K Ultra HD Smart Google TV L43M8-A2IN (Black)

26,499 42,999
Screen Size 43 Inches
Brand Mi
Display Technology LED
Resolution 4K
Refresh Rate 60 Hz
Special Feature 4K Dolby Vision | Hey Google | Google TV | HDR 10 | HLG | Wide Color Gamut | Supported Applications : PatchWall, Netflix, Amazon Prime Video, Disney+ Hotstar, Miracast, Media player, Gallery, TV Manager, User Manual, Live TV, YouTube, YouTube Music, Play Store4K Dolby Vision | Hey Google | Google TV | HDR 10 | HLG | Wide Color Gamut | Supported Applications : PatchWall, Netflix, Amazon Prime Video, Disney+… See more
Included Components 1 LED TV, 2 Table Stand Base along with screws, 1 User Manual, 1 Warranty Card, 1 Remote Control, 2 AAA Battery1 LED TV, 2 Table Stand Base along with screws, 1 User Manual, 1 Warranty Card, 1 Remote Control, 2 AAA Battery
Connectivity Technology Wi-Fi, USB, Ethernet
Product Dimensions 24.1D x 95.7W x 61.2H Centimeters
Supported Internet Services Netflix, Amazon Prime Video, PatchWall, Miracast, Disney+ Hotstar

The television has good quality, good picture quality, and a good balance of picture and sound quality. Customers find it a good value for money. However, some have reported issues with the remote not working or the display switching on and off. There are also complaints about poor connectivity options and WiFi connectivity issues.

Movies & TV

Related Articles

90,000OFF
OnePlus 163 cm (65 inches) Q Series 4K Ultra HD QLED Smart Google TV 65 Q2 Pro (Black)

OnePlus 163 cm (65 inches) Q Series 4K Ultra HD QLED Smart Google TV 65 Q2 Pro (Black)

69,999 1,59,999
Screen Size 65 Inches
Brand OnePlus
Display Technology QLED
Resolution 4K
Refresh Rate 120 Hz
Special Feature QLED TV | Speak Now™ Hands-free voice control | Remote With Voice Assistant | Google TV | HDR 10+|Dolby Vision | DCI-P3 97% Wide Color Gamut | Supported Applications : Netflix, Disney+Hotstar, YoutubeQLED TV | Speak Now™ Hands-free voice control | Remote With Voice Assistant | Google TV | HDR 10+|Dolby Vision | DCI-P3 97%
Included Components 1 LED TV, 2 Table Stand Base, 1 User Manual, 1 Warranty Card, 1 Remote Control,1 AV IN Adapter, 1AC Cord, 2 AAA Battery1 LED TV, 2 Table Stand Base, 1 User Manual, 1 Warranty Card, 1 Remote Control,1 AV IN Adapter, 1AC Cord, 2 AAA Battery
Connectivity Technology Wi-Fi, USB, Ethernet, HDMI
Aspect Ratio 1.78:1
Product Dimensions 9.2D x 145W x 88.2H Centimeters

Customers appreciate the TV's sound quality and quality. They find it easy to install and like the vivid colors. However, opinions differ on picture quality, value for money, and responsiveness.

Movies & TV

Recent Articles

Recent Quick Read

Recent Great People

19,000OFF
Redmi 108 cm (43 inches) F Series 4K Ultra HD Smart LED Fire TV L43R8-FVIN (Black)

Redmi 108 cm (43 inches) F Series 4K Ultra HD Smart LED Fire TV L43R8-FVIN (Black)

23,999 42,999
Screen Size 43 Inches
Brand Redmi
Display Technology LED
Resolution 4K
Refresh Rate 60 Hz
Special Feature Fire TV Built-In | Redmi Voice Remote with Alexa | Prime Video, Netflix, Disney+ Hotstar, YouTube, Jio Cinema, Sony LIV and 12000+ apps from Fire TV Appstore | Set-top-box Live TV Integration to switch between DTH TV channels and OTT apps seamlessly by just asking Alexa | Picture-in-Picture mode | Display Mirroring – Apple Airplay 2 & Miracast | Smart Home Dashboard | 2GB RAM + 8GB Storage | Metal Bezel-less DesignFire TV Built-In | Redmi Voice Remote with Alexa | Prime Video, Netflix, Disney+ Hotstar, YouTube, Jio Cinema, Sony LIV and 12000+ apps from Fire TV Appstore | Set-top-box… See more
Included Components 1 LED TV, 1 Remote Control, 1 Manual, 2 Stands Unit, 4 Screws, 2 x AAA batteries
Connectivity Technology Wi-Fi, USB, Ethernet, HDMI
Aspect Ratio 16:9
Product Dimensions 24.1D x 95.7W x 60.7H Centimeters

The screen is really good, however viewing angles are not so good. Speakers are decent and I didn't feel any need for external speakers. Remote is amazing, I love the alexa support. Fire TV OS is also faster than android and I prefer it however it mainly shows amazon apps suggestions which I don't like much. One thing that could be improved is the 8GB storage out of which we only get 1-2GB free.

For my preferences this TV stood out and has delivered pretty good as well. For me it a value for money deal and I'm happy with the purchase so far. Let's see how long it lasts as the ultimate test for it would be of durability. If it lasts next 7-8 years it would be a complete paisa vasool product for me.

Home Appliances
8,000OFF
LG 80 cm (32 inches) HD Ready Smart LED TV 32LM563BPTC (Dark Iron Gray)

LG 80 cm (32 inches) HD Ready Smart LED TV 32LM563BPTC (Dark Iron Gray)

13,990 21,990
Screen Size 32 Inches
Brand LG
Display Technology LED
Resolution 720p
Refresh Rate 60 Hz
Special Feature Flat
Included Components 1 LED TV, 1 Table Top Stand, 1 User Manual, 1 Warranty Card, 1 Remote Control, 2 AAA Batteries
Connectivity Technology Wi-Fi
Aspect Ratio 16:9
Product Dimensions 3.3D x 20.1W x 17.4H Centimeters
  • Resolution: HD Ready (1366x768) | Refresh Rate: 60 hertz
  • Connectivity: 2 HDMI ports to connect set top box, Blu Ray players, gaming console | 1 USB ports to connect hard drives and other USB devices
  • Sound output: 10 Watts Output I 2 Speakers | DTS Virtual:X | Sound Type : Down FiringSmart
  • TV Features: Web OS Smart TV | Wi-Fi | Home Dashboard | Screen Mirroring | Mini TV Browser | Multi-Tasking | Office 365, Set WXHXD (with Stand ) mm - 739 x 472 x 168Display: Active HDR | Display Type: Flat | BackLight Module: Slim LED
Movies & TV
5,000OFF
OPPO F27 Pro+ 5G (Midnight Navy, 8GB RAM, 256GB Storage) | 6.7

OPPO F27 Pro+ 5G (Midnight Navy, 8GB RAM, 256GB Storage) | 6.7" FHD+ AMOLED Toughest 3D Curved Display|64MP AI Featured Camera|IP69 | 67W SUPERVOOC| with No Cost EMI/Additional Exchange Offers

29,999 34,999
  • DISPLAY : 17.02cm (6.7"Inch) AMOLED 3D Curved Display with Screen-to-body ratio: 93% .FHD+ Resolution with 2412×1080 Pixels , with 1Billion Colours and 120Hz Refresh Rate.
  • CAMERA : Dual Ultra-Clear Camera 64MP Main Camera + 2MP Portrait camera |8MP Front Selfie Camera| AI Portrait Retouching |AI Eraser|AI Smart Image Matting features
  • MEMORY STORAGE,SIM & PROCESSOR : 8 GB RAM|256 GB ROM | Dual 5G Sim Slot |Latest Android 14 Operating System and ColorOS 14.0 System Platform with MediaTek Dimensity 7050.
  • FAST CHARGING & BATTERY : 67W SuperVOOC charging enable full charging in 44Mins .It have 5000mAh durable long Battery .
  • TOUGHEST 3D CURVED SCREEN & PREMIUM DESIGN :All-Round Armor Impact Resistant Structure with Screen Protected by corning gorilla Glass Victus 2.Cosmos Ring design has been adopted to showcase a classic style of beauty featuring inclusiveness and orderliness. The elegant circular panel looks unique and eye-catching.Premium back leather design with stain resistance
Mobiles & Accessories
19,000OFF
Aspire Lite AMD Ryzen 3 5300U Premium Thin and Light Laptop (Windows 11 Home/8 GB RAM/512 GB SSD) AL15-41 with 39.62 cm (15.6

Aspire Lite AMD Ryzen 3 5300U Premium Thin and Light Laptop (Windows 11 Home/8 GB RAM/512 GB SSD) AL15-41 with 39.62 cm (15.6") Full HD Display, Metal Body, Steel Gray, 1.6 KG

28,990 47,990

About this item

  • Processor: AMD Ryzen 3 5300U Quad-Core Mobile Processor with AMD Radeon Graphics.Maximum Power Supply Wattage 65 W
  • Visibly Stunning: Experience sharp details and crisp colors on the 15.6" Full HD display, 16:9 aspect ratio, ultra-slim design and narrow bezels.
  • Internal Specifications: RAM - 8 GB of Dual-channel DDR4, 2 SODIMM sockets; Storage - 512 GB SSD NVMe solid-state drive storage (expandable up to 1TB) to store your files and media
  • Ports: 1 x USB 3.2 Gen 1 (Type A), 2 x USB 2.0 (Type A), 1 x USB 3.2 Gen 2 (Type C), 1 x DC-in jack
  • Keyboard: 100-/101-/104-key keyboard with independent numeric keypad, international language support
Mobiles & Accessories
We Need Your Consent
By clicking “Accept Cookies”, you agree to the storing of cookies on your device to enhance your site navigation experience.
I Accept Cookies