2

I'm trying to load some of the information from the author into a node--article.html.twig. So I can provide some information like bio, social media,etc.

Similar to this...

enter image description here

I can easily get the author_name and the author_picture because these two are given by the template. I added extra field at admin/config/people/accounts/fields in this case the only extra field is field_bio

enter image description here

I try to use the include to add this user field into.

node--article.html.twig

<h4>{{ author_name }}</h4>
<p>{% include('field--user--field-bio--user.html.twig') %}</p>

This what I have on the field template - field--user--field-bio--user.html.twig

<span{{ attributes }}>
  {%- for item in items -%}
      {{ item.content }}
  {%- endfor -%}
</span>

<p>Hello</p>

The word "Hello" is been display on the node--article.html.twig; however, the item.content which is my field information is not been load. =(

I also try to dig up into the user variables by using {{ kint() }} to see if there is this information and I did not find it either.

I have seen some example with a module, which I have avoid them because this is for a contrib theme and I do not want to have any module dependency. I have done this many times with DS but I do not want to use any extra module.

So I have couple questions...

  • Should I use include for this use case or not ?
    • If I have too, what I'm doing wrong ? Why the item.content is not been load ?
    • If I should not use it in this use case, what should I do instead ?
  • You should pass the required arguments in a preprocess function – Eyal Jan 2 '16 at 20:20
  • Why not make a block view and place an instance for pages that are of Article node type? – mradcliffe Jan 2 '16 at 22:14
  • Check out in the Manage Display. Maybe this field is excluded. Otherwise, it should be there in the template as a variable. Have you tried dumping all the vars like here? drupal.org/node/1906780 – Alexei Rayu Jan 3 '16 at 9:11
2

Should I use include for this use case or not ?

This is not possible, you can't mix different templates like node and field because they have different variables.

If I have too, what I'm doing wrong ? Why the item.content is not been load ?

item.content is only loaded in field, not in node template.

If I should not use it in this use case, what should I do instead ?

You should define the user field you need in preprocess, examples for the two fields already defined:

function template_preprocess_node(&$variables) {
  $variables['author_name'] = drupal_render($variables['elements']['uid']);
  $variables['author_picture'] = user_view($node->getOwner(), 'compact');

A basic example to use this in your own hook:

function mytheme_preprocess_node(&$variables) {
  $node = $variables['node'];
  $variables['user_field1'] = $node->getOwner()->field_field1->value;

And in twig

{{ user_field1 }}
| improve this answer | |
  • Btw. You should use \Drupal::service('renderer')->renderRoot() instead of deprecated drupal_render(). – kenorb Aug 5 '17 at 10:56
  • 1
    @kenorb, this is a code example from core, so you should file an issue to change this – 4k4 Aug 5 '17 at 12:02
  • @kenorb, renderRoot() is not a replacement for drupal_render(). This would be render(). – 4k4 Aug 5 '17 at 12:15
  • Ok, good to know, as drupal_render or render just doesn't work sometimes properly, render() as well sometimes fails to render arrays, so my understanding that renderRoot() method is for final render to render everything just in case. – kenorb Aug 5 '17 at 13:01
  • @kenorb, in cases where render() doesn't work properly the best solution is most times not to render at all. In D8 site building is done 99 % with unrendered render arrays. And yes, renderRoot() is for final render, but final means, you deliver it right away, like in a response. – 4k4 Aug 5 '17 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.