EntityFormControllerNG.php 2.81 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Entity\EntityFormControllerNG.
6
7
8
9
10
11
12
13
 */

namespace Drupal\Core\Entity;

/**
 * Entity form controller variant for entity types using the new property API.
 *
 * @todo: Merge with EntityFormController and overhaul once all entity types
14
15
16
17
18
 * are converted to the new entity field API.
 *
 * See the EntityNG documentation for an explanation of "NG".
 *
 * @see \Drupal\Core\EntityNG
19
20
21
22
23
24
 */
class EntityFormControllerNG extends EntityFormController {

  /**
   * Overrides EntityFormController::form().
   */
25
26
  public function form(array $form, array &$form_state) {
    $entity = $this->entity;
27
    // @todo Exploit the Field API to generate the default widgets for the
28
    // entity fields.
29
30
    $info = $entity->entityInfo();
    if (!empty($info['fieldable'])) {
31
      field_attach_form($entity, $form, $form_state, $this->getFormLangcode($form_state));
32
    }
33

34
35
    // Add a process callback so we can assign weights and hide extra fields.
    $form['#process'][] = array($this, 'processForm');
36

37
38
39
    return $form;
  }

40
41
42
43
44
  /**
   * Overrides EntityFormController::submitEntityLanguage().
   */
  protected function submitEntityLanguage(array $form, array &$form_state) {
    // Nothing to do here, as original field values are always stored with
45
    // Language::LANGCODE_DEFAULT language.
46
47
48
49
    // @todo Delete this method when merging EntityFormControllerNG with
    //   EntityFormController.
  }

50
51
52
53
  /**
   * Overrides EntityFormController::buildEntity().
   */
  public function buildEntity(array $form, array &$form_state) {
54
    $entity = clone $this->entity;
55
56
    $entity_type = $entity->entityType();
    $info = entity_get_info($entity_type);
57
    // @todo Exploit the Field API to process the submitted entity fields.
58

59
60
61
    // Copy top-level form values that are entity fields but not handled by
    // field API without changing existing entity fields that are not being
    // edited by this form. Values of fields handled by field API are copied
62
    // by field_attach_extract_form_values() below.
63
    $values_excluding_fields = $info['fieldable'] ? array_diff_key($form_state['values'], field_info_instances($entity_type, $entity->bundle())) : $form_state['values'];
64
    $definitions = $entity->getPropertyDefinitions();
65
    foreach ($values_excluding_fields as $key => $value) {
66
      if (isset($definitions[$key])) {
67
        $entity->$key = $value;
68
      }
69
70
    }

71
    // Invoke all specified builders for copying form values to entity fields.
72
73
    if (isset($form['#entity_builders'])) {
      foreach ($form['#entity_builders'] as $function) {
74
        call_user_func_array($function, array($entity_type, $entity, &$form, &$form_state));
75
76
77
      }
    }

78
    // Invoke field API for copying field values.
79
    if ($info['fieldable']) {
80
      field_attach_extract_form_values($entity, $form, $form_state);
81
82
83
84
    }
    return $entity;
  }
}