You are here

public function EntityAPIController::buildContent in Entity API 7

Implements EntityAPIControllerInterface.

Parameters

$content: Optionally. Allows pre-populating the built content to ease overriding this method.

Overrides EntityAPIControllerInterface::buildContent

File

includes/entity.controller.inc, line 585
Provides a controller building upon the core controller but providing more features like full CRUD functionality.

Class

EntityAPIController
A controller implementing EntityAPIControllerInterface for the database.

Code

public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {

  // Remove previously built content, if exists.
  $entity->content = $content;
  $langcode = isset($langcode) ? $langcode : $GLOBALS['language_content']->language;

  // Allow modules to change the view mode.
  $context = array(
    'entity_type' => $this->entityType,
    'entity' => $entity,
    'langcode' => $langcode,
  );
  drupal_alter('entity_view_mode', $view_mode, $context);

  // Make sure the used view-mode gets stored.
  $entity->content += array(
    '#view_mode' => $view_mode,
  );

  // By default add in properties for all defined extra fields.
  if ($extra_field_controller = entity_get_extra_fields_controller($this->entityType)) {
    $wrapper = entity_metadata_wrapper($this->entityType, $entity);
    $extra = $extra_field_controller
      ->fieldExtraFields();
    $type_extra =& $extra[$this->entityType][$this->entityType]['display'];
    $bundle_extra =& $extra[$this->entityType][$wrapper
      ->getBundle()]['display'];
    foreach ($wrapper as $name => $property) {
      if (isset($type_extra[$name]) || isset($bundle_extra[$name])) {
        $this
          ->renderEntityProperty($wrapper, $name, $property, $view_mode, $langcode, $entity->content);
      }
    }
  }

  // Add in fields.
  if (!empty($this->entityInfo['fieldable'])) {

    // Perform the preparation tasks if they have not been performed yet.
    // An internal flag prevents the operation from running twice.
    $key = isset($entity->{$this->idKey}) ? $entity->{$this->idKey} : NULL;
    field_attach_prepare_view($this->entityType, array(
      $key => $entity,
    ), $view_mode);
    $entity->content += field_attach_view($this->entityType, $entity, $view_mode, $langcode);
  }

  // Invoke hook_ENTITY_view() to allow modules to add their additions.
  if (module_exists('rules')) {
    rules_invoke_all($this->entityType . '_view', $entity, $view_mode, $langcode);
  }
  else {
    module_invoke_all($this->entityType . '_view', $entity, $view_mode, $langcode);
  }
  module_invoke_all('entity_view', $entity, $this->entityType, $view_mode, $langcode);
  $build = $entity->content;
  unset($entity->content);
  return $build;
}