Drupal 8 Get Node Author

Drupal 8 Get Node Author ID

Drupal 8 Get Node Author ID 1024 699 Jesandy

#1 Goals - Drupal 8 Get Node Author

Show the user custom field > in this case is field full name and also the date node is created with Drupal 8 get node author. Basically to get the user id and especially the custom user field is different than other function form in node.html.twig and it looks like this on single node:

#2 Custom Your Theme File

Open your theme file in root folder on cpanel, the file name should be YOURTHEME.theme and at the new line add this snippet

function YOURTHEMENAME_preprocess_node(&$variables) {
$node = $variables[‘node’];
$variables[‘user_field_full_name’] = $node->getOwner()->field_full_name->value;
}

  • Replace YOURTHEMENAME with your theme name
  • In this case I want to insert my user custom field Full Name (field_full_name) on node twig. Replace the field_full_name with other field you want as same as text field. The same field there have to use on the next step

#3 Node HTML Twig

Open your node html twig template, in this case the file name is node.html.twig and add this snippet anywhere you want to place

<div class=”article-meta {{ author_attributes.addClass(‘author’) }}”>
{% if display_submitted %}
{% block submitted %}
by <a href=”/user/{{ node.Owner.uid.value }}”>{{ user_field_full_name }}</a> / {{ node.created.value|format_date(‘simple’) }}
{% endblock %}
{% endif %}
</div>

  • Remove <div></div> and {% block submitted %} if you want, I did this because I need to add css style
  • The field_full_name same as the function on step #2, if you want to replace with your custom field, don’t forget to change
  • Before i forget {{ node.created.value|format_date(‘simple’) }} is to show the date content created, and I created my on date time format named is “Simple” > j F Y

BONUS - Style CSS Class

If you want to looks same like the example above, open your style.css and add this code:

.article-meta {
text-transform: uppercase;
letter-spacing: 1.6px;
font-size: .8em;
color: grey;
}

Jesandy

"Jesus Believer, Founder of beBright Event, CEO of SEOLangit.com, Digital Marketers, Brand Marketing Consultant, Game Reviewer, @zadewagaming contributors, Traveler, Movie Freaks, Pizza Lovers, SEO Specialist, Webmaster and Drupal Enthusiast"

All stories by: Jesandy

Leave a Reply

Your email address will not be published.