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.
- I copied the node.html.twig template into my theme
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; }
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?