1

I have a content type "Blog entry" which I have overridden the template. (node--blog_entry.tpl.php)

The target is to display the node author's full name (as a link) which will take the user to a page (produced by a view) which shows all the other articles they have written.

In my template file, I have the following code:

 function bootstrap_mysite_preprocess_node(&$vars) {
   $vars['author_name'] = $vars['name'];
   $vars['submitted_dt'] = format_date($vars['node']->created, 'custom', 'jS F Y');
 }

The view that I have created which shows the author's other articles takes the UserID as an argument, so what I need is to display a link which which shows the author's full name, but passes their UserID.

the code I have in my custom content template is:

<div class="author-name">
    WRITTEN BY <a href="/user-posts/<?php print $uid; ?>"> <?php print $author_name; ?></a>
  </div>

But author name is not only the author's username (not their full name) but it is output as a link which is no good for me. I simply want to output the author's full name as plain text.

So, to summarise - What can I use instead of $vars['name']; (which producesa username that links to a profile) that will instead produce some plain text of the user's full name ?

2 Answers 2

2

Your question doesn't indicate whether you are storing the user's full name anywhere. You can add fields to the user profile (at /admin/config/people/accounts/fields). Suppose you add a firstname field (field_user_fname) and lastname field (field_user_lname), you could then write a custom function in your template file, like this:

function _get_author_name($uid) {
  $wrapper = entity_metadata_wrapper('user', $uid);
  $fname = $wrapper->field_user_fname->value();
  $lname = $wrapper->field_user_lname->value();
  return "$fname $lname";
}

And then, since you already know the UID, you could call the function from your content template:

<div class="author-name">
    WRITTEN BY <a href="/user-posts/<?php print $uid; ?>"> 
    <?php print _get_author_name($uid); ?></a>
  </div>

You could also investigate the Realname module.

0

Update the nme variable by fetching the admin/config/people/accounts/fields first_name and last_name (if existing or else add and use same machine name in below code snippet)

use Drupal\Core\Session\AnonymousUserSession;

/**
 * Implements template_preprocess_username() for username.html.twig.
 */
function MYTHEME_preprocess_username(&$variables) {
  $account = $variables['account'] ?: new AnonymousUserSession();

  $variables['extra'] = '';
  $variables['uid'] = $account->id();
  if (empty($variables['uid'])) {
    if (theme_get_setting('features.comment_user_verification')) {
      $variables['extra'] = ' (' . t('Anonymous') . ')';
    }
  }


 // that $variables['name'] is safe for printing.
 // Use machine name of Fields First Name and Last Name
 $name = $account->get('first_name')->value.' '.$account->get('last_name')->value;

 if (mb_strlen($name) > 20) {
    $name = Unicode::truncate($name, 15, FALSE, TRUE);
 }

  $variables['name'] = $name;
  // {{ username }} and {{extra}} will be available within username.html.twig.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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