I have a field_author with different values expert, moderator or contributor

For get this custom USER field in node.twig i using next function

function MYTHEME_preprocess_node(&$variables) {

if ($node = \Drupal::routeMatch()->getParameter('node')) {
                  $variables['author'] = $node->getOwner()->field_fname->value;

And then {{ author }} in node.twig

How to get vlue of this field? For example {{ author.0 or author.1 or author.3}}?

Thanl you for helping!


Does field_fname is a custom field added to users? if so what is the field type (select or textfield) and if it is a select how many values accept limited or unlimited values?

anyway you can try this for checkbox list Try this

function MYTHEME_preprocess_node(&$variables) {

if ($node = \Drupal::routeMatch()->getParameter('node')) {
    $variables['author'] = $node->getOwner()->get('field_fname')->getValue();


{% if author.0.target_id == "expert" %}
<h1>Hello Expert<h1>
{% endif %}
  • 1. Yes, field_fname is a custom field added to users. Field type is textfield (List) with checkboxes. You can select only one value. – Oleksandr Osheko Oct 11 '18 at 16:21
  • See the Updates on my answer – MiharbKH Oct 11 '18 at 16:26
  • Not works. As I understand the problem is here $variables['author'] = $node->getOwner()->get('field_fname')->getValue(); – Oleksandr Osheko Oct 11 '18 at 16:40
  • Not really it works perfectlly you cant test it with the field roles of user change it with $variables['author'] = $node->getOwner()->get('roles')->getValue(); you will get the first selected role. – MiharbKH Oct 11 '18 at 16:43
  • 1
    Perfect! Thank you very much! You helped me a LOT! – Oleksandr Osheko Oct 11 '18 at 17:05

You can try using variable node

Get user.name

{{ node.uid.entity.name.value }}

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.