Using hook_field_extra_fields to add extra fields to the display of an entity.

Not a lot of time to document this, tight schedule today, but I wanted to make sure this code didn't slip through the cracks.

<?php
/**
 * Implements hook_field_extra_fields().
 *
 * Adds an extra display field for the Created Date of a message.
 */
function examplemodule_field_extra_fields() {
  $message_entity_type = entity_get_info('message');
  foreach ($message_entity_type['bundles'] as $bundlename => $bundle) {
    $fields['message'][$bundlename]['display'] = array(
      'created_date' => array(
        'label' => t('Created Date'),
        'description' => t('The date/time when this Message was created.'),
        'weight' => 0,
      ),
    );
  }
  return $fields;
}
 
/**
 * Implements hook_entity_view().
 *
 * Renders the extra display field, Created Date.
 */
function examplemodule_entity_view($entity, $type, $view_mode, $langcode) {
  if ($type == 'message' && $view_mode == 'message_log') {
    $entity->content['created_date'] = array(
      '#markup' => date('d M Y h:i a', $entity->timestamp),
      '#weight' => -1,
      '#prefix' => '<div class="date">',
      '#suffix' => '</div>',
    );
    $entity->content['#attached']['css'] = array(drupal_get_path('module', 'examplemodule') . '/theme/examplemodule.css');
  }
 
}

Tags

Article Type

General