You are here

function theme_bricks_inline_entity_form_table in Bricks​ 7.5

Theme function for inline_entity_form field widget.

See also

bricks_inline_field_widget_form_alter()

File

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

Code

function theme_bricks_inline_entity_form_table($variables) {
  $form = $variables['form'];
  $entity_type = $form['#entity_type'];
  $fields = $form['#table_fields'];
  $has_tabledrag = inline_entity_form_has_tabledrag($form);

  // Sort the fields by weight.
  uasort($fields, 'drupal_sort_weight');
  $header = array();
  if ($has_tabledrag) {
    $header[] = array(
      'data' => '',
      'class' => array(
        'ief-tabledrag-header',
      ),
    );
    $header[] = array(
      'data' => t('Sort order'),
      'class' => array(
        'ief-sort-order-header',
      ),
    );
  }

  // Add header columns for each field.
  $first = TRUE;
  foreach ($fields as $field_name => $field) {
    $column = array(
      'data' => $field['label'],
    );

    // The first column gets a special class.
    if ($first) {
      $column['class'] = array(
        'ief-first-column-header',
      );
      $first = FALSE;
    }
    $header[] = $column;
  }
  $header[] = t('Operations');

  // Build an array of entity rows for the table.
  $rows = array();
  foreach (element_children($form) as $key) {
    $row_classes = array(
      'ief-row-entity',
    );
    $cells = array();
    if ($has_tabledrag) {
      $indentation = theme('indentation', array(
        'size' => $form[$key]['depth']['#value'],
      ));
      $cells[] = array(
        'data' => $indentation,
        'class' => array(
          'ief-tabledrag-handle',
        ),
        'style' => 'width: auto',
      );
      $cells[] = drupal_render($form[$key]['delta']);
      $row_classes[] = 'draggable';
    }

    // Add a special class to rows that have a form underneath, to allow
    // for additional styling.
    if (!empty($form[$key]['form'])) {
      $row_classes[] = 'ief-row-entity-form';
    }

    // Add fields that represent the entity.
    foreach ($fields as $field_name => $field) {
      $data = $form[$key][$field_name];
      if (is_array($data)) {
        $data = drupal_render($data);
      }
      $cells[] = array(
        'data' => $data,
        'class' => array(
          'inline-entity-form-' . $entity_type . '-' . $field_name,
        ),
      );
    }

    // Add the buttons belonging to the "Operations" column.
    $cells[] = drupal_render($form[$key]['actions']);

    // Create the row.
    $rows[] = array(
      'data' => $cells,
      'class' => $row_classes,
    );

    // If the current entity array specifies a form, output it in the next row.
    if (!empty($form[$key]['form'])) {
      $row = array(
        array(
          'data' => drupal_render($form[$key]['form']),
          'colspan' => count($fields) + 1,
        ),
      );
      $rows[] = array(
        'data' => $row,
        'class' => array(
          'ief-row-form',
        ),
        'no_striping' => TRUE,
      );
    }
  }
  if (!empty($rows)) {
    $id = 'ief-entity-table-' . $form['#id'];
    if ($has_tabledrag) {

      // Add the tabledrag JavaScript.
      drupal_add_tabledrag($id, 'order', 'all', 'ief-entity-delta');
      drupal_add_tabledrag($id, 'depth', 'group', 'ief-entity-depth');

      // Fake call to enable indentation:
      drupal_add_tabledrag($id, 'match', 'parent', 'ief-entity-delta');
      drupal_add_js(drupal_get_path('module', 'bricks') . '/bricks.js');
    }

    // Return the themed table.
    $table_attributes = array(
      'id' => $id,
      'class' => array(
        'ief-entity-table',
      ),
    );
    return theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'sticky' => FALSE,
      'attributes' => $table_attributes,
    ));
  }
}