function entity_example_basic_view in Examples for Developers 7
Menu callback to display an entity.
As we load the entity for display, we're responsible for invoking a number of hooks in their proper order.
See also
Related topics
1 string reference to 'entity_example_basic_view'
- entity_example_menu in entity_example/
entity_example.module - Implements hook_menu().
File
- entity_example/
entity_example.module, line 315 - Implements the basic functionality required to create and display an entity.
Code
function entity_example_basic_view($entity, $view_mode = 'tweaky') {
// Our entity type, for convenience.
$entity_type = 'entity_example_basic';
// Start setting up the content.
$entity->content = array(
'#view_mode' => $view_mode,
);
// Build fields content - this is where the Field API really comes in to play.
// The task has very little code here because it all gets taken care of by
// field module.
// field_attach_prepare_view() lets the fields load any data they need
// before viewing.
field_attach_prepare_view($entity_type, array(
$entity->basic_id => $entity,
), $view_mode);
// We call entity_prepare_view() so it can invoke hook_entity_prepare_view()
// for us.
entity_prepare_view($entity_type, array(
$entity->basic_id => $entity,
));
// Now field_attach_view() generates the content for the fields.
$entity->content += field_attach_view($entity_type, $entity, $view_mode);
// OK, Field API done, now we can set up some of our own data.
$entity->content['created'] = array(
'#type' => 'item',
'#title' => t('Created date'),
'#markup' => format_date($entity->created),
);
$entity->content['item_description'] = array(
'#type' => 'item',
'#title' => t('Item Description'),
'#markup' => $entity->item_description,
);
// Now to invoke some hooks. We need the language code for
// hook_entity_view(), so let's get that.
global $language;
$langcode = $language->language;
// And now invoke hook_entity_view().
module_invoke_all('entity_view', $entity, $entity_type, $view_mode, $langcode);
// Now invoke hook_entity_view_alter().
drupal_alter(array(
'entity_example_basic_view',
'entity_view',
), $entity->content, $entity_type);
// And finally return the content.
return $entity->content;
}