123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767 |
- <?php
- class EntityDefaultUIController {
- protected $entityType;
- protected $entityInfo, $path;
- protected $id_count;
-
- 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'];
- }
-
- public function hook_menu() {
- $items = array();
-
- $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,
- );
-
- $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),
- );
-
- $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'])) {
-
- $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'])) {
-
- 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;
- }
-
- public function hook_forms() {
-
-
- $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',
- );
-
-
-
-
-
-
- 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';
-
- }
- }
- return $forms;
- }
-
- public function overviewForm($form, &$form_state) {
-
- $form['table'] = $this->overviewTable();
- $form['pager'] = array('#theme' => 'pager');
- return $form;
- }
-
- public function overviewFormValidate($form, &$form_state) {}
-
- public function overviewFormSubmit($form, &$form_state) {}
-
- public function overviewTable($conditions = array()) {
- $query = new EntityFieldQuery();
- $query->entityCondition('entity_type', $this->entityType);
-
- 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;
- }
-
- protected function overviewTableHeaders($conditions, $rows, $additional_header = array()) {
- $header = $additional_header;
- array_unshift($header, t('Label'));
- if (!empty($this->entityInfo['exportable'])) {
- $header[] = t('Status');
- }
-
- $header[] = array('data' => t('Operations'), 'colspan' => $this->operationCount());
- return $header;
- }
-
- 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;
- }
-
- 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),
- );
-
- foreach ($additional_cols as $col) {
- $row[] = $col;
- }
-
- if (!empty($this->entityInfo['exportable'])) {
- $row[] = array('data' => array(
- '#theme' => 'entity_status',
- '#status' => $entity->{$this->statusKey},
- ));
- }
-
- $field_ui = !empty($this->entityInfo['bundle of']) && entity_type_is_fieldable($this->entityInfo['bundle of']) && module_exists('field_ui');
-
- $exportable = !empty($this->entityInfo['exportable']);
-
- $i18n = !empty($this->entityInfo['i18n controller class']);
-
- 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;
- }
-
- 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;
- }
-
- public function operationFormValidate($form, &$form_state) {
- if ($form_state['op'] == 'import') {
- if ($entity = entity_import($this->entityType, $form_state['values']['import'])) {
-
- $form_state[$this->entityType] = $entity;
- if (!$form_state['values']['overwrite']) {
-
- $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.'));
- }
- }
- }
-
- 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;
- }
-
- 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':
-
- $id = entity_id($this->entityType, $entity);
- $entities = entity_load($this->entityType, array($id));
- if ($existing_entity = reset($entities)) {
-
- $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;
- }
- }
-
- public function entityFormSubmitBuildEntity($form, &$form_state) {
-
-
-
-
- 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];
- }
- }
- class EntityContentUIController extends EntityDefaultUIController {
-
- public function hook_menu() {
- $items = parent::hook_menu();
- $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
-
- unset($items[$this->path]);
- $defaults = array(
- 'file' => $this->entityInfo['admin ui']['file'],
- 'file path' => isset($this->entityInfo['admin ui']['file path']) ? $this->entityInfo['admin ui']['file path'] : drupal_get_path('module', $this->entityInfo['module']),
- );
-
- $items[$this->path . '/' . $wildcard] = array(
- 'title callback' => 'entity_ui_get_page_title',
- 'title arguments' => array('view', $this->entityType, $this->id_count),
- 'page callback' => 'entity_ui_entity_page_view',
- 'page arguments' => array($this->id_count),
- 'load arguments' => array($this->entityType),
- 'access callback' => 'entity_access',
- 'access arguments' => array('view', $this->entityType, $this->id_count),
- ) + $defaults;
- $items[$this->path . '/' . $wildcard . '/view'] = array(
- 'title' => 'View',
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'load arguments' => array($this->entityType),
- 'weight' => -10,
- ) + $defaults;
- $items[$this->path . '/' . $wildcard . '/edit'] = array(
- 'page callback' => 'entity_ui_get_form',
- 'page arguments' => array($this->entityType, $this->id_count),
- 'load arguments' => array($this->entityType),
- 'access callback' => 'entity_access',
- 'access arguments' => array('edit', $this->entityType, $this->id_count),
- 'title' => 'Edit',
- 'type' => MENU_LOCAL_TASK,
- 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
- ) + $defaults;
- $items[$this->path . '/' . $wildcard . '/delete'] = array(
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array($this->entityType . '_operation_form', $this->entityType, $this->id_count, 'delete'),
- 'load arguments' => array($this->entityType),
- 'access callback' => 'entity_access',
- 'access arguments' => array('delete', $this->entityType, $this->id_count),
- 'title' => 'Delete',
- 'type' => MENU_LOCAL_TASK,
- 'context' => MENU_CONTEXT_INLINE,
- 'file' => $this->entityInfo['admin ui']['file'],
- 'file path' => isset($this->entityInfo['admin ui']['file path']) ? $this->entityInfo['admin ui']['file path'] : drupal_get_path('module', $this->entityInfo['module']),
- ) + $defaults;
- return $items;
- }
-
- public function operationFormSubmit($form, &$form_state) {
- parent::operationFormSubmit($form, $form_state);
-
- $form_state['redirect'] = '<front>';
- }
- }
- class EntityBundleableUIController extends EntityContentUIController {
-
- public function hook_menu() {
- $items = parent::hook_menu();
-
- $items[$this->path . '/add'] = array(
- 'title callback' => 'entity_ui_get_action_title',
- 'title arguments' => array('add', $this->entityType),
- 'page callback' => 'entity_ui_bundle_add_page',
- 'page arguments' => array($this->entityType),
- 'access callback' => 'entity_access',
- 'access arguments' => array('create', $this->entityType),
- 'type' => MENU_LOCAL_ACTION,
- );
- $items[$this->path . '/add/%'] = array(
- 'title callback' => 'entity_ui_get_action_title',
- 'title arguments' => array('add', $this->entityType, $this->id_count + 1),
- 'page callback' => 'entity_ui_get_bundle_add_form',
- 'page arguments' => array($this->entityType, $this->id_count + 1),
- 'access callback' => 'entity_access',
- 'access arguments' => array('create', $this->entityType),
- );
- if (!empty($this->entityInfo['admin ui']['file'])) {
-
- foreach (array('/add', '/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;
- }
- }
- function entity_ui_overview_form($form, &$form_state, $entity_type) {
- return entity_ui_controller($entity_type)->overviewForm($form, $form_state);
- }
- function entity_ui_operation_form($form, &$form_state, $entity_type, $entity, $op) {
- $form_state['op'] = $op;
- return entity_ui_controller($entity_type)->operationForm($form, $form_state, $entity, $op);
- }
- function entity_ui_main_form_defaults($form, &$form_state, $entity = NULL, $op = NULL) {
-
-
- return entity_ui_form_defaults($form, $form_state, $form_state['entity_type'], $entity, $op);
- }
- function entity_ui_clone_entity($entity_type, $entity) {
-
- $entity = clone $entity;
- $entity_info = entity_get_info($entity_type);
- $entity->{$entity_info['entity keys']['id']} = FALSE;
- if (!empty($entity_info['entity keys']['name'])) {
- $entity->{$entity_info['entity keys']['name']} = FALSE;
- }
- $entity->is_new = TRUE;
-
- if (!empty($entity_info['exportable'])) {
- $status_key = isset($entity_info['entity keys']['status']) ? $entity_info['entity keys']['status'] : 'status';
- $entity->$status_key = ENTITY_CUSTOM;
- }
- return $entity;
- }
- function entity_ui_form_defaults($form, &$form_state, $entity_type, $entity = NULL, $op = NULL) {
- $defaults = array(
- 'entity_type' => $entity_type,
- );
- if (isset($entity)) {
- $defaults[$entity_type] = $entity;
- }
- if (isset($op)) {
- $defaults['op'] = $op;
- }
- $form_state += $defaults;
- if (isset($op)) {
- drupal_set_title(entity_ui_get_page_title($op, $entity_type, $entity), PASS_THROUGH);
- }
-
- if (isset($form_state['build_info']['base_form_id']) && $form_state['build_info']['base_form_id'] != $entity_type . '_form') {
- $form['#validate'][] = 'entity_ui_controller_form_validate';
- $form['#submit'][] = 'entity_ui_controller_form_submit';
- }
- return $form;
- }
- function entity_ui_controller_form_validate($form, &$form_state) {
-
- $base = substr($form_state['build_info']['base_form_id'], 10, -5);
- $method = $base . 'FormValidate';
- entity_ui_controller($form_state['entity_type'])->$method($form, $form_state);
- }
- function entity_ui_controller_form_submit($form, &$form_state) {
-
- $base = substr($form_state['build_info']['base_form_id'], 10, -5);
- $method = $base . 'FormSubmit';
- entity_ui_controller($form_state['entity_type'])->$method($form, $form_state);
- }
- function entity_ui_form_submit_build_entity($form, &$form_state) {
- return entity_ui_controller($form_state['entity_type'])->entityFormSubmitBuildEntity($form, $form_state);
- }
- function entity_ui_validate_machine_name($element, &$form_state) {
- if (is_numeric($element['#value'])) {
- form_error($element, t('Machine-readable names must not consist of numbers only.'));
- }
- }
- function theme_entity_ui_overview_item($variables) {
- $output = $variables['url'] ? l($variables['label'], $variables['url']['path'], $variables['url']['options']) : check_plain($variables['label']);
- if ($variables['name']) {
- $output .= ' <small> (' . t('Machine name') . ': ' . check_plain($variables['name']) . ')</small>';
- }
- return $output;
- }
|