1

Here is where I'm at:

I downloaded the group module, created custom fields for the group, then on the page itself I overrided it with a twig template (page--group.html.twig). The question now becomes, how do I pass down the custom fields from group (machine name of a field is "group_picture") to the twig template of page--group.html.twig?

I used kint() to see if anything was being passed down from the group, but nothing was being passed down. What am I missing here?

Top level kint() variables:

'page' => array(33)
'theme_hook_original' => string(4) "page"
→'attributes' => Drupal\Core\Template\Attribute(1)
→'title_attributes' => Drupal\Core\Template\Attribute(1)
→'content_attributes' => Drupal\Core\Template\Attribute(1)
'title_prefix' => array(0)
'title_suffix' => array(0)
'db_is_active' => boolTRUE
'is_admin' => boolTRUE
'logged_in' => boolTRUE
→'user' => Drupal\Core\Session\AccountProxy(7)
→'directory' => string(23) "themes/custom/kropotkin"
→'base_path' => string(1) "/"
→'front_page' => string(1) "/"
→'language' => Drupal\Core\Language\Language(5)
'is_front' => boolFALSE
→'#cache' => array(1)
→'#attached' => array(1)
→'navbar_top_attributes' => Drupal\Core\Template\Attribute(1)
→'navbar_attributes' => Drupal\Core\Template\Attribute(1)
→'sidebar_first_attributes' => Drupal\Core\Template\Attribute(1)
→'sidebar_second_attributes' => Drupal\Core\Template\Attribute(1)
'container_navbar' => string(1) "0"
'container' => string(9) "container"
→'theme_hook_suggestions' => array(3)
'theme_hook_suggestion' => string(4) "page"

My twig override is currently this:

{% extends "@themename/page.html.twig" %}

{% block content %}
    {{kint()}}
    BLAH BLAH
{% endblock %}

and the page.html.twig is coming straight from bootstrap_barrio's subtheme template.

Majo0od
  • 2,278
  • 10
  • 33
  • 58
  • can you eloborate by posting the twig code? – DarkBee Feb 15 '21 at 07:15
  • Are they not in the {{content}} variable? I would expect them to be as with all other entities. That is ofcourse if you have set the fields to be visible on the "manage display" page for the group type. Also, are you sure you are overriding the correct template? the group modules seems to have it's [own templates](https://git.drupalcode.org/project/group/-/tree/8.x-1.x/templates) – 2pha Feb 15 '21 at 07:23
  • @2pha it's not. I'll post the variables available from kint. – Majo0od Feb 15 '21 at 17:15
  • Also `kint(content)` returns null – Majo0od Feb 15 '21 at 17:22
  • Also according to my twig debug in inspect, it suggests using `page--group.html.twig` and one of the options for override. – Majo0od Feb 15 '21 at 17:24
  • @DarkBee added more twig code and explanation – Majo0od Feb 15 '21 at 17:28
  • Does [this](https://stackoverflow.com/questions/39120390/drupal-8-passing-custom-variables-to-theme-template) help? – DarkBee Feb 17 '21 at 13:16
  • No - is there no way for you to pass fields into your drupal theme without the need for a module? – Majo0od Feb 18 '21 at 06:25
  • Well I mean, you could create an extension and register the field as a [global](https://twig.symfony.com/doc/3.x/advanced.html#id1)? – DarkBee Feb 18 '21 at 10:32
  • Like I said.... are you sure you are editing the correct template? It depends on your theme, but the "page" template does not generally output fields. It it the entity type template that does. In the case of nodes, it would usually be the "node" template. In the case of groups, it would be one of the [group templates](https://git.drupalcode.org/project/group/-/tree/8.x-1.x/templates), these template do have a "content" variable. – 2pha Feb 18 '21 at 23:10
  • The "content" variable will output the fields how they are ordered on the entities "manage display" page. If you really need to add extra variables to a template, it is done via preprocess functions. – 2pha Feb 18 '21 at 23:13
  • @2pha Maybe I'm not understanding what is a "correct template" because the twig template I specified is being displayed on the group page. Isn't that considered the "correct template?" Or else it would not have shown up at all? – Majo0od Feb 19 '21 at 03:36
  • Ah I see now. So I created the `group.html.twig` template which goes *into* the group page section. Now it makes sense. And within there, you have all the variables available. So from what I understand, if you are on the page level (which is what my `page--group.html.twig` did) you will not have access to the variables unless you get to the **specific** section that has those details. Ok. Makes sense. How about I push this further? What if I wanted the `group.html.twig` variables available in the `page--group.html.twig` file? – Majo0od Feb 19 '21 at 03:42
  • Which FYI, is what I'm asking here from the original question. – Majo0od Feb 19 '21 at 03:43

1 Answers1

1

With Drupal core's default rendering engine, you can't access variables of group.html.twig from page--group.html.twig. You have to do it manually.

You can implement hook_preprocess_HOOK to retrieve data from field of group then pass it to template:

  • In your theme file (suppose it is your_theme.theme):
    function your_theme_preprocess_page(&$variables) {
      $group_id = \Drupal::routeMatch()->getRawParameter('group'); // get group ID from route
      if (!empty($group_id)) {
        $group = \Drupal\group\Entity\Group::load($group_id); // load group entity
        $field_text = $group->get('field_text')->value; // get field value you want, here I use a text field for example            
        $variables['group_field_text'] = $field_text; // store it in $variables, so you can access it in template
      }
    }
    
  • In your page.html.twig:
    {{ group_field_text }}
    
Kien Nguyen
  • 2,616
  • 2
  • 7
  • 24
  • Thanks for this - is there a way I can find a list of all the drupal functions that I can use within the `.theme` file? – Majo0od Feb 23 '21 at 17:00
  • @Majo0od [Here](https://api.drupal.org/api/drupal/core!core.api.php/group/hooks/8.8.x) is the list of hook functions. Note that only certain hooks can be implemented in `.theme` files. Read more about that [here](https://drupal.stackexchange.com/questions/157381/theme-hooks-vs-module-hooks) – Kien Nguyen Feb 24 '21 at 02:25