You are here

function fieldblock_entity_view_alter in Field as Block 7

Same name and namespace in other branches
  1. 8 fieldblock.module \fieldblock_entity_view_alter()

Implements hook_entity_view_alter(). Stores fields attached to the current entity in a static cache, to be retrieved in fieldblock_block_view, and hides the field from the renderable array.

File

./fieldblock.module, line 188
Allow fields to be rendered in blocks.

Code

function fieldblock_entity_view_alter(&$build, $type) {

  // Do nothing if essential information is missing.
  if (!isset($build['#bundle'], $build['#entity_type'], $build['#view_mode'])) {
    return;
  }
  $entity_type = $build['#entity_type'];
  $bundle = $build['#bundle'];
  $view_mode = $build['#view_mode'];

  // Check whether the view mode uses custom display settings or the 'default'
  // mode.
  $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  $actual_mode = !empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default';
  $fieldblocks_storage =& drupal_static(FIELDBLOCK_STORAGE_STATIC);
  $variable_name = 'fieldblock-' . $entity_type . '-' . $bundle . '-' . $actual_mode;
  $fieldblock_settings = variable_get($variable_name, array());

  // Loop over the fieldblocks for this entity + bundle + view mode combination
  // and store the field's render array for later use.
  foreach ($fieldblock_settings as $field_name => $field_label) {
    if (isset($build[$field_name])) {
      $fieldblock_name = $variable_name . '-' . $field_name;
      $fieldblock_id = md5($fieldblock_name);
      $fieldblocks_storage[$fieldblock_id] = $build[$field_name];
      hide($build[$field_name]);
      $fieldblocks_storage[$fieldblock_id]['fieldblock_name'] = $fieldblock_name;
    }
  }
}