You are here

function _template_preprocess_inline_entity_form_entity_table in Bricks​ 7.5

Part of theme_inline_entity_form_entity_table().

1 call to _template_preprocess_inline_entity_form_entity_table()
bricks_inline_preprocess_bricks_inline_entity_form_table in bricks_inline/bricks_inline.module
Implements hook_preprocess_HOOK() for theme_bricks_inline_entity_form_table().

File

bricks_inline/bricks_inline.module, line 122
Main file for bricks_inline module.

Code

function _template_preprocess_inline_entity_form_entity_table(&$variables) {
  $form =& $variables['form'];
  $entity_type = $form['#entity_type'];
  $fields = $form['#table_fields'];

  // Sort the fields by weight.
  uasort($fields, 'drupal_sort_weight');
  foreach (element_children($form) as $key) {
    $entity =& $form[$key]['#entity'];
    list($entity_id) = entity_extract_ids($entity_type, $entity);

    // Many field formatters (such as the ones for files and images) need
    // certain data that might be missing on unsaved entities because the field
    // load hooks haven't run yet. Because of that, those hooks are invoked
    // explicitly. This is the same trick used by node_preview().
    if ($form[$key]['#needs_save']) {
      _field_invoke_multiple('load', $entity_type, array(
        $entity_id => $entity,
      ));
    }

    // Add fields that represent the entity.
    $wrapper = entity_metadata_wrapper($entity_type, $entity);
    foreach ($fields as $field_name => $field) {
      $data = '';
      if ($field['type'] == 'property') {
        $property = $wrapper->{$field_name};

        // label() returns human-readable versions of token and list properties.
        $data = $property
          ->label() ? $property
          ->label() : $property
          ->value();
        $data = empty($field['sanitized']) ? check_plain($data) : $data;
      }
      elseif ($field['type'] == 'field' && isset($entity->{$field_name})) {
        $display = array(
          'label' => 'hidden',
        ) + $field;

        // The formatter needs to be under the 'type' key.
        if (isset($display['formatter'])) {
          $display['type'] = $display['formatter'];
          unset($display['formatter']);
        }
        $renderable_data = field_view_field($entity_type, $entity, $field_name, $display);

        // The field has specified an exact delta to display.
        if (isset($field['delta'])) {
          if (!empty($renderable_data[$field['delta']])) {
            $renderable_data = $renderable_data[$field['delta']];
          }
          else {

            // The field has no value for the specified delta, show nothing.
            $renderable_data = array();
          }
        }
        $data = $renderable_data;
      }
      elseif ($field['type'] == 'callback' && isset($field['render_callback']) && is_callable($field['render_callback'])) {
        $data = call_user_func($field['render_callback'], $entity_type, $entity);
      }
      $form[$key][$field_name] = $data;
    }
  }
}