I need to remove title from rendered custom entity.

unset($entity->title) is not good because i still need to use its value in this rendered entity to use in markup.

css display:none is not good idea on my point of view as well.

  • "I need to remove title from rendered custom entity." ... "because i still need to use its value in this rendered entity" - you're contradicting yourself in a couple of sentences. Can you clarify the question. – David Thomas Jan 14 '13 at 8:50
  • for e.g. i need $entity->title to use in l() with some other url. Looks like it is not possible to do it in hook_entity_view and should be done in somevere else. – drupal2000 Jan 14 '13 at 9:31
  • Where rendered title is generated firstly, in what function? – drupal2000 Jan 14 '13 at 9:34
  • It was easy. I made it in my custom entity.tpl.php. – drupal2000 Jan 14 '13 at 10:48

hook_entity_view(), and hook_entity_view_alter() are not the hooks you should use to avoid an entity title is rendered, as the entity title is not generally passed to the $build parameter passed to those hooks.

This is the content of the $build parameter for three different entity Drupal defines.

  • Node


  • Comment


  • Taxonomy term


Furthermore, the purpose of hook_entity_view() is adding content to $entity->content, not altering $entity. The description given in the documentation page for the hook is the following:

The module may add elements to $entity->content prior to rendering. The structure of $entity->content is a renderable array as expected by drupal_render().

To avoid the title is rendered for an entity, you should use a preprocess function for the template file used by the entity, and unset a variable used from the template file for the title. For example, for a node, a module could use the following code.

function mymodule_preprocess_node(&$variables) {
  $variables['title'] = '';

Similar code would work for comments, but in that case the function would be mymodule_preprocess_comment().
For taxonomy terms, there isn't a title but there is the term name, which is printed from the template file using the following code.

<?php if (!$page): ?>
  <h2><a href="<?php print $term_url; ?>"><?php print $term_name; ?></a></h2>
<?php endif; ?>

Setting $term_url, and $term_name to an empty string would cause the following HTML output to be printed.

<h2><a href=""></a></h2>
  • Thx for a lot of details, i solved my problem by coping default entity.tpl.php and commented out 'title'. – drupal2000 Jan 16 '13 at 20:23

By the comments it looks like you took care of this, but I was going to suggest using the hide() function in a template file and then print out that title variable where ever you needed it in the markup of the tpl.

  • Thx a lot, i did not know about this function. – drupal2000 Jan 16 '13 at 20:26

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.