You are here

function entity_table in Entity Construction Kit (ECK) 7.3

Same name and namespace in other branches
  1. 7 eck.module \entity_table()
  2. 7.2 eck.module \entity_table()

Creates a table showing a group of entities.

Parameters

array $entities: the entities to create the table from

bool $select: a boolean value that will determine whether the table is a select table or a regular table

1 call to entity_table()
eck__entity__list in ./eck.entity.inc
Entity overview page callback.

File

./eck.module, line 244

Code

function entity_table($entities, $select = FALSE) {
  module_load_include('inc', 'eck', 'eck.entity');

  // This is information set up for each bundle in the hook_entity_info
  // look there for more details.
  $crud_info = NULL;
  $rows = array();
  $header = array(
    t('Name'),
    array(
      'data' => t('Operations'),
      'colspan' => '1',
    ),
  );
  $info = NULL;
  foreach ($entities as $entity) {
    $info = array();
    $entity_type = $entity
      ->entityType();
    $bundle = $entity
      ->bundle();
    $id = $entity->id;
    if ($crud_info == NULL) {
      $crud_info = get_bundle_crud_info($entity_type, $bundle);
    }
    $allowed_operations = '';
    $destination = drupal_get_destination();

    // Check that the user has permissions to edit.
    if (eck_access('update', 'entity', $entity)) {
      $edit_path = str_replace('%', $id, $crud_info['edit']['path']);
      $allowed_operations = l(t('edit'), $edit_path, array(
        'query' => $destination,
      ));
    }

    // Check that the user has permissions to delete.
    if (eck_access('delete', 'entity', $entity)) {
      $delete_path = str_replace('%', $id, $crud_info['delete']['path']);
      $allowed_operations .= ($allowed_operations ? '<br />' : '') . l(t('delete'), $delete_path, array(
        'query' => $destination,
      ));
    }
    $uri = entity_uri($entity_type, $entity);
    if (eck_access("view", "entity", $entity)) {
      $row = array(
        l(entity_label($entity_type, $entity), $uri['path'], $uri['options']),
      );
    }
    else {
      $row = array(
        entity_label($entity_type, $entity),
      );
    }
    $row[] = array(
      'data' => $allowed_operations,
    );
    $info['entity'] = $entity;
    drupal_alter("entity_{$entity_type}_{$bundle}_tr", $row, $info);
    $info['bundle'] = $bundle;
    drupal_alter("entity_{$entity_type}_tr", $row, $info);
    $info['entity_type'] = $entity_type;
    drupal_alter("entity_tr", $row, $info);
    $rows[$id] = $row;
  }
  if ($info) {
    unset($info['entity']);
    drupal_alter("entity_th", $header, $info);
    unset($info['entity_type']);
    drupal_alter("entity_{$entity_type}_th", $header, $info);
    unset($info['bundle']);
    drupal_alter("entity_{$entity_type}_{$bundle}_th", $header, $info);
  }
  if ($select) {
    if (!isset($entity_type)) {
      return array(
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $rows,
      );
    }
    else {
      return drupal_get_form("entity_table_select_{$entity_type}_{$bundle}", $entity_type, $bundle, $header, $rows);
    }
  }
  else {
    return array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    );
  }
}