Projet

Général

Profil

Paste
Télécharger (28,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entity / includes / entity.ui.inc @ 3aa14731

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides a controller for building an entity overview form.
6
 */
7

    
8
/**
9
 * Default UI controller providing admin UI.
10
 *
11
 * This controller suites best for managing configuration entities.
12
 * For a controller suiting content entities, see EntityContentUIController.
13
 */
14
class EntityDefaultUIController {
15

    
16
  protected $entityType;
17
  protected $entityInfo, $path;
18
  protected $id_count;
19

    
20
  /**
21
   * Defines the number of entries to show per page in overview table.
22
   */
23
  public $overviewPagerLimit = 25;
24

    
25
  public function __construct($entity_type, $entity_info) {
26
    $this->entityType = $entity_type;
27
    $this->entityInfo = $entity_info;
28
    $this->path = $this->entityInfo['admin ui']['path'];
29
    $this->statusKey = empty($this->entityInfo['entity keys']['status']) ? 'status' : $this->entityInfo['entity keys']['status'];
30
  }
31

    
32
  /**
33
   * Provides definitions for implementing hook_menu().
34
   */
35
  public function hook_menu() {
36
    $items = array();
37
    // Set this on the object so classes that extend hook_menu() can use it.
38
    $this->id_count = count(explode('/', $this->path));
39
    $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
40
    $plural_label = isset($this->entityInfo['plural label']) ? $this->entityInfo['plural label'] : $this->entityInfo['label'] . 's';
41

    
42
    $items[$this->path] = array(
43
      'title' => $plural_label,
44
      'page callback' => 'drupal_get_form',
45
      'page arguments' => array($this->entityType . '_overview_form', $this->entityType),
46
      'description' => 'Manage ' . $plural_label . '.',
47
      'access callback' => 'entity_access',
48
      'access arguments' => array('view', $this->entityType),
49
      'file' => 'includes/entity.ui.inc',
50
    );
51
    $items[$this->path . '/list'] = array(
52
      'title' => 'List',
53
      'type' => MENU_DEFAULT_LOCAL_TASK,
54
      'weight' => -10,
55
    );
56
    $items[$this->path . '/add'] = array(
57
      'title callback' => 'entity_ui_get_action_title',
58
      'title arguments' => array('add', $this->entityType),
59
      'page callback' => 'entity_ui_get_form',
60
      'page arguments' => array($this->entityType, NULL, 'add'),
61
      'access callback' => 'entity_access',
62
      'access arguments' => array('create', $this->entityType),
63
      'type' => MENU_LOCAL_ACTION,
64
    );
65
    $items[$this->path . '/manage/' . $wildcard] = array(
66
      'title' => 'Edit',
67
      'title callback' => 'entity_label',
68
      'title arguments' => array($this->entityType, $this->id_count + 1),
69
      'page callback' => 'entity_ui_get_form',
70
      'page arguments' => array($this->entityType, $this->id_count + 1),
71
      'load arguments' => array($this->entityType),
72
      'access callback' => 'entity_access',
73
      'access arguments' => array('update', $this->entityType, $this->id_count + 1),
74
    );
75
    $items[$this->path . '/manage/' . $wildcard . '/edit'] = array(
76
      'title' => 'Edit',
77
      'load arguments' => array($this->entityType),
78
      'type' => MENU_DEFAULT_LOCAL_TASK,
79
    );
80

    
81
    // Clone form, a special case for the edit form.
82
    $items[$this->path . '/manage/' . $wildcard . '/clone'] = array(
83
      'title' => 'Clone',
84
      'page callback' => 'entity_ui_get_form',
85
      'page arguments' => array($this->entityType, $this->id_count + 1, 'clone'),
86
      'load arguments' => array($this->entityType),
87
      'access callback' => 'entity_access',
88
      'access arguments' => array('create', $this->entityType),
89
    );
90
    // Menu item for operations like revert and delete.
91
    $items[$this->path . '/manage/' . $wildcard . '/%'] = array(
92
      'page callback' => 'drupal_get_form',
93
      'page arguments' => array($this->entityType . '_operation_form', $this->entityType, $this->id_count + 1, $this->id_count + 2),
94
      'load arguments' => array($this->entityType),
95
      'access callback' => 'entity_access',
96
      'access arguments' => array('delete', $this->entityType, $this->id_count + 1),
97
      'file' => 'includes/entity.ui.inc',
98
    );
99

    
100
    if (!empty($this->entityInfo['exportable'])) {
101
      // Menu item for importing an entity.
102
      $items[$this->path . '/import'] = array(
103
        'title callback' => 'entity_ui_get_action_title',
104
        'title arguments' => array('import', $this->entityType),
105
        'page callback' => 'drupal_get_form',
106
        'page arguments' => array($this->entityType . '_operation_form', $this->entityType, NULL, 'import'),
107
        'access callback' => 'entity_access',
108
        'access arguments' => array('create', $this->entityType),
109
        'file' => 'includes/entity.ui.inc',
110
        'type' => MENU_LOCAL_ACTION,
111
      );
112
    }
113

    
114
    if (!empty($this->entityInfo['admin ui']['file'])) {
115
      // Add in the include file for the entity form.
116
      foreach (array("/manage/$wildcard", "/manage/$wildcard/clone", '/add') as $path_end) {
117
        $items[$this->path . $path_end]['file'] = $this->entityInfo['admin ui']['file'];
118
        $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']);
119
      }
120
    }
121
    return $items;
122
  }
123

    
124
  /**
125
   * Provides definitions for implementing hook_forms().
126
   *
127
   * Use per bundle form ids if possible, such that easy per bundle alterations
128
   * are supported too.
129
   *
130
   * Note that for performance reasons, this method is invoked only for forms
131
   * which receive the entity_type as first argument. Thus any forms added must
132
   * follow that pattern.
133
   *
134
   * @see entity_forms()
135
   */
136
  public function hook_forms() {
137
    // The overview and the operation form are implemented by the controller,
138
    // the callback and validation + submit handlers just invoke the controller.
139
    $forms[$this->entityType . '_overview_form'] = array(
140
      'callback' => 'entity_ui_overview_form',
141
      'wrapper_callback' => 'entity_ui_form_defaults',
142
    );
143
    $forms[$this->entityType . '_operation_form'] = array(
144
      'callback' => 'entity_ui_operation_form',
145
      'wrapper_callback' => 'entity_ui_form_defaults',
146
    );
147

    
148
    // The entity form (ENTITY_TYPE_form) handles editing, adding and cloning.
149
    // For that form, the wrapper callback entity_ui_main_form_defaults() gets
150
    // directly invoked via entity_ui_get_form().
151
    // If there are bundles though, we use form ids that include the bundle name
152
    // (ENTITY_TYPE_edit_BUNDLE_NAME_form) to enable per bundle alterations
153
    // as well as alterations based upon the base form id (ENTITY_TYPE_form).
154
    if (!(count($this->entityInfo['bundles']) == 1 && isset($this->entityInfo['bundles'][$this->entityType]))) {
155
      foreach ($this->entityInfo['bundles'] as $bundle => $bundle_info) {
156
        $forms[$this->entityType . '_edit_' . $bundle . '_form']['callback'] = $this->entityType . '_form';
157
        // Again the wrapper callback is invoked by entity_ui_get_form() anyway.
158
      }
159
    }
160
    return $forms;
161
  }
162

    
163
  /**
164
   * Builds the entity overview form.
165
   */
166
  public function overviewForm($form, &$form_state) {
167
    // By default just show a simple overview for all entities.
168
    $form['table'] = $this->overviewTable();
169
    $form['pager'] = array('#theme' => 'pager');
170
    return $form;
171
  }
172

    
173
  /**
174
   * Overview form validation callback.
175
   *
176
   * @param $form
177
   *   The form array of the overview form.
178
   * @param $form_state
179
   *   The overview form state which will be used for validating.
180
   */
181
  public function overviewFormValidate($form, &$form_state) {}
182

    
183
  /**
184
   * Overview form submit callback.
185
   *
186
   * @param $form
187
   *   The form array of the overview form.
188
   * @param $form_state
189
   *   The overview form state which will be used for submitting.
190
   */
191
  public function overviewFormSubmit($form, &$form_state) {}
192

    
193

    
194
  /**
195
   * Generates the render array for a overview table for arbitrary entities
196
   * matching the given conditions.
197
   *
198
   * @param $conditions
199
   *   An array of conditions as needed by entity_load().
200

    
201
   * @return Array
202
   *   A renderable array.
203
   */
204
  public function overviewTable($conditions = array()) {
205

    
206
    $query = new EntityFieldQuery();
207
    $query->entityCondition('entity_type', $this->entityType);
208

    
209
    // Add all conditions to query.
210
    foreach ($conditions as $key => $value) {
211
      $query->propertyCondition($key, $value);
212
    }
213

    
214
    if ($this->overviewPagerLimit) {
215
      $query->pager($this->overviewPagerLimit);
216
    }
217

    
218
    $results = $query->execute();
219

    
220
    $ids = isset($results[$this->entityType]) ? array_keys($results[$this->entityType]) : array();
221
    $entities = $ids ? entity_load($this->entityType, $ids) : array();
222
    ksort($entities);
223

    
224
    $rows = array();
225
    foreach ($entities as $entity) {
226
      $rows[] = $this->overviewTableRow($conditions, entity_id($this->entityType, $entity), $entity);
227
    }
228

    
229
    $render = array(
230
      '#theme' => 'table',
231
      '#header' => $this->overviewTableHeaders($conditions, $rows),
232
      '#rows' => $rows,
233
      '#empty' => t('None.'),
234
    );
235
    return $render;
236
  }
237

    
238
  /**
239
   * Generates the table headers for the overview table.
240
   */
241
  protected function overviewTableHeaders($conditions, $rows, $additional_header = array()) {
242
    $header = $additional_header;
243
    array_unshift($header, t('Label'));
244
    if (!empty($this->entityInfo['exportable'])) {
245
      $header[] = t('Status');
246
    }
247
    // Add operations with the right colspan.
248
    $header[] = array('data' => t('Operations'), 'colspan' => $this->operationCount());
249
    return $header;
250
  }
251

    
252
  /**
253
   * Returns the operation count for calculating colspans.
254
   */
255
  protected function operationCount() {
256
    $count = 3;
257
    $count += !empty($this->entityInfo['bundle of']) && entity_type_is_fieldable($this->entityInfo['bundle of']) && module_exists('field_ui') ? 2 : 0;
258
    $count += !empty($this->entityInfo['exportable']) ? 1 : 0;
259
    $count += !empty($this->entityInfo['i18n controller class']) ? 1 : 0;
260
    return $count;
261
  }
262

    
263
  /**
264
   * Generates the row for the passed entity and may be overridden in order to
265
   * customize the rows.
266
   *
267
   * @param $additional_cols
268
   *   Additional columns to be added after the entity label column.
269
   */
270
  protected function overviewTableRow($conditions, $id, $entity, $additional_cols = array()) {
271
    $entity_uri = entity_uri($this->entityType, $entity);
272

    
273
    $row[] = array('data' => array(
274
      '#theme' => 'entity_ui_overview_item',
275
      '#label' => entity_label($this->entityType, $entity),
276
      '#name' => !empty($this->entityInfo['exportable']) ? entity_id($this->entityType, $entity) : FALSE,
277
      '#url' => $entity_uri ? $entity_uri : FALSE,
278
      '#entity_type' => $this->entityType),
279
    );
280

    
281
    // Add in any passed additional cols.
282
    foreach ($additional_cols as $col) {
283
      $row[] = $col;
284
    }
285

    
286
    // Add a row for the exportable status.
287
    if (!empty($this->entityInfo['exportable'])) {
288
      $row[] = array('data' => array(
289
        '#theme' => 'entity_status',
290
        '#status' => $entity->{$this->statusKey},
291
      ));
292
    }
293
    // In case this is a bundle, we add links to the field ui tabs.
294
    $field_ui = !empty($this->entityInfo['bundle of']) && entity_type_is_fieldable($this->entityInfo['bundle of']) && module_exists('field_ui');
295
    // For exportable entities we add an export link.
296
    $exportable = !empty($this->entityInfo['exportable']);
297
    // If i18n integration is enabled, add a link to the translate tab.
298
    $i18n = !empty($this->entityInfo['i18n controller class']);
299

    
300
    // Add operations depending on the status.
301
    if (entity_has_status($this->entityType, $entity, ENTITY_FIXED)) {
302
      $row[] = array('data' => l(t('clone'), $this->path . '/manage/' . $id . '/clone'), 'colspan' => $this->operationCount());
303
    }
304
    else {
305
      $row[] = l(t('edit'), $this->path . '/manage/' . $id);
306

    
307
      if ($field_ui) {
308
        $row[] = l(t('manage fields'), $this->path . '/manage/' . $id . '/fields');
309
        $row[] = l(t('manage display'), $this->path . '/manage/' . $id . '/display');
310
      }
311
      if ($i18n) {
312
        $row[] = l(t('translate'), $this->path . '/manage/' . $id . '/translate');
313
      }
314
      if ($exportable) {
315
        $row[] = l(t('clone'), $this->path . '/manage/' . $id . '/clone');
316
      }
317

    
318
      if (empty($this->entityInfo['exportable']) || !entity_has_status($this->entityType, $entity, ENTITY_IN_CODE)) {
319
        $row[] = l(t('delete'), $this->path . '/manage/' . $id . '/delete', array('query' => drupal_get_destination()));
320
      }
321
      elseif (entity_has_status($this->entityType, $entity, ENTITY_OVERRIDDEN)) {
322
        $row[] = l(t('revert'), $this->path . '/manage/' . $id . '/revert', array('query' => drupal_get_destination()));
323
      }
324
      else {
325
        $row[] = '';
326
      }
327
    }
328
    if ($exportable) {
329
      $row[] = l(t('export'), $this->path . '/manage/' . $id . '/export');
330
    }
331
    return $row;
332
  }
333

    
334

    
335
  /**
336
   * Builds the operation form.
337
   *
338
   * For the export operation a serialized string of the entity is directly
339
   * shown in the form (no submit function needed).
340
   */
341
  public function operationForm($form, &$form_state, $entity, $op) {
342
    switch ($op) {
343
      case 'revert':
344
        $label = entity_label($this->entityType, $entity);
345
        $confirm_question = t('Are you sure you want to revert the %entity %label?', array('%entity' => $this->entityInfo['label'], '%label' => $label));
346
        return confirm_form($form, $confirm_question, $this->path);
347

    
348
      case 'delete':
349
        $label = entity_label($this->entityType, $entity);
350
        $confirm_question = t('Are you sure you want to delete the %entity %label?', array('%entity' => $this->entityInfo['label'], '%label' => $label));
351
        return confirm_form($form, $confirm_question, $this->path);
352

    
353
      case 'export':
354
        if (!empty($this->entityInfo['exportable'])) {
355
          $export = entity_export($this->entityType, $entity);
356
          $form['export'] = array(
357
            '#type' => 'textarea',
358
            '#title' => t('Export'),
359
            '#description' => t('For importing copy the content of the text area and paste it into the import page.'),
360
            '#rows' => 25,
361
            '#default_value' => $export,
362
          );
363
          return $form;
364
        }
365

    
366
      case 'import':
367
        $form['import'] = array(
368
          '#type' => 'textarea',
369
          '#title' => t('Import'),
370
          '#description' => t('Paste an exported %entity_type here.', array('%entity_type' => $this->entityInfo['label'])),
371
          '#rows' => 20,
372
        );
373
        $form['overwrite'] = array(
374
          '#title' => t('Overwrite'),
375
          '#type' => 'checkbox',
376
          '#description' => t('If checked, any existing %entity with the same identifier will be replaced by the import.', array('%entity' => $this->entityInfo['label'])),
377
          '#default_value' => FALSE,
378
        );
379
        $form['submit'] = array(
380
          '#type' => 'submit',
381
          '#value' => t('Import'),
382
        );
383
        return $form;
384
    }
385
    drupal_not_found();
386
    exit;
387
  }
388

    
389
  /**
390
   * Operation form validation callback.
391
   */
392
  public function operationFormValidate($form, &$form_state) {
393
    if ($form_state['op'] == 'import') {
394
      if ($entity = entity_import($this->entityType, $form_state['values']['import'])) {
395
        // Store the successfully imported entity in $form_state.
396
        $form_state[$this->entityType] = $entity;
397
        if (!$form_state['values']['overwrite']) {
398
          // Check for existing entities with the same identifier.
399
          $id = entity_id($this->entityType, $entity);
400
          $entities = entity_load($this->entityType, array($id));
401
          if (!empty($entities)) {
402
            $label = entity_label($this->entityType, $entity);
403
            $vars = array('%entity' => $this->entityInfo['label'], '%label' => $label);
404
            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));
405
          }
406
        }
407
      }
408
      else {
409
        form_set_error('import', t('Import failed.'));
410
      }
411
    }
412
  }
413

    
414
  /**
415
   * Operation form submit callback.
416
   */
417
  public function operationFormSubmit($form, &$form_state) {
418
    $msg = $this->applyOperation($form_state['op'], $form_state[$this->entityType]);
419
    drupal_set_message($msg);
420
    $form_state['redirect'] = $this->path;
421
  }
422

    
423
  /**
424
   * Applies an operation to the given entity.
425
   *
426
   * Note: the export operation is directly carried out by the operationForm()
427
   * method.
428
   *
429
   * @param string $op
430
   *   The operation (revert, delete or import).
431
   * @param $entity
432
   *   The entity to manipulate.
433
   *
434
   * @return
435
   *   The status message of what has been applied.
436
   */
437
  public function applyOperation($op, $entity) {
438
    $label = entity_label($this->entityType, $entity);
439
    $vars = array('%entity' => $this->entityInfo['label'], '%label' => $label);
440
    $id = entity_id($this->entityType, $entity);
441
    $edit_link = l(t('edit'), $this->path . '/manage/' . $id . '/edit');
442

    
443
    switch ($op) {
444
      case 'revert':
445
        entity_delete($this->entityType, $id);
446
        watchdog($this->entityType, 'Reverted %entity %label to the defaults.', $vars, WATCHDOG_NOTICE, $edit_link);
447
        return t('Reverted %entity %label to the defaults.', $vars);
448

    
449
      case 'delete':
450
        entity_delete($this->entityType, $id);
451
        watchdog($this->entityType, 'Deleted %entity %label.', $vars);
452
        return t('Deleted %entity %label.', $vars);
453

    
454
      case 'import':
455
        // First check if there is any existing entity with the same ID.
456
        $id = entity_id($this->entityType, $entity);
457
        $entities = entity_load($this->entityType, array($id));
458
        if ($existing_entity = reset($entities)) {
459
          // Copy DB id and remove the new indicator to overwrite the DB record.
460
          $idkey = $this->entityInfo['entity keys']['id'];
461
          $entity->{$idkey} = $existing_entity->{$idkey};
462
          unset($entity->is_new);
463
        }
464
        entity_save($this->entityType, $entity);
465
        watchdog($this->entityType, 'Imported %entity %label.', $vars);
466
        return t('Imported %entity %label.', $vars);
467

    
468
      default:
469
        return FALSE;
470
    }
471
  }
472

    
473
  /**
474
   * Entity submit builder invoked via entity_ui_form_submit_build_entity().
475
   *
476
   * Extracts the form values and updates the entity.
477
   *
478
   * The provided implementation makes use of the helper function
479
   * entity_form_submit_build_entity() provided by core, which already invokes
480
   * the field API attacher for fieldable entities.
481
   *
482
   * @return
483
   *   The updated entity.
484
   *
485
   * @see entity_ui_form_submit_build_entity()
486
   */
487
  public function entityFormSubmitBuildEntity($form, &$form_state) {
488
    // Add the bundle property to the entity if the entity type supports bundles
489
    // and the form provides a value for the bundle key. Especially new entities
490
    // need to have their bundle property pre-populated before we invoke
491
    // entity_form_submit_build_entity().
492
    if (!empty($this->entityInfo['entity keys']['bundle']) && isset($form_state['values'][$this->entityInfo['entity keys']['bundle']])) {
493
      $form_state[$this->entityType]->{$this->entityInfo['entity keys']['bundle']} = $form_state['values'][$this->entityInfo['entity keys']['bundle']];
494
    }
495
    entity_form_submit_build_entity($this->entityType, $form_state[$this->entityType], $form, $form_state);
496
    return $form_state[$this->entityType];
497
  }
498
}
499

    
500
/**
501
 * UI controller providing UI for content entities.
502
 *
503
 * For a controller providing UI for bundleable content entities, see
504
 * EntityBundleableUIController.
505
 * For a controller providing admin UI for configuration entities, see
506
 * EntityDefaultUIController.
507
 */
508
class EntityContentUIController extends EntityDefaultUIController {
509

    
510
  /**
511
   * Provides definitions for implementing hook_menu().
512
   */
513
  public function hook_menu() {
514
    $items = parent::hook_menu();
515
    $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
516

    
517
    // Unset the manage entity path, as the provided UI is for admin entities.
518
    unset($items[$this->path]);
519

    
520
    $defaults = array(
521
      'file' => $this->entityInfo['admin ui']['file'],
522
      'file path' =>  isset($this->entityInfo['admin ui']['file path']) ? $this->entityInfo['admin ui']['file path'] : drupal_get_path('module', $this->entityInfo['module']),
523
    );
524

    
525
    // Add view, edit and delete menu items for content entities.
526
    $items[$this->path . '/' . $wildcard] = array(
527
      'title callback' => 'entity_ui_get_page_title',
528
      'title arguments' => array('view', $this->entityType, $this->id_count),
529
      'page callback' => 'entity_ui_entity_page_view',
530
      'page arguments' => array($this->id_count),
531
      'load arguments' => array($this->entityType),
532
      'access callback' => 'entity_access',
533
      'access arguments' => array('view', $this->entityType, $this->id_count),
534
    ) + $defaults;
535
    $items[$this->path . '/' . $wildcard . '/view'] = array(
536
      'title' => 'View',
537
      'type' => MENU_DEFAULT_LOCAL_TASK,
538
      'load arguments' => array($this->entityType),
539
      'weight' => -10,
540
    ) + $defaults;
541
    $items[$this->path . '/' . $wildcard . '/edit'] = array(
542
      'page callback' => 'entity_ui_get_form',
543
      'page arguments' => array($this->entityType, $this->id_count),
544
      'load arguments' => array($this->entityType),
545
      'access callback' => 'entity_access',
546
      'access arguments' => array('edit', $this->entityType, $this->id_count),
547
      'title' => 'Edit',
548
      'type' => MENU_LOCAL_TASK,
549
      'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
550
    ) + $defaults;
551
    $items[$this->path . '/' . $wildcard . '/delete'] = array(
552
      'page callback' => 'drupal_get_form',
553
      'page arguments' => array($this->entityType . '_operation_form', $this->entityType, $this->id_count, 'delete'),
554
      'load arguments' => array($this->entityType),
555
      'access callback' => 'entity_access',
556
      'access arguments' => array('delete', $this->entityType, $this->id_count),
557
      'title' => 'Delete',
558
      'type' => MENU_LOCAL_TASK,
559
      'context' => MENU_CONTEXT_INLINE,
560
      'file' => $this->entityInfo['admin ui']['file'],
561
      'file path' => isset($this->entityInfo['admin ui']['file path']) ? $this->entityInfo['admin ui']['file path'] : drupal_get_path('module', $this->entityInfo['module']),
562
    ) + $defaults;
563

    
564
    return $items;
565
  }
566

    
567
  /**
568
   * Operation form submit callback.
569
   */
570
  public function operationFormSubmit($form, &$form_state) {
571
    parent::operationFormSubmit($form, $form_state);
572
    // The manage entity path is unset for the content entity UI.
573
    $form_state['redirect'] = '<front>';
574
  }
575
}
576

    
577
/**
578
 * UI controller providing UI for bundleable content entities.
579
 *
580
 * Adds a bundle selection page to the entity/add path, analogously to the
581
 * node/add path.
582
 */
583
class EntityBundleableUIController extends EntityContentUIController {
584

    
585
  /**
586
   * Provides definitions for implementing hook_menu().
587
   */
588
  public function hook_menu() {
589
    $items = parent::hook_menu();
590

    
591
    // Extend the 'add' path.
592
    $items[$this->path . '/add'] = array(
593
      'title callback' => 'entity_ui_get_action_title',
594
      'title arguments' => array('add', $this->entityType),
595
      'page callback' => 'entity_ui_bundle_add_page',
596
      'page arguments' => array($this->entityType),
597
      'access callback' => 'entity_access',
598
      'access arguments' => array('create', $this->entityType),
599
      'type' => MENU_LOCAL_ACTION,
600
    );
601
    $items[$this->path . '/add/%'] = array(
602
      'title callback' => 'entity_ui_get_action_title',
603
      'title arguments' => array('add', $this->entityType, $this->id_count + 1),
604
      'page callback' => 'entity_ui_get_bundle_add_form',
605
      'page arguments' => array($this->entityType, $this->id_count + 1),
606
      'access callback' => 'entity_access',
607
      'access arguments' => array('create', $this->entityType),
608
    );
609

    
610
    if (!empty($this->entityInfo['admin ui']['file'])) {
611
      // Add in the include file for the entity form.
612
      foreach (array('/add', '/add/%') as $path_end) {
613
        $items[$this->path . $path_end]['file'] = $this->entityInfo['admin ui']['file'];
614
        $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']);
615
      }
616
    }
617

    
618
    return $items;
619
  }
620
}
621

    
622
/**
623
 * Form builder function for the overview form.
624
 *
625
 * @see EntityDefaultUIController::overviewForm()
626
 */
627
function entity_ui_overview_form($form, &$form_state, $entity_type) {
628
  return entity_ui_controller($entity_type)->overviewForm($form, $form_state);
629
}
630

    
631
/**
632
 * Form builder for the entity operation form.
633
 *
634
 * @see EntityDefaultUIController::operationForm()
635
 */
636
function entity_ui_operation_form($form, &$form_state, $entity_type, $entity, $op) {
637
  $form_state['op'] = $op;
638
  return entity_ui_controller($entity_type)->operationForm($form, $form_state, $entity, $op);
639
}
640

    
641
/**
642
 * Form wrapper the main entity form.
643
 *
644
 * @see entity_ui_form_defaults()
645
 */
646
function entity_ui_main_form_defaults($form, &$form_state, $entity = NULL, $op = NULL) {
647
  // Now equals entity_ui_form_defaults() but is still here to keep backward
648
  // compatibility.
649
  return entity_ui_form_defaults($form, $form_state, $form_state['entity_type'], $entity, $op);
650
}
651

    
652
/**
653
 * Clones the entity object and makes sure it will get saved as new entity.
654
 *
655
 * @return
656
 *   The cloned entity object.
657
 */
658
function entity_ui_clone_entity($entity_type, $entity) {
659
  // Clone the entity and make sure it will get saved as a new entity.
660
  $entity = clone $entity;
661

    
662
  $entity_info = entity_get_info($entity_type);
663
  $entity->{$entity_info['entity keys']['id']} = FALSE;
664
  if (!empty($entity_info['entity keys']['name'])) {
665
    $entity->{$entity_info['entity keys']['name']} = FALSE;
666
  }
667
  $entity->is_new = TRUE;
668

    
669
  // Make sure the status of a cloned exportable is custom.
670
  if (!empty($entity_info['exportable'])) {
671
    $status_key = isset($entity_info['entity keys']['status']) ? $entity_info['entity keys']['status'] : 'status';
672
    $entity->$status_key = ENTITY_CUSTOM;
673
  }
674
  return $entity;
675
}
676

    
677
/**
678
 * Form wrapper callback for all entity ui forms.
679
 *
680
 * This callback makes sure the form state is properly initialized and sets
681
 * some useful default titles.
682
 *
683
 * @see EntityDefaultUIController::hook_forms()
684
 */
685
function entity_ui_form_defaults($form, &$form_state, $entity_type, $entity = NULL, $op = NULL) {
686
  $defaults = array(
687
    'entity_type' => $entity_type,
688
  );
689
  if (isset($entity)) {
690
    $defaults[$entity_type] = $entity;
691
  }
692
  if (isset($op)) {
693
    $defaults['op'] = $op;
694
  }
695
  $form_state += $defaults;
696
  if (isset($op)) {
697
    drupal_set_title(entity_ui_get_page_title($op, $entity_type, $entity), PASS_THROUGH);
698
  }
699
  // Add in handlers pointing to the controller for the forms implemented by it.
700
  if (isset($form_state['build_info']['base_form_id']) && $form_state['build_info']['base_form_id'] != $entity_type . '_form') {
701
    $form['#validate'][] = 'entity_ui_controller_form_validate';
702
    $form['#submit'][] = 'entity_ui_controller_form_submit';
703
  }
704
  return $form;
705
}
706

    
707
/**
708
 * Validation callback for forms implemented by the UI controller.
709
 */
710
function entity_ui_controller_form_validate($form, &$form_state) {
711
  // Remove 'entity_ui_' prefix and the '_form' suffix.
712
  $base = substr($form_state['build_info']['base_form_id'], 10, -5);
713
  $method = $base . 'FormValidate';
714
  entity_ui_controller($form_state['entity_type'])->$method($form, $form_state);
715
}
716

    
717
/**
718
 * Submit callback for forms implemented by the UI controller.
719
 */
720
function entity_ui_controller_form_submit($form, &$form_state) {
721
  // Remove 'entity_ui_' prefix and the '_form' suffix.
722
  $base = substr($form_state['build_info']['base_form_id'], 10, -5);
723
  $method = $base . 'FormSubmit';
724
  entity_ui_controller($form_state['entity_type'])->$method($form, $form_state);
725
}
726

    
727
/**
728
 * Submit builder for the main entity form, which extracts the form values and updates the entity.
729
 *
730
 * This is a helper function for entities making use of the entity UI
731
 * controller.
732
 *
733
 * @return
734
 *   The updated entity.
735
 *
736
 * @see EntityDefaultUIController::hook_forms()
737
 * @see EntityDefaultUIController::entityFormSubmitBuildEntity()
738
 */
739
function entity_ui_form_submit_build_entity($form, &$form_state) {
740
  return entity_ui_controller($form_state['entity_type'])->entityFormSubmitBuildEntity($form, $form_state);
741
}
742

    
743
/**
744
 * Validation callback for machine names of exportables.
745
 *
746
 * We don't allow numeric machine names, as entity_load() treats them as the
747
 * numeric identifier and they are easily confused with ids in general.
748
 */
749
function entity_ui_validate_machine_name($element, &$form_state) {
750
  if (is_numeric($element['#value'])) {
751
    form_error($element, t('Machine-readable names must not consist of numbers only.'));
752
  }
753
}
754

    
755
/**
756
 * Returns HTML for an entity on the entity overview listing.
757
 *
758
 * @ingroup themeable
759
 */
760
function theme_entity_ui_overview_item($variables) {
761
  $output = $variables['url'] ? l($variables['label'], $variables['url']['path'], $variables['url']['options']) : check_plain($variables['label']);
762
  if ($variables['name']) {
763
    $output .= ' <small>(' . t('Machine name') . ': ' . check_plain($variables['name']) . ')</small>';
764
  }
765
  return $output;
766
}
767