Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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!
Comments
Comment #2
web247 CreditAttribution: web247 commentedHello,
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:
Comment #3
jwkovell CreditAttribution: jwkovell commentedThanks, 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;
Comment #4
jwkovell CreditAttribution: jwkovell commentedHere's a patch based on web247's mod.
Comment #5
jwkovell CreditAttribution: jwkovell commentedHere is a new patch that addresses a few issues I ran into with the old version:
The script now provides empty strings as fallback values.
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.Comment #6
Ruslan PiskarovThank you very much @web247 and @jwkovell. Works perfectly.
Comment #7
Ruslan PiskarovSometimes I see the following:
I updated jwkovell's patch #5.
Only one difference were added additional checkings.
Comment #8
Ruslan PiskarovFixed 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)."
Comment #9
ifrikThe patch does not apply to Field Group 3.0-beta, but it does apply to the 3.0-dev from 6 Aug 2018.
Comment #10
Ruslan PiskarovYes. The patch #8 for the latest 3.0-dev.
Comment #11
webczarina CreditAttribution: webczarina commentedI'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?
Comment #12
Ruslan Piskarov@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.
Comment #13
eg2234 CreditAttribution: eg2234 commentedI'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?
Comment #14
sleitner CreditAttribution: sleitner commentedPatch #8 works in Drupal 8.7.3 Field Group 3.0rc1 .
Comment #15
scottAtRoot802 CreditAttribution: scottAtRoot802 commentedI'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?
Comment #16
sleitner CreditAttribution: sleitner commentedAny news on this issue?
Comment #17
dercheffeWould also like to change the rendering of my fields and the markup inside a field group via twig. Any news about this issue?
Comment #18
cweiske CreditAttribution: cweiske at Mogic GmbH commentedComment #19
cweiske CreditAttribution: cweiske at Mogic GmbH commentedIf 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 thefield-group-html-element.html.twig
comments.Comment #20
AnybodyCould this please be prepared as MR? Also needs tests.