How can I build a render array that results in a string rather than a DrupalCoreRenderMarkup object?

I’d like to use Twig Tweak’s drupal_view() to render a view in my paragraph template. I have a couple entity reference fields on the paragraph and need the IDs of the referenced entities passed as contextual filter arguments.

I built a Contextual Filter String field formatter to format the contextual filter string with the hope that it could be passed directly as the argument’s value. The formatter will return all if the reference field is empty, otherwise it will return the referenced entity IDs as a string with AND/OR separators, e.g. 1,2,3 for AND or 1+2+3 for OR.

Inside my template, I’m attempting to render the view with the following twig:

{% set arg1 = content.field_arg1|render %}
{% set arg2 = content.field_arg2|render %}

{{ drupal_view('my_view', 'my_display', arg1, arg2) }}

But this does not work!

Rather than strings, my arg variables are actually DrupalCoreRenderMarkup objects so the arguments are not passed correctly to the view.

After researching, I’ve found that I can workaround this issue by filtering my arg variables with a php filter that returns a string. For example, using the trim filter works:

{% set arg1 = content.field_arg1|render|trim %}
{% set arg2 = content.field_arg2|render|trim %}

{{ drupal_view('my_view', 'my_display', arg1, arg2) }}

If possible, I would like to build a render array that doesn’t require any fancy footwork in the template to pass the values to the drupal_view() function. I.e. a render array where I can pass content.field_arg1 or content.field_arg|render directly as the arguments. Is this possible?

Render arrays I’ve tried that did not work:

$inline_template = [
  '#type' => 'inline_template',
  '#template' => '{{ contextual_filter_string }}', // also tried with |trim filter here
  '#context' => [
    'contextual_filter_string' => '1+2+3',
  ],
];

$markup = [
  '#markup' => '1+2+3',
];

$plain_text = [
  '#plain_text' => '1+2+3',
];

Note: not shown in the above render arrays is cache metadata (tags) for the entities referenced in the field.

Yes, there are alternate ways to get these field values and pass them to drupal_view(), however that’s not what this question is about. I would like to use a Field Formatter to return a render array (I think I have to return a render array) that results in a string ready to be passed directly to drupal_view(). This allows me to configure the format of the argument in the UI without touching code. I suspect this is impossible.

Some alternative approaches that I’ve considered:

  • I could preprocess my paragraph and add these filter strings to the
    $variables array.
  • I could build my own twig function to generate these contextual filter strings.
  • I could get the values directly from the entity in my template, e.g. paragraph.field_arg1.target_id as Les Lim answered, but this doesn’t work cleanly for multi-value fields.

Edit: It seems that this is impossible so accepting Les Lim’s answer as the best alternative.

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 can I build a render array that results in a string rather than a DrupalCoreRenderMarkup object?

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.