I'm looking for some help theming field groups.

Within a field group twig template, I want to be able to work with the group's child field values separately.

So, if I'm working in field-group-html-element--group-machine-name.html.twig, how do I render the values of field_one and field_two, which are set up as children of the field group?

Thanks in advance for any help!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kittysunshine created an issue. See original summary.

web247’s picture

Hello,
For working with group's child field values separately I added the names field into the $variables array.
The function that prepares variables for fieldgroup html element templates, is located into /templates/theme.inc

function template_preprocess_field_group_html_element(&$variables)

At the end of this function I added the following code:

// Get the childrens of a group element.
$children = Element::children($element);
if (!empty($children)) {
foreach ($children as $key => $child) {
$title_child = $variables['element'][$child]['#title'];
$val_child = $variables['element'][$child]['#object']->get($child)->getValue();
$variables[$child]['label'] = $title_child;
$variables[$child]['value'] = current($val_child[0]);
}
}

In this way you can render each child of a group in the field-group-html-element--group-machine-name.html.twig file by adding the machine name of the field and choose between 'label' and 'value' that you want to display . For example:

{{ field_test.label }}
{{field_test.value}}
jwkovell’s picture

Thanks, that worked for me!

If anyone else needs this, don't forget to add the needed use statement to the top of theme.inc:
use Drupal\Core\Render\Element;

jwkovell’s picture

Here's a patch based on web247's mod.

jwkovell’s picture

Here is a new patch that addresses a few issues I ran into with the old version:

  1. In some cases, a field with no value would cause the script to break
    The script now provides empty strings as fallback values.
  2. Only the first value of a multi-value field was passed to twig
    A new "field" attribute ({{field_test.field}}) is passed to the template that can be used to loop over multiple values or access other field-related methods. This also opens up the possibility of accessing referenced entites from the field scope, assuming that the method is whitelisted in Twig.
Ruslan Piskarov’s picture

Status: Active » Reviewed & tested by the community

Thank you very much @web247 and @jwkovell. Works perfectly.

Ruslan Piskarov’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
1.27 KB

Sometimes I see the following:

Notice: Undefined index: #title in template_preprocess_field_group_html_element() (line 100 of modules/contrib/field_group/templates/theme.inc).
Notice: Undefined index: #object in template_preprocess_field_group_html_element() (line 101 of modules/contrib/field_group/templates/theme.inc).

I updated jwkovell's patch #5.
Only one difference were added additional checkings.

+      if (isset($variables['element'][$child]['#title'])
+        && isset($variables['element'][$child]['#object'])
Ruslan Piskarov’s picture

Fixed incompatibility with PHP7.2.
Was "Error: Class 'Element' not found in template_preprocess_field_group_html_element() (line 95 of modules/contrib/field_group/templates/theme.inc)."

ifrik’s picture

The patch does not apply to Field Group 3.0-beta, but it does apply to the 3.0-dev from 6 Aug 2018.

Ruslan Piskarov’s picture

Yes. The patch #8 for the latest 3.0-dev.

webczarina’s picture

I'm not sure if this is the right place to ask this question, but...

My site uses Field Group 8.x-1.0, so I patched it with @jwkovell's field_group-rendering_child_field_in_field_group_twig_template-2872723-5-D8.patch. My problem is that one of my child fields is a reference to a taxonomy term, and {{field_name.value}} is producing the tid number, not the term itself. How can I fix this?

Ruslan Piskarov’s picture

@webczarina, could you debug the patch you applied?
At $variables[$child]['value'] = current($child_object->get($child)->getValue()[0]);
Maybe more correctly $variables[$child]['value'] = current($child_object->get($child)->getValue().
I can't test right now, don't have a project with an installed module.

eg2234’s picture

I've added the patch at #8 (Drupal 8.6.14, Field Group 3.x-dev), and can get {{ field_machinename.label }} and {{ field_machinename.value }} without trouble in field-group-html-element--group-machinename.html.twig. The issue comes with internal links, those get rendered as internal://path -- how can I print those as usable relative URLs?

sleitner’s picture

Version: 8.x-1.0-rc6 » 8.x-3.x-dev
Component: Miscellaneous » Code
Category: Support request » Feature request
Status: Needs review » Reviewed & tested by the community

Patch #8 works in Drupal 8.7.3 Field Group 3.0rc1 .

scottAtRoot802’s picture

I'm having a similar issue as @webczarina but with link fields instead of taxonomy. I've applied patch #8 and can get text fields to appear correctly. However, links fields only display the URI ( for example: entity:node/149 ), not the rendered markup. Any ideas how to fix this?

sleitner’s picture

Any news on this issue?

dercheffe’s picture

Would also like to change the rendering of my fields and the markup inside a field group via twig. Any news about this issue?

cweiske’s picture

cweiske’s picture

If you just need the value of a URL child element in the field group template, access it via

{{ element.field_detail_url[0]['#url'].toString() }}

The "element" variable is available in the templates, even if that is not written down in the field-group-html-element.html.twig comments.

Anybody’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Could this please be prepared as MR? Also needs tests.