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.
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.
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>
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.