1 field.block.inc FieldBlock::getContent()

Sets block content on block view.

Overrides Block::getContent

File

core/modules/field/field.block.inc, line 76

Class

FieldBlock
FieldBlock extends Block

Code

function getContent() {
  $settings = $this->settings;

  // Get a shortcut to the entity.
  list($entity_type, $field_name) = explode('-', $this->childDelta);
  $entity = $this->contexts[$entity_type]->data;

  // If the entity is not an EntityInterface object, try to get one.
  if (!is_a($entity, 'EntityInterface')) {
    $info = entity_get_info($entity_type);
    $id_key = $info['entity keys']['id'];

    // Unable to load an entity without an id property value.
    if (!isset($entity->{$id_key})) {
      return NULL;
    }

    $entity = $info['load hook']($entity->{$id_key});

    // Either there is a problem with this entity type definition,
    //  or the entity does not exist or is failing to load for some reason.
    if (!is_a($entity, 'EntityInterface')) {
      return NULL;
    }

    // Save the EntityInterface object for future use.
    $this->contexts[$entity_type]->setData($entity);
  }

  // Load the entity type's information for this field.
  $field = field_info_instance($entity_type, $field_name, $entity->bundle());

  // Do not render if the entity type does not have this field.
  if (empty($field)) {
    return NULL;
  }
  $language = field_language($entity_type, $entity, $field_name);

  $field_settings = array(
    'type' => $settings['formatter'],
    'settings' => $settings['formatter_settings'],
    'label' => $settings['label'],
  );

  $all_values = field_get_items($entity_type, $entity, $field_name, $language);
  if (!is_array($all_values)) {
    // Do not render if the field is empty.
    return NULL;
  }
  // Reverse values.
  if ($settings['delta_reversed']) {
    $all_values = array_reverse($all_values, TRUE);
  }

  if (isset($settings['delta_limit'])) {
    $offset = intval($settings['delta_offset']);
    $limit = !empty($settings['delta_limit']) ? $settings['delta_limit'] : NULL;
    $all_values = array_slice($all_values, $offset, $limit, TRUE);
  }

  $clone = clone $entity;
  $clone->{$field_name}[$language] = $all_values;
  $field_output = field_view_field($entity_type, $clone, $field_name, $field_settings, $language);

  return $field_output;
}