Adding Extra Content to Nodes in Drupal 7
Have you ever wanted to add some extra content to be shown whenever a node (or other entity) is viewed in Drupal? You may already be familiar with how to accomplish such a task using hook_node_view() (as I was), but what you might not be familiar with is the very handy hook_field_extra_fields().
What's this hook do, you ask? Simple, it allows you to add "fake" fields to a fieldable entity and configure them on the "Manage Fields" page of that entity (node, comment, etc). With your content now available as fields on the entity, site administrators can now reorder your content along with any other field. What's more is that this approach works great with view modes as well so that your content can be shown differently based on how the entity is being viewed. A few things to keep in mind:
- If you are adding fields to an existing entity (meaning, you're module doesn't define the entity you're wanting to add fields to), you'll want to use hook_field_extra_fields_alter() and add your fields that way
- To get the saved weight for your field(s), use field_extra_fields_get_display() and get the weight from there. You'll have to pass in the entity_type, the bundle, and the view mode.
Hope that helps!
Tags