[Solved] How to use hook_entity_view in drupal7 ?

I work in a team of developers and we separately develop modules in drupal. We are assigned to create entities, and there was time once I need to alter the view which was output of some other entity module. So, I am sharing here my experience how I did that.

Lets say we have two custom entity module
1. product module – creates a product entity
2. customer module – creates a customer entity

I need to alter the output of product entity module from customer module entity. In Drupal6 it was quite easy using node_api and then adding our own content in node->content, once we verified the correct content type. In Drupal7 we do it using the hook_entity_view.

To use hook_entity_view the product module should implement the single line of code highlighted in line 17. More information on hook_entity_view

function product_page_view( $product, $view_mode='full' ) {
// Remove previously built content, if exists.
  $product->content = array();

  if ($view_mode == 'teaser') {
    $product->content['title_en'] = array(
        '#markup' => filter_xss($product->title_en),
        '#weight' => -5,

// Build fields content.
  field_attach_prepare_view(PRODUCT_ENTITY, array($product->pid => $product), $view_mode);

  entity_prepare_view(PRODUCT_ENTITY, array($product->pid => $product));

  module_invoke_all('entity_view', $product, 'product', 'full', $GLOBALS['language_content']->language);

  $product->content += field_attach_view(PRODUCT_ENTITY, $product, $view_mode);

   * Specify the theme to use and set the #element. Note that the
   * key you use to pass the entity object must match the key you
   * set in in the variables in entity_theme().
  $product->content += array(
      '#theme' => 'product',
      '#element' => $product,
      '#view_mode' => 'full',
      '#language' => NULL,

  return $product->content;

After implementating that code in product entity module. There is one more step. We now finally need to use hook_entity_view hook in customer module, which is similar to using other hooks in drupal. i.e. modulename_entity_view().

Below code is roughly a random sample code, that I picked up and altered the variables to depict product and customer relationships.

we might be using the same hook to alter the output of more than one entity module so, I am checking here the $type variable and then depending on the cases, I have used my output logic.

Here I have simply added my own content to the output of product entity module. This is done by the code from line 13-18.

function customer_entity_view( $entity, $type, $view_mode, $langcode ) {
  $t = 0;
  switch ($type) {
    case 'product':
      $productStatus = entity_get_controller(PRODUCT_ENTITY)->getProductStatus($product->pid);

      if ($productStatus == 'yes') {
        $output = drupal_get_form('customer_product_page_form', $product->pid);
        $body = drupal_render($output);

	// Alter the output of product entity module
	// Add your customer output to the output of product entity module
        $entity->content['product'] = array(
            '#prefix' => '<div id="customer_product_info">', // used by javascripts to populate sliders
            '#suffix' => '</div>',
            '#markup' => $body,
            '#weight' => -10,

If there is anything that you don’t understand. Please mention it in the comments. I will try to make them clear as far as possible.

Samundra Shrestha

I work as fullstack developer. Having spent more than 5 years in the web development technologies, I aspire to create an environment where we can share, learn and grow together.

6 thoughts on “[Solved] How to use hook_entity_view in drupal7 ?

  • November 21, 2013 at 11:20 AM

    Hi,i want to ask about $entity->content[‘product’],what does that means and where can i find tutorial about it?Thanks a lot

  • April 10, 2012 at 10:35 AM

    a great post, thanks, in my case i wanted to attach a form to the end of the contact page.
    ie after all the other cck fields that may get added to the entity.
    So i did this…

    function aat_contact_entity_view( $entity, $type, $view_mode, $langcode ) {

    switch ($type) {
    case 'node':
    if($entity->nid == variable_get('aat_contact_contact_nid',0)) {

    $output = drupal_get_form('aat_contact_am_contact');

    // create a new content field and stick the html in it
    // the field *seems* to stay at the end regardless of name or weight
    $entity->content['a_new_field'] = array(
    '#markup' => drupal_render($output),
    '#weight' => 10,

    • April 10, 2012 at 12:43 PM

      Thanks Andy for the awesome comment :) That is nice way to alter the contact page.

      I am feeling happy that you found it useful :)


Leave a Reply

Your email address will not be published. Required fields are marked *