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
- product module – creates a product entity
- 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
[sourcecode language=”php” highlight=”17″]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;
}
[/sourcecode]
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.
Hi,i want to ask about $entity->content[‘product’],what does that means and where can i find tutorial about it?Thanks a lot
$entity->content[‘product’] is a form element used Drupal expects and is rendered by the Drupal’s form api. Please see the form api of drupal https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7, You can find tutorials on form api. see this link as well, Drupal Form API handbook https://drupal.org/node/37775. Hope this clears your confusion.
Thanks a lot for the detail.
I am glad I could help.
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,
);
}
break;
}
}
Thanks Andy for the awesome comment 🙂 That is nice way to alter the contact page.
I am feeling happy that you found it useful 🙂