How to show author's full name in Drupal

I found a good question at Codenone regarding author's full name in Drupal. This is a good question because it clearly shows the power of Drupal. If you notice, all contents in Drupal will show their authors somewhere in the page. An author is identified by his/her username. And there is no full name stored in the database by default. Other CMSs usually store user's full name in its database so you might think Drupal is worse. In fact, you are allowed to add additional metadata using profile module which is included in the core modules. So showing author's full name in each node is very easy.

  1. Enable profile module.
  2. Add a new required field to profile module and named it "fullname".
  3. Add below code to template.php in your theme directory.

    function phptemplate_username($object) {
     
      if ($object->uid && $object->name) {
        // Shorten the name when it is too long or it will break many tables.
        if (drupal_strlen($object->name) > 20) {
          $name = drupal_substr($object->name, 0, 15) .'...';
        }
        else {
          $name = $object->name;
        }
        if ($object->fullname) {
          if (drupal_strlen($object->fullname) > 20) {
            $name = drupal_substr($object->fullname, 0, 15) .'...';
          }
          else {
            $name = $object->fullname;
          }
        }
     
        if (user_access('access user profiles')) {
          $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
        }
        else {
          $output = check_plain($name);
        }
      }
      else if ($object->name) {
        // Sometimes modules display content composed by people who are
        // not registered members of the site (e.g. mailing list or news
        // aggregator modules). This clause enables modules to display
        // the true author of the content.
        if ($object->homepage) {
          $output = l($object->name, $object->homepage);
        }
        else {
          $output = check_plain($object->name);
        }
     
        $output .= ' ('. t('not verified') .')';
      }
      else {
        $output = variable_get('anonymous', t('Anonymous'));
      }
     
      return $output;
    }

That's all.

Tags: , ,

Reply