Khanh Hoang - Kenn
Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.
Sometimes you need to display a field that have no input, like a static text or image on each node, or a Facebook like button, or whatever that does not have an input, but you still want to display it as a field, for that you have the hook hook_field_extra_fields.
To display a pseudo field you just need two functions (these functions I added to a custom module called wk_extra_fields)
<?php /** * Implements hook_field_extra_fields(). */ function wk_extra_fields_field_extra_fields() { $extra = array(); // Just adding the psuedo field to the page node type. $extra['node']['page'] = array( 'display' => array( 'wk_foo' => array( 'label' => t('WK: Foo'), 'description' => t('A foo field.'), 'weight' => -5, ), ), ); return $extra; } /** * Implements hook_node_view(). */ function wk_extra_fields_node_view($node, $view_mode, $langcode) { // The field is showed in when using full view mode and on page node type. if ($view_mode == 'full' && $node->type == 'page') { $node->content['wk_foo'] = array( '#markup' => '<div class="wk-foo">Foo!</div>', ); } } ?>
So we use the function wk_extra_fields_field_extra_fields() to create the pseudo field for the page node type, after that we can activate it in the display tab, and choose where to show the field.
When we view the node, the function wk_extra_fields_node_view() is called and the content is displayed.