class EntityDefaultUIController in Entity API 7
Default UI controller providing admin UI.
This controller suites best for managing configuration entities. For a controller suiting content entities, see EntityContentUIController.
Hierarchy
- class \EntityDefaultUIController
Expanded class hierarchy of EntityDefaultUIController
1 string reference to 'EntityDefaultUIController'
- entity_ui_controller in ./
entity.module - Determines the UI controller class for a given entity type.
File
- includes/
entity.ui.inc, line 14 - Provides a controller for building an entity overview form.
View source
class EntityDefaultUIController {
protected $entityType;
protected $entityInfo, $path;
protected $id_count;
/**
* Defines the number of entries to show per page in overview table.
*/
public $overviewPagerLimit = 25;
public function __construct($entity_type, $entity_info) {
$this->entityType = $entity_type;
$this->entityInfo = $entity_info;
$this->path = $this->entityInfo['admin ui']['path'];
$this->statusKey = empty($this->entityInfo['entity keys']['status']) ? 'status' : $this->entityInfo['entity keys']['status'];
}
/**
* Provides definitions for implementing hook_menu().
*/
public function hook_menu() {
$items = array();
// Set this on the object so classes that extend hook_menu() can use it.
$this->id_count = count(explode('/', $this->path));
$wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
$plural_label = isset($this->entityInfo['plural label']) ? $this->entityInfo['plural label'] : $this->entityInfo['label'] . 's';
$items[$this->path] = array(
'title' => $plural_label,
'page callback' => 'drupal_get_form',
'page arguments' => array(
$this->entityType . '_overview_form',
$this->entityType,
),
'description' => 'Manage ' . $plural_label . '.',
'access callback' => 'entity_access',
'access arguments' => array(
'view',
$this->entityType,
),
'file' => 'includes/entity.ui.inc',
);
$items[$this->path . '/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$this->path . '/add'] = array(
'title callback' => 'entity_ui_get_action_title',
'title arguments' => array(
'add',
$this->entityType,
),
'page callback' => 'entity_ui_get_form',
'page arguments' => array(
$this->entityType,
NULL,
'add',
),
'access callback' => 'entity_access',
'access arguments' => array(
'create',
$this->entityType,
),
'type' => MENU_LOCAL_ACTION,
);
$items[$this->path . '/manage/' . $wildcard] = array(
'title' => 'Edit',
'title callback' => 'entity_label',
'title arguments' => array(
$this->entityType,
$this->id_count + 1,
),
'page callback' => 'entity_ui_get_form',
'page arguments' => array(
$this->entityType,
$this->id_count + 1,
),
'load arguments' => array(
$this->entityType,
),
'access callback' => 'entity_access',
'access arguments' => array(
'update',
$this->entityType,
$this->id_count + 1,
),
);
$items[$this->path . '/manage/' . $wildcard . '/edit'] = array(
'title' => 'Edit',
'load arguments' => array(
$this->entityType,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
// Clone form, a special case for the edit form.
$items[$this->path . '/manage/' . $wildcard . '/clone'] = array(
'title' => 'Clone',
'page callback' => 'entity_ui_get_form',
'page arguments' => array(
$this->entityType,
$this->id_count + 1,
'clone',
),
'load arguments' => array(
$this->entityType,
),
'access callback' => 'entity_access',
'access arguments' => array(
'create',
$this->entityType,
),
);
// Menu item for operations like revert and delete.
$items[$this->path . '/manage/' . $wildcard . '/%'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
$this->entityType . '_operation_form',
$this->entityType,
$this->id_count + 1,
$this->id_count + 2,
),
'load arguments' => array(
$this->entityType,
),
'access callback' => 'entity_access',
'access arguments' => array(
'delete',
$this->entityType,
$this->id_count + 1,
),
'file' => 'includes/entity.ui.inc',
);
if (!empty($this->entityInfo['exportable'])) {
// Menu item for importing an entity.
$items[$this->path . '/import'] = array(
'title callback' => 'entity_ui_get_action_title',
'title arguments' => array(
'import',
$this->entityType,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
$this->entityType . '_operation_form',
$this->entityType,
NULL,
'import',
),
'access callback' => 'entity_access',
'access arguments' => array(
'create',
$this->entityType,
),
'file' => 'includes/entity.ui.inc',
'type' => MENU_LOCAL_ACTION,
);
}
if (!empty($this->entityInfo['admin ui']['file'])) {
// Add in the include file for the entity form.
foreach (array(
"/manage/{$wildcard}",
"/manage/{$wildcard}/clone",
'/add',
) as $path_end) {
$items[$this->path . $path_end]['file'] = $this->entityInfo['admin ui']['file'];
$items[$this->path . $path_end]['file path'] = isset($this->entityInfo['admin ui']['file path']) ? $this->entityInfo['admin ui']['file path'] : drupal_get_path('module', $this->entityInfo['module']);
}
}
return $items;
}
/**
* Provides definitions for implementing hook_forms().
*
* Use per bundle form ids if possible, such that easy per bundle alterations
* are supported too.
*
* Note that for performance reasons, this method is invoked only for forms
* which receive the entity_type as first argument. Thus any forms added must
* follow that pattern.
*
* @see entity_forms()
*/
public function hook_forms() {
// The overview and the operation form are implemented by the controller,
// the callback and validation + submit handlers just invoke the controller.
$forms[$this->entityType . '_overview_form'] = array(
'callback' => 'entity_ui_overview_form',
'wrapper_callback' => 'entity_ui_form_defaults',
);
$forms[$this->entityType . '_operation_form'] = array(
'callback' => 'entity_ui_operation_form',
'wrapper_callback' => 'entity_ui_form_defaults',
);
// The entity form (ENTITY_TYPE_form) handles editing, adding and cloning.
// For that form, the wrapper callback entity_ui_main_form_defaults() gets
// directly invoked via entity_ui_get_form().
// If there are bundles though, we use form ids that include the bundle name
// (ENTITY_TYPE_edit_BUNDLE_NAME_form) to enable per bundle alterations
// as well as alterations based upon the base form id (ENTITY_TYPE_form).
if (!(count($this->entityInfo['bundles']) == 1 && isset($this->entityInfo['bundles'][$this->entityType]))) {
foreach ($this->entityInfo['bundles'] as $bundle => $bundle_info) {
$forms[$this->entityType . '_edit_' . $bundle . '_form']['callback'] = $this->entityType . '_form';
// Again the wrapper callback is invoked by entity_ui_get_form() anyway.
}
}
return $forms;
}
/**
* Builds the entity overview form.
*/
public function overviewForm($form, &$form_state) {
// By default just show a simple overview for all entities.
$form['table'] = $this
->overviewTable();
$form['pager'] = array(
'#theme' => 'pager',
);
return $form;
}
/**
* Overview form validation callback.
*
* @param $form
* The form array of the overview form.
* @param $form_state
* The overview form state which will be used for validating.
*/
public function overviewFormValidate($form, &$form_state) {
}
/**
* Overview form submit callback.
*
* @param $form
* The form array of the overview form.
* @param $form_state
* The overview form state which will be used for submitting.
*/
public function overviewFormSubmit($form, &$form_state) {
}
/**
* Generates the render array for a overview table for arbitrary entities
* matching the given conditions.
*
* @param $conditions
* An array of conditions as needed by entity_load().
*
* @return array
* A renderable array.
*/
public function overviewTable($conditions = array()) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', $this->entityType);
// Add all conditions to query.
foreach ($conditions as $key => $value) {
$query
->propertyCondition($key, $value);
}
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($this->entityType, $ids) : array();
ksort($entities);
$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;
}
/**
* Generates the table headers for the overview table.
*/
protected function overviewTableHeaders($conditions, $rows, $additional_header = array()) {
$header = $additional_header;
array_unshift($header, t('Label'));
if (!empty($this->entityInfo['exportable'])) {
$header[] = t('Status');
}
// Add operations with the right colspan.
$header[] = array(
'data' => t('Operations'),
'colspan' => $this
->operationCount(),
);
return $header;
}
/**
* Returns the operation count for calculating colspans.
*/
protected function operationCount() {
$count = 3;
$count += !empty($this->entityInfo['bundle of']) && entity_type_is_fieldable($this->entityInfo['bundle of']) && module_exists('field_ui') ? 2 : 0;
$count += !empty($this->entityInfo['exportable']) ? 1 : 0;
$count += !empty($this->entityInfo['i18n controller class']) ? 1 : 0;
return $count;
}
/**
* 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()) {
$entity_uri = entity_uri($this->entityType, $entity);
$row[] = array(
'data' => array(
'#theme' => 'entity_ui_overview_item',
'#label' => entity_label($this->entityType, $entity),
'#name' => !empty($this->entityInfo['exportable']) ? entity_id($this->entityType, $entity) : FALSE,
'#url' => $entity_uri ? $entity_uri : FALSE,
'#entity_type' => $this->entityType,
),
);
// Add in any passed additional cols.
foreach ($additional_cols as $col) {
$row[] = $col;
}
// Add a row for the exportable status.
if (!empty($this->entityInfo['exportable'])) {
$row[] = array(
'data' => array(
'#theme' => 'entity_status',
'#status' => $entity->{$this->statusKey},
),
);
}
// In case this is a bundle, we add links to the field ui tabs.
$field_ui = !empty($this->entityInfo['bundle of']) && entity_type_is_fieldable($this->entityInfo['bundle of']) && module_exists('field_ui');
// For exportable entities we add an export link.
$exportable = !empty($this->entityInfo['exportable']);
// If i18n integration is enabled, add a link to the translate tab.
$i18n = !empty($this->entityInfo['i18n controller class']);
// Add operations depending on the status.
if (entity_has_status($this->entityType, $entity, ENTITY_FIXED)) {
$row[] = array(
'data' => l(t('clone'), $this->path . '/manage/' . $id . '/clone'),
'colspan' => $this
->operationCount(),
);
}
else {
$row[] = l(t('edit'), $this->path . '/manage/' . $id);
if ($field_ui) {
$row[] = l(t('manage fields'), $this->path . '/manage/' . $id . '/fields');
$row[] = l(t('manage display'), $this->path . '/manage/' . $id . '/display');
}
if ($i18n) {
$row[] = l(t('translate'), $this->path . '/manage/' . $id . '/translate');
}
if ($exportable) {
$row[] = l(t('clone'), $this->path . '/manage/' . $id . '/clone');
}
if (empty($this->entityInfo['exportable']) || !entity_has_status($this->entityType, $entity, ENTITY_IN_CODE)) {
$row[] = l(t('delete'), $this->path . '/manage/' . $id . '/delete', array(
'query' => drupal_get_destination(),
));
}
elseif (entity_has_status($this->entityType, $entity, ENTITY_OVERRIDDEN)) {
$row[] = l(t('revert'), $this->path . '/manage/' . $id . '/revert', array(
'query' => drupal_get_destination(),
));
}
else {
$row[] = '';
}
}
if ($exportable) {
$row[] = l(t('export'), $this->path . '/manage/' . $id . '/export');
}
return $row;
}
/**
* Builds the operation form.
*
* For the export operation a serialized string of the entity is directly
* shown in the form (no submit function needed).
*/
public function operationForm($form, &$form_state, $entity, $op) {
switch ($op) {
case 'revert':
$label = entity_label($this->entityType, $entity);
$confirm_question = t('Are you sure you want to revert the %entity %label?', array(
'%entity' => $this->entityInfo['label'],
'%label' => $label,
));
return confirm_form($form, $confirm_question, $this->path);
case 'delete':
$label = entity_label($this->entityType, $entity);
$confirm_question = t('Are you sure you want to delete the %entity %label?', array(
'%entity' => $this->entityInfo['label'],
'%label' => $label,
));
return confirm_form($form, $confirm_question, $this->path);
case 'export':
if (!empty($this->entityInfo['exportable'])) {
$export = entity_export($this->entityType, $entity);
$form['export'] = array(
'#type' => 'textarea',
'#title' => t('Export'),
'#description' => t('For importing copy the content of the text area and paste it into the import page.'),
'#rows' => 25,
'#default_value' => $export,
);
return $form;
}
case 'import':
$form['import'] = array(
'#type' => 'textarea',
'#title' => t('Import'),
'#description' => t('Paste an exported %entity_type here.', array(
'%entity_type' => $this->entityInfo['label'],
)),
'#rows' => 20,
);
$form['overwrite'] = array(
'#title' => t('Overwrite'),
'#type' => 'checkbox',
'#description' => t('If checked, any existing %entity with the same identifier will be replaced by the import.', array(
'%entity' => $this->entityInfo['label'],
)),
'#default_value' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Import'),
);
return $form;
}
drupal_not_found();
exit;
}
/**
* Operation form validation callback.
*/
public function operationFormValidate($form, &$form_state) {
if ($form_state['op'] == 'import') {
if ($entity = entity_import($this->entityType, $form_state['values']['import'])) {
// Store the successfully imported entity in $form_state.
$form_state[$this->entityType] = $entity;
if (!$form_state['values']['overwrite']) {
// Check for existing entities with the same identifier.
$id = entity_id($this->entityType, $entity);
$entities = entity_load($this->entityType, array(
$id,
));
if (!empty($entities)) {
$label = entity_label($this->entityType, $entity);
$vars = array(
'%entity' => $this->entityInfo['label'],
'%label' => $label,
);
form_set_error('import', t('Import of %entity %label failed, a %entity with the same machine name already exists. Check the overwrite option to replace it.', $vars));
}
}
}
else {
form_set_error('import', t('Import failed.'));
}
}
}
/**
* Operation form submit callback.
*/
public function operationFormSubmit($form, &$form_state) {
$msg = $this
->applyOperation($form_state['op'], $form_state[$this->entityType]);
drupal_set_message($msg);
$form_state['redirect'] = $this->path;
}
/**
* Applies an operation to the given entity.
*
* Note: the export operation is directly carried out by the operationForm()
* method.
*
* @param string $op
* The operation (revert, delete or import).
* @param $entity
* The entity to manipulate.
*
* @return string
* The status message of what has been applied.
*/
public function applyOperation($op, $entity) {
$label = entity_label($this->entityType, $entity);
$vars = array(
'%entity' => $this->entityInfo['label'],
'%label' => $label,
);
$id = entity_id($this->entityType, $entity);
$edit_link = l(t('edit'), $this->path . '/manage/' . $id . '/edit');
switch ($op) {
case 'revert':
entity_delete($this->entityType, $id);
watchdog($this->entityType, 'Reverted %entity %label to the defaults.', $vars, WATCHDOG_NOTICE, $edit_link);
return t('Reverted %entity %label to the defaults.', $vars);
case 'delete':
entity_delete($this->entityType, $id);
watchdog($this->entityType, 'Deleted %entity %label.', $vars);
return t('Deleted %entity %label.', $vars);
case 'import':
// First check if there is any existing entity with the same ID.
$id = entity_id($this->entityType, $entity);
$entities = entity_load($this->entityType, array(
$id,
));
if ($existing_entity = reset($entities)) {
// Copy DB id and remove the new indicator to overwrite the DB record.
$idkey = $this->entityInfo['entity keys']['id'];
$entity->{$idkey} = $existing_entity->{$idkey};
unset($entity->is_new);
}
entity_save($this->entityType, $entity);
watchdog($this->entityType, 'Imported %entity %label.', $vars);
return t('Imported %entity %label.', $vars);
default:
return FALSE;
}
}
/**
* Entity submit builder invoked via entity_ui_form_submit_build_entity().
*
* Extracts the form values and updates the entity.
*
* The provided implementation makes use of the helper function
* entity_form_submit_build_entity() provided by core, which already invokes
* the field API attacher for fieldable entities.
*
* @return object
* The updated entity.
*
* @see entity_ui_form_submit_build_entity()
*/
public function entityFormSubmitBuildEntity($form, &$form_state) {
// Add the bundle property to the entity if the entity type supports bundles
// and the form provides a value for the bundle key. Especially new entities
// need to have their bundle property pre-populated before we invoke
// entity_form_submit_build_entity().
if (!empty($this->entityInfo['entity keys']['bundle']) && isset($form_state['values'][$this->entityInfo['entity keys']['bundle']])) {
$form_state[$this->entityType]->{$this->entityInfo['entity keys']['bundle']} = $form_state['values'][$this->entityInfo['entity keys']['bundle']];
}
entity_form_submit_build_entity($this->entityType, $form_state[$this->entityType], $form, $form_state);
return $form_state[$this->entityType];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityDefaultUIController:: |
protected | property | ||
EntityDefaultUIController:: |
protected | property | ||
EntityDefaultUIController:: |
protected | property | ||
EntityDefaultUIController:: |
public | property | Defines the number of entries to show per page in overview table. | |
EntityDefaultUIController:: |
public | function | Applies an operation to the given entity. | |
EntityDefaultUIController:: |
public | function | Entity submit builder invoked via entity_ui_form_submit_build_entity(). | |
EntityDefaultUIController:: |
public | function | Provides definitions for implementing hook_forms(). | |
EntityDefaultUIController:: |
public | function | Provides definitions for implementing hook_menu(). | 1 |
EntityDefaultUIController:: |
protected | function | Returns the operation count for calculating colspans. | |
EntityDefaultUIController:: |
public | function | Builds the operation form. | |
EntityDefaultUIController:: |
public | function | Operation form submit callback. | 1 |
EntityDefaultUIController:: |
public | function | Operation form validation callback. | |
EntityDefaultUIController:: |
public | function | Builds the entity overview form. | |
EntityDefaultUIController:: |
public | function | Overview form submit callback. | |
EntityDefaultUIController:: |
public | function | Overview form validation callback. | |
EntityDefaultUIController:: |
public | function | Generates the render array for a overview table for arbitrary entities matching the given conditions. | |
EntityDefaultUIController:: |
protected | function | Generates the table headers for the overview table. | |
EntityDefaultUIController:: |
protected | function | Generates the row for the passed entity and may be overridden in order to customize the rows. | |
EntityDefaultUIController:: |
public | function |