Read Data to Paragraph Template in Drupal Application

ByKarthik Kumar D Kon17th Jul 2022, 2022-12-01T08:00:00+05:30
Read
Pause
Resume
Stop
Read Data to Paragraph Template in Drupal Application

In this article we are going to see how to read the dynamic data of the node or entity or field values to the template file, which are specific to the paragraph template.

Generally while the Paragraph module is used, default template suggestions given by the paragraph module or the template suggestions provided by the hooks are used and further template design is done. Here’s the article which helps to write the template suggestions.

In few cases, there will be a need to read the actual values of the node fields or the entity data like for example media. And in some cases there will be unlimited values stored and need to read such data to paragraph template files.

In this article, I will share the possible ways of reading the data to the template file and few of them might not be the best approach but it's the decision to be taken for what type of project you are working with.

Let's see now the possible ways to read the data to the paragraph template file.

  • To read the Url of the current node, just you can use the below code, in the paragraph.html.twig file or in display of paragraph–<display>.html.twig

{{ url("<current>") }}

  • To read the Title of the node, you can use the preprocess hooks like theme_preprocess_paragraph and read the node title and store it into a variable. And the variable data can be printed in the paragraph.html.twig file or in the display of paragraph–<display>.html.twig.

function bartik_preprocess_paragraph(&$variables) {
   $node = Drupal::request()->attributes->get("node");
   $variables["node_title"] = $node->getTitle();
}

{{ node_title }}

  • To read the field value (for example text)

paragraph.field_text.entity.name.value

  • To read the Image (created via Media type or with just Image type), you can use the below different types.

{% if paragraph.field_media_image is not empty %}
  {{ file_url(paragraph.field_media_image.entity.field_media_image.entity.fileuri) }}
{% endif %}

{{ file_url(paragraph.field_just_image.entity.uri.value) }}

  • If you want to read the Image (created via Media type) values in a custom approach or write some preprocess, then below code will help you. You can write the below code in the theme_preprocess_paragraph or other preprocess functions, then store into a variable and Render the stored variable in the paragraph template.

$paragraph = $term->get("field_custom_paragraph")->referencedEntities();
if ($paragraph) {
  $paragraph = reset($paragraph);
  $field_media_image = $paragraph>get("field_media_image");
  if ($field_media_image[0] && $field_media_image[0]->entity) {
    $build = Drupal::entityTypeManager()->getViewBuilder("media")->view($field_media_image[0]->entity);
    $variables["content"]["field_media_image"] = $build;
  }
}

{{ content.field_media_image }}

These are few possible ways to render read the data to the paragraph templates.

Ideally the best approach is not to tweak the data or reading of the data at the rendering layer, because this will have some impact on the performance. I suggest to not tweak the data in the preprocess or reload the same paragraph value in the preprocess, And suggest to only use, if the case you don’t have any other possibility during the development process of drupal application.

Hope this article was helpful, read this article on how to work with paragraphs and Thanks for reading this article.

26,991OFF
Hisense 139 cm (55 inches) Bezelless Series 4K Ultra HD Smart LED Google TV 55A6H (Black)

Hisense 139 cm (55 inches) Bezelless Series 4K Ultra HD Smart LED Google TV 55A6H (Black)

32,999 59,990
Resolution : 4K Ultra HD (3840x2160) | Refresh Rate : 60 Hertz | 178 Degree wide viewing angle. Connectivity: 3 HDMI ports to connect set top box, Blu Ray players, gaming console | 2 USB ports to connect hard drives and other USB devices | Dual-band Wi-Fi | Bluetooth 5.1. Sound : 24 Watts Output | Dolby Atmos | Dolby Digital. Smart TV features: Google TV | Watchlist | Google Assistant | Far Field Vioce Control | Chromecast, Miracast, DLNA | Auto Low Latency Mode for VRR | Supported Apps : Netflix, Youtube, Prime Video, Hotstar, SonyLiv, Hungama, JioCinema, Zee5, Eros Now. Display :10 bit Panel | Bezel-less Floating Display Design | ALLM | Decoding of Dolby Vision, HDR10, HLG | 1 Billion Colours | MEMC.
Televisions
350OFF
Amazon Brand - Solimo Slim Stainless Steel Water Bottle, Set of 3, 1 L Each

Amazon Brand - Solimo Slim Stainless Steel Water Bottle, Set of 3, 1 L Each

649 999
Stainless Steel water bottle set for home and office use. Made using high quality stainless steel for added durability and long life. Made from 100% food grade materials to be safe for everyday usage. Features a spill-proof design; has a snug fitting lid for easy usage and prevention of leaks. Ergonomic design for comfortable grip and rust free body. Ideal for storing water, iced tea, juices, shakes and more. Dimensions: L x W x H: 7.3 x 7.3 x 26 cm. Package Contents:- Solimo Stainless Steel Water Bottle Set of 3, Weight: 200 gm each, Colour: Silver, Capacity: 1 litre each.
Kitchen & Dining
1,272OFF
Bajaj Rex 500W Mixer Grinder, Purple

Bajaj Rex 500W Mixer Grinder, Purple

2,278 3,550
Wattage : 500 W, Voltage: 230V~50 Hz: Revolution: 20,000 RPM. Powerful 500W Titan Motor with Radio Knob: Easy selection of Speed. 2-in-1 Blade: The dry grinding jar can be used for dry and chutney grinding both. Product warranty by Bajaj - 2 Years. T&C Applied. Pulse Mode: Used for mixing and grinding without stopping your mixie. Perfect to mince chicken , get bread crumps, small puree and making lump free gravies.
Kitchen Appliances

Related Articles

32,991OFF
Hisense 139 cm (55 inches) Tornado 2.0 Series 4K Ultra HD Smart LED Google TV 55A7H (Silver)

Hisense 139 cm (55 inches) Tornado 2.0 Series 4K Ultra HD Smart LED Google TV 55A7H (Silver)

36,999 69,990
Resolution : 4K Ultra HD (3840x2160) | Refresh Rate : 60 Hertz | 178 Degree wide viewing angle. Connectivity: 3 HDMI ports to connect set top box, Blu Ray players, gaming console (HDMI 1 eARC supported) | 2 USB ports to connect hard drives and other USB devices | Dual-band Wi-Fi | Bluetooth 5.1. Sound : 102 Watts Output | JBL 6 Speakers System | Dolby Atmos for remarkable sound quality. Smart TV features: Google TV with Watchlist | Google Play Store | Google Assistant | Chromecast, Miracast, DLNA, Airplay | Auto Low Latency Mode for VRR | Supported Apps : Netflix, Youtube, Prime Video, Hotstar, SonyLiv, Hungama, JioCinema, Zee5, Eros Now. Display :10 bit Panel | Bezel-less Floating Display Design | ALLM | Decoding of Dolby Vision, HDR10, HLG | 1 Billion Colours | MEMC. Warranty Information: 2 Year Comprehensive Warranty on product provided by Hisense from date of purchase.. Installation: For requesting installation/wall mounting/demo of this product once delivered, please directly call Hisense Support for assistance (Please visit Hisense Website for Toll Free Numbers) and provide product's model name and seller's details mentioned on your invoice. The service center will allot you a convenient slot for the service. Easy Returns: This product is eligible for replacement within 10 days of delivery in case of any product defects, damage or features not matching the description.
Televisions

Recent Articles

Recent Quick Read

Recent Great People

1,059OFF
Butterfly Abs Hippo Tabletop Wet Grinder 2 Litre, 150 W, 230 V, Ac 50Hz, Cherry Red

Butterfly Abs Hippo Tabletop Wet Grinder 2 Litre, 150 W, 230 V, Ac 50Hz, Cherry Red

3,998 5,057
Wattage: 150 W; Voltage: 220-240V, 50-60Hz ; Revolution: 1300. No of Jars: 1; Jar Size: 2 Liters; Jar Material: Stainless Steel; Body Material: ABS body; Cord Material: 180 CM; Speed Control: 1 Speed Control. Read Manual Before Use. Powerful motor; Product Dimensions: 47.5 x 30.5 x 28 cm; Uniform grinding pressure and 4 Way grinding system. Shock proof ABS body, Feature: Detachable Stainless Steel drum & roller stones. Sturdy single phase high torque motor; Unbreakable and transparent lids made of polycarbonate material; Includes: Main Unit, 2 stones, lid, Warranty card with user manual.
Kitchen Appliances
802OFF
Bajaj RCX 1.8 DLX Rice Cooker, 1.8 Litre , White

Bajaj RCX 1.8 DLX Rice Cooker, 1.8 Litre , White

1,898 2,700
1.8-Litre capacity; 1Kg raw rice cooking capacity. The Cooking Bowl is Aluminum with anodized finish. Automatic cooking. Keep Warm : No. Warranty: 2 years on product. Power: 700 watts; Operating voltage: 230 volts.
Kitchen Appliances
4,171OFF
Amazon Brand - Solimo Xxxl Bean Bag Filled With Beans (Black And Brown)(Faux Leather)

Amazon Brand - Solimo Xxxl Bean Bag Filled With Beans (Black And Brown)(Faux Leather)

1,829 6,000
Dimensions: 71D x 71W x 124H Centimeters; Weight: 3600 Grams. Beans Required: No. Pattern: Faux Leather| Style: With Beans. Special Features: Velcro and zipper offer double protection to prevent beans from spilling out. Warranty: 1 year on manufacturing defects | Specific use: Bedroom, Lounge. Assembly Instruction: No assembly required.
Furniture
601OFF
amazon basics Vacuum Compression Storage Bags with Hand Pump - Medium, 5-Pack, Plastic

amazon basics Vacuum Compression Storage Bags with Hand Pump - Medium, 5-Pack, Plastic

599 1,200
Set of heavy-duty plastic vacuum compression storage bags. Compress bulky textiles, clothing, linens, and more at an 80% size reduction. Protects items from moisture, dirt, insects, mold, odors, and more. Hand pump included, or connect to any standard vacuum cleaner hose for faster results. Heavy-duty valve and double seal zipper for air tight protection. 5-pack of Medium bags.
Household
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