class ClientsHandlerEntityUIController in Web Service Clients 7.3
Default controller for providing handler UI.
Expects:
- 'type' key on the entity to be the handler type.
- various entity info property documented in clients_hook_entity_info().
Hierarchy
Expanded class hierarchy of ClientsHandlerEntityUIController
File
- includes/
clients.ui.inc, line 15 - Provides a controller for building an entity overview form.
View source
class ClientsHandlerEntityUIController extends EntityDefaultUIController {
/**
* Provides definitions for implementing hook_menu().
*/
public function hook_menu() {
// Start with what EntityAPI gives us, then doctor it.
$items = parent::hook_menu();
// Change the EntityAPI to deal with multiple handler types.
$items[$this->path . '/add']['title'] = 'Add TODO';
$items[$this->path . '/add']['page callback'] = 'clients_handler_add_page';
$items[$this->path . '/add']['page arguments'] = array(
$this->entityType,
);
// Add links. Follow same pattern as node.
$handler_types_callback = $this->entityInfo['admin ui']['types callback'];
$handler_types = $handler_types_callback();
foreach ($handler_types as $type => $type_info) {
$items[$this->path . '/add/' . $type] = array(
'title' => 'Create !name',
'title arguments' => array(
'!name' => $type_info['label'],
),
'page callback' => 'clients_handler_get_add_form',
'page arguments' => array(
$this->entityType,
$type,
),
'file' => $this->entityInfo['admin ui']['file'],
// Need to specify file path as this gets used in entity_menu().
'file path' => drupal_get_path('module', $this->entityInfo['module']),
);
}
// All menu items for handlers have the same access.
// This means we don't need an access callback.
foreach ($items as $path => $item) {
$items[$path]['access callback'] = 'user_access';
$items[$path]['access arguments'] = array(
$this->entityInfo['admin ui']['access permission'],
);
}
return $items;
}
/**
* Overriden to sort the handlers by machine name.
*
* @param $conditions
* An array of conditions as needed by entity_load().
*
* @return Array
* A renderable array.
*/
public function overviewTable($conditions = array()) {
$entities = $this
->getOverviewEntities($conditions);
$rows = array();
foreach ($entities as $entity) {
$rows[] = $this
->overviewTableRow($conditions, entity_id($this->entityType, $entity), $entity);
}
$render = array(
'#theme' => 'table',
'#header' => $this
->overviewTableHeaders($conditions, $rows),
'#rows' => $rows,
'#empty' => t('None.'),
);
return $render;
}
/**
* Retrieves the entities for the admin overview.
*/
function getOverviewEntities($conditions) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', $this->entityType);
// Add all conditions to query.
foreach ($conditions as $key => $value) {
$query
->propertyCondition($key, $value);
}
// Add the sort order.
// entity_load() appears to respect the order of the incoming $ids array,
// so no need to do any sorting here.
$query
->propertyOrderBy('name');
if ($this->overviewPagerLimit) {
$query
->pager($this->overviewPagerLimit);
}
$results = $query
->execute();
$ids = isset($results[$this->entityType]) ? array_keys($results[$this->entityType]) : array();
$entities = $ids ? entity_load_multiple_by_name($this->entityType, $ids) : array();
return $entities;
}
/**
* Generates the table headers for the overview table.
*/
protected function overviewTableHeaders($conditions, $rows, $additional_header = array()) {
// Our subclasses have almost certainly added additional columns.
// Put the handler type at the front so it comes just after the name.
array_unshift($additional_header, t('Type'));
return parent::overviewTableHeaders($conditions, $rows, $additional_header);
}
/**
* Generates the row for the passed entity and may be overridden in order to
* customize the rows.
*
* @param $additional_cols
* Additional columns to be added after the entity label column.
*/
protected function overviewTableRow($conditions, $id, $entity, $additional_cols = array()) {
$handler_types_callback = $this->entityInfo['admin ui']['types callback'];
$handler_types = $handler_types_callback();
$broken_class = $this->entityInfo['factory']['broken class'];
if (get_class($entity) == $broken_class) {
// If the handler is broken, there will be no type data for it.
$type_label = $entity->type;
$type_label .= ' <span class="warning">' . t("Broken handler") . '</span>';
}
else {
$handler_type = $handler_types[$entity->type];
$type_label = $handler_type['label'];
}
// Our subclasses have almost certainly added additional columns.
// Put the handler type at the front so it comes just after the name.
array_unshift($additional_cols, $type_label);
$row = parent::overviewTableRow($conditions, $id, $entity, $additional_cols);
return $row;
}
}