4

In my node.tpl I have added <?php print $type; ?>. This gives me the machine name of the node type e.g. blog_entry. What do I use to get the human readable form e.g. Blog entry?

Edit (Here is my current code)

<?php if ($display_submitted): ?>
  <div class="submitted"><div class="submitted-type submitted-item"><?php print $type; ?></div><div class="submitted-author submitted-item"><?php print $name; ?></div><div class="submitted-date submitted-item"><?php print $date; ?></div><div class="submitted-comments submitted-item"><?php print $comment_count; ?></div></div>
  <?php endif; ?>

3 Answers 3

8

You can use the node_type_load() function to load up the node type object, and then extract the name:

$type_obj = node_type_load($type);
$name = $type_obj->name;

You can integrate with your current code like so:

<?php
  $type_obj = node_type_load($type);
  $human_readable_type = $type_obj->name;
?>

<?php if ($display_submitted): ?>
  <div class="submitted">
    <div class="submitted-type submitted-item">
      <?php print $human_readable_type; ?>
    </div>
  <div class="submitted-author submitted-item">
    <?php print $name; ?>
  </div>
  <div class="submitted-date submitted-item">
    <?php print $date; ?>
  </div>
  <div class="submitted-comments submitted-item">
    <?php print $comment_count; ?>
  </div>
</div>
<?php endif; ?>
7
  • I have added my current code above, Clive. Can you tell me how to restructure it using what you are suggesting? I am still hacking away trying to understand this stuff and need my hand held.
    – Hertzdonit
    Jun 18, 2012 at 14:49
  • @Hertzdonit I've updated the answer, have a go with that
    – Clive
    Jun 18, 2012 at 14:52
  • Hi Clive. It's probably best to keep this type of logic out of the templates. I have added an example in my answer.
    – Sam152
    Jun 18, 2012 at 14:55
  • @Sam152 Yep you're right...just didn't want to confuse the poor fellow ;)
    – Clive
    Jun 18, 2012 at 14:57
  • Fair enough point. I would imagine learning a framework with limited knowledge of the underlying language would be a daunting task.
    – Sam152
    Jun 18, 2012 at 14:59
4

You can look up this information using entity_get_info(). Here is a small code example of how to use this API call:

$content_type = 'blog_entry';
$entity_info = entity_get_info('node');
$human_name = $entity_info['bundles'][$content_type]['label'];

This call also returns lots of other useful information such as the fields attached to each bundle, but since it loads so much data it's probably better to use the solution Clive posted if you are going to be calling it every page load.


Expanding on Clives answer, if you would like to have the type accessible from the node template, add the following code to your template.php file inside your current themes file and ensure that you replace MY_THEME with the name of your theme:

function MY_THEME_preprocess_node(&$vars){
    $type_obj = node_type_load($vars['type']);
    $vars['human_type'] = $type_obj->name;
}

Then anywhere inside your node templates you can use:

<?php print $human_type; ?>
3

D7 I use node_type_get_name($node->type);

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.