How do I render a field template?

I have problems with rendering twig templates inside my block theme. Is it possible to display default template for a field that I’ve fetched with entity manage query?

I’ve got a custom carousel slider module which uses ‘Banner’ node type. It has a banner image, link URI fields. Everything works fine, but I would like to display the responsive image template instead of building my own HTML.

Here’s how I do it right now.

<div id="Carousel" class="glide">     <div class="glide__arrows">         <span class="glide__arrow prev" data-glide-dir="<">&lt;</span>         <span class="glide__arrow next" data-glide-dir=">">&gt;</span>     </div>      <div class="glide__wrapper">         <ul class="glide__track">             {% for item in baners %}                 <li class="glide__slide">                     <div class="box">                         <img src="{{ file_url(item.field_baner.0.entity.uri.value)}}" alt="{{ item.field_baner.alt}}" />                         <div class="box2">                         {% if item.field_url.uri %}                             <h2>                                 <a href="{{ item.field_url.uri }}">                                     {% if (item.field_description.value) %}                                         {{ item.field_description.value }}                                     {% endif %}                                     {% if (item.field_url.title) %}                                         <br /><span class="goto">{{ item.field_url.title }} &gt;</span>                                     {% endif %}                                 </a>                             </h2>                         {% endif %}                         </div>                     </div>                 </li>             {% endfor %}         </ul>     </div>     <div class="glide__bullets"></div> </div> 

I am fetching banners this way.

$storage = Drupal::entityManager()->getStorage('node'); $nids = $storage->getQuery()     ->condition('type', 'baner')     ->condition('status', 1)     ->execute(); return $storage->loadMultiple($nids); 

What I would like is to find a way to display a responsive image, such as in the following way.

{{ responsive_image(item.field_baner) }} 

Or render them in module class so I could display them in my template.

$banerBuildParams = [       '#theme' => 'responsive_image',       '#width' => $variables['width'],       '#height' => $variables['height'],       '#responsive_image_style_id' => $variables['responsive_image_style_id'],       '#uri' => $variables['uri'],   ];   //render template   $template = $this->getTemplate($banerBuildParams); 

[edit]

I think I’ve found a solution to render all the view I can use this code to pretender nodes (not sure it’s the best approach to the problem):

node_view($baner, 'teaser');  

The problem I’ve still got is that I use external jquery library which requires some specific html formatting which not comply with the default generated tags. Maybe somebody more experienced with Drupal could suggest something? I know I can create my own plugin for field type. It seems a lot trouble for just one not reusable element. What if I need to group two fields in specific DIV and some others in different one? Using node_view I am loosing the way my code looks like:/

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How do I render a field template?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.