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,
);
}
break;
default:break;
}
}
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.



