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.

[sourcecode language=”php” highlight=”13,14,15,16,17,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,
);
}
break;
default:break;
}
}

[/sourcecode]

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.