3

I am trying to customize the authored by information for all of my nodes that prints the User First and Last Name, which are custom fields on the User Profile.

  1. I copied the node.html.twig template into my theme
  2. I added the following code to load the user information in my preprocess function

    function blend_preprocess_node(&$variables) {
    
    // Load the current user.
    $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
    
    // get field data from that user
    $firstname = $user->get('field_first_name')->value;
    $lastname = $user->get('field_last_name')->value;
    
    // some default getters include
    $email = $user->get('mail')->value;
    $name = $user->get('name')->value;
    $uid= $user->get('uid')->value;
    
    }
    
  3. I added this code to node.html.twig

    {{ title_prefix }}   {% if not page %}
    <h2{{ title_attributes }}>
      <a href="{{ url }}" rel="bookmark">{{ label }}</a>
    </h2>   {% endif %}   {{ title_suffix }}
    
    {% if display_submitted %}
    
    <div class="posted">
    
        <div class="services-icon">
          {{ content.field_services }}
        </div> 
    
    
      {{ author_picture }}
    
      <div{{ author_attributes }}>
        {% trans %}Submitted by {{ firstname }} {{ lastname }} on {{ date|date("m/d/Y") }}{% endtrans %}
        {{ metadata }}
      </div>
    </div>   {% endif %}
    {# We hide the comments and links now so that we can render them later. #}   <div{{ content_attributes }}>
    {{ content|without('comment', 'links', 'field_tags', 'field_services') }}   </div>
    
    {% if content.field_tags|length and not is_front %}
    {{ content.field_tags }}   {% endif %}
    {{ content.links }}   {{ content.comment }} </article>
    

How do I make variables (mostly fields) from different entities available to print in different template files?

8

Assuming this information is from the user profile.

You could do the following

blend.theme

function blend_preprocess_node(&$variables){
    $node = $variables['node'];
    $variables['firstname'] = $node->getOwner()->field_first_name->value;
    $variables['lastname'] = $node->getOwner()->field_last_name->value;
}

From there you can use {{ firstname }} and {{lastname}} anywhere in your template.

  • Thanks for the quick reply but your solution just gave me white screen. Is there another example on how I would print information from the node Author? – kaleemclarkson Jan 18 '16 at 1:06
  • Oopss.. the $node = $variables['node']; need to be inside of the preprocess function. Sorry, I have fixed in my answer. – darol100 Jan 18 '16 at 1:13
  • You rock sir!!! For future reference. When looking to get access variables from other entities, where would I search in the D8 API for the correct functions? – kaleemclarkson Jan 18 '16 at 1:38
  • "D8 API for the correct functions"... hahaha... I do not know. I know this because I'm working on contrib theme. And I ask a similar question before so I have plenty of examples in my working theme. – darol100 Jan 18 '16 at 3:02
  • hahah thanks sir! that is awesome :) Well I just added yours to my Dropbucket. Its a cool place for Drupal snippets dropbucket.org/node/7235 – kaleemclarkson Jan 18 '16 at 3:11
0

The accepted answer didn't work for me on Drupal version 8.3.4 and more. Actually the $node = $variables['node'] didn't work so I found out the following method to make it work.

On your .theme file (in this example blend.theme)

function blend_preprocess_node(&$variables){
  $node = \Drupal::routeMatch()->getParameter('node');
  $variables['firstname'] = $node->getOwner()->field_first_name->value;
  $variables['lastname'] = $node->getOwner()->field_last_name->value;
}

This worked for me

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.