You are here

function _eva_extract_entity_from_build in EVA: Entity Views Attachment 7

Extract an actual entity object from its $build array.

This is a bit more complicated than it should be, since core entities, contrib entities, and contrib entities based on EntityAPI all store their junk in different slots of the build array. See http://drupal.org/node/1170198.

Parameters

array $build: The build array as passed to hook_entity_view_alter().

string $entity_type: (optional) The entity type. Defaults to NULL.

Return value

object|bool Either the entity object, or FALSE if it cannot be found.

1 call to _eva_extract_entity_from_build()
eva_entity_view_alter in ./eva.module
Implements hook_entity_view_alter().

File

./eva.module, line 259

Code

function _eva_extract_entity_from_build($build, $entity_type = NULL) {

  // EntityAPI often sticks stuff in here.
  if (!empty($build['#entity'])) {
    return $build['#entity'];
  }
  elseif (!empty($build['#entity_type']) && !empty($build['#' . $build['#entity_type']])) {
    return $build['#' . $build['#entity_type']];
  }
  elseif ($entity_type && !empty($build['#' . $entity_type])) {
    return $build['#' . $entity_type];
  }
  elseif ($build['#entity_type'] == 'user') {
    return $build['#account'];
  }
  elseif ($build['#entity_type'] == 'taxonomy_term') {
    return $build['#term'];
  }
  return FALSE;
}