diff --git a/core/core.services.yml b/core/core.services.yml index 402d6ee..50a06ee 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -188,6 +188,8 @@ services: entity.manager: class: Drupal\Core\Entity\EntityManager arguments: ['@container.namespaces', '@service_container', '@module_handler', '@cache.cache', '@language_manager', '@string_translation'] + entity.content.form_helper: + class: Drupal\Core\Entity\ContentEntityFormHelper plugin.manager.field.field_type: class: Drupal\Core\Field\FieldTypePluginManager arguments: ['@container.namespaces', '@cache.field', '@language_manager', '@module_handler'] diff --git a/core/lib/Drupal/Core/Entity/ContentEntityFormController.php b/core/lib/Drupal/Core/Entity/ContentEntityFormController.php index 509d10f..5aac3c4 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityFormController.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityFormController.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Entity; -use Drupal\Core\Language\Language; +use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -15,7 +15,7 @@ * * @see \Drupal\Core\ContentEntityBase */ -class ContentEntityFormController extends EntityFormController { +class ContentEntityFormController extends EntityFormController implements ContentEntityFormControllerInterface { /** * The entity manager. @@ -25,13 +25,23 @@ class ContentEntityFormController extends EntityFormController { protected $entityManager; /** + * The ContentEntity form helper. + * + * @var \Drupal\Core\Entity\ContentEntityFormHelper + */ + protected $entityFormHelper; + + /** * Constructs a ContentEntityFormController object. * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. */ - public function __construct(EntityManagerInterface $entity_manager) { + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper) { $this->entityManager = $entity_manager; + $this->EntityFormHelper = $entity_form_helper; } /** @@ -39,7 +49,8 @@ public function __construct(EntityManagerInterface $entity_manager) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager') + $container->get('entity.manager'), + $container->get('entity.content.form_helper') ); } @@ -47,12 +58,7 @@ public static function create(ContainerInterface $container) { * {@inheritdoc} */ public function form(array $form, array &$form_state) { - $entity = $this->entity; - // @todo Exploit the Field API to generate the default widgets for the - // entity fields. - if ($entity->getEntityType()->isFieldable()) { - field_attach_form($entity, $form, $form_state, $this->getFormLangcode($form_state)); - } + $this->EntityFormHelper->attachWidgets($this->entity, $form_state['form_display'], $form, $form_state); // Add a process callback so we can assign weights and hide extra fields. $form['#process'][] = array($this, 'processForm'); @@ -60,33 +66,36 @@ public function form(array $form, array &$form_state) { return $form; } + public function processForm($element, $form_state, $form) { + parent::processForm($element, $form_state, $form); + + $form_display = $this->getFormDisplay($form_state); + + // Assign the weights configured in the form display. + foreach ($form_display->getComponents() as $name => $options) { + if (isset($element[$name])) { + $element[$name]['#weight'] = $options['weight']; + } + } + + // Hide extra fields. + $extra_fields = field_info_extra_fields($this->entity->getEntityTypeId(), $this->entity->bundle(), 'form'); + foreach ($extra_fields as $extra_field => $info) { + if (!$form_display->getComponent($extra_field)) { + $element[$extra_field]['#access'] = FALSE; + } + } + + return $element; + } + /** * {@inheritdoc} */ public function validate(array $form, array &$form_state) { $this->updateFormLangcode($form_state); $entity = $this->buildEntity($form, $form_state); - $entity_type = $entity->getEntityTypeId(); - $entity_langcode = $entity->language()->id; - - $violations = array(); - foreach ($entity as $field_name => $field) { - $field_violations = $field->validate(); - if (count($field_violations)) { - $violations[$field_name] = $field_violations; - } - } - - // Map errors back to form elements. - if ($violations) { - foreach ($violations as $field_name => $field_violations) { - $field_state = field_form_get_state($form['#parents'], $field_name, $form_state); - $field_state['constraint_violations'] = $field_violations; - field_form_set_state($form['#parents'], $field_name, $form_state, $field_state); - } - - field_invoke_method('flagErrors', _field_invoke_widget_target($form_state['form_display']), $entity, $form, $form_state); - } + $this->EntityFormHelper->validateWidgetsValues($entity, $form_state['form_display'], $form, $form_state); // @todo Remove this. // Execute legacy global validation handlers. @@ -102,6 +111,10 @@ protected function init(array &$form_state) { // language. $langcode = $this->getFormLangcode($form_state); $this->entity = $this->entity->getTranslation($langcode); + + $form_display = entity_get_render_form_display($this->entity, $this->getOperation()); + $this->setFormDisplay($form_display, $form_state); + parent::init($form_state); } @@ -130,34 +143,41 @@ public function isDefaultFormLangcode(array $form_state) { */ public function buildEntity(array $form, array &$form_state) { $entity = clone $this->entity; - $entity_type = $entity->getEntityTypeId(); - $info = \Drupal::entityManager()->getDefinition($entity_type); - // @todo Exploit the Entity Field API to process the submitted field values. - // 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 - // by field_attach_extract_form_values() below. - $values_excluding_fields = $info->isFieldable() ? array_diff_key($form_state['values'], field_info_instances($entity_type, $entity->bundle())) : $form_state['values']; - $definitions = $entity->getPropertyDefinitions(); - foreach ($values_excluding_fields as $key => $value) { - if (isset($definitions[$key])) { - $entity->$key = $value; + // First, extract values from widgets. + $extracted = $this->EntityFormHelper->extractWidgetsValues($entity, $form_state['form_display'], $form, $form_state); + + // Then extract the values of fields that are not rendered through widgets, + // by simply copying from top-level form values. This leaves the fields + // that are not being edited within this form untouched. + foreach ($form_state['values'] as $name => $values) { + if ($entity->hasField($name) && !isset($extracted[$name])) { + $entity->$name = $values; } } // Invoke all specified builders for copying form values to entity fields. if (isset($form['#entity_builders'])) { foreach ($form['#entity_builders'] as $function) { - call_user_func_array($function, array($entity_type, $entity, &$form, &$form_state)); + call_user_func_array($function, array($entity->getEntityTypeId(), $entity, &$form, &$form_state)); } } - // Invoke field API for copying field values. - if ($info->isFieldable()) { - field_attach_extract_form_values($entity, $form, $form_state, array('langcode' => $this->getFormLangcode($form_state))); - } return $entity; } + /** + * {@inheritdoc} + */ + public function getFormDisplay(array $form_state) { + return isset($form_state['form_display']) ? $form_state['form_display'] : NULL; + } + + /** + * {@inheritdoc} + */ + public function setFormDisplay(EntityFormDisplayInterface $form_display, array &$form_state) { + $form_state['form_display'] = $form_display; + return $this; + } } diff --git a/core/lib/Drupal/Core/Entity/ContentEntityFormControllerInterface.php b/core/lib/Drupal/Core/Entity/ContentEntityFormControllerInterface.php new file mode 100644 index 0000000..77235d9 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/ContentEntityFormControllerInterface.php @@ -0,0 +1,41 @@ + The location of field values in $form_state['values'], + * '#entity_type' => The name of the entity type, + * '#bundle' => The name of the bundle, + * // One sub-array per field appearing in the entity, keyed by field name. + * // The structure of the array differs slightly depending on whether the + * // widget is 'single-value' (provides the input for one field value, + * // most common case), and will therefore be repeated as many times as + * // needed, or 'multiple-values' (one single widget allows the input of + * // several values, e.g checkboxes, select box...). + * 'field_foo' => array( + * '#access' => TRUE if the current user has 'edit' grants for the field, + * FALSE if not. + * 'widget' => array( + * '#field_name' => The name of the field, + * '#language' => $langcode, + * '#field_parents' => The 'parents' space for the field in the form, + * equal to the #parents property of the $form parameter received by + * field_attach_form(), + * '#required' => Whether or not the field is required, + * '#title' => The label of the field instance, + * '#description' => The description text for the field instance, + * + * // Only for 'single' widgets: + * '#theme' => 'field_multiple_value_form', + * '#cardinality' => The field cardinality, + * '#cardinality_multiple => TRUE if the field can contain multiple + * items, FALSE otherwise. + * // One sub-array per copy of the widget, keyed by delta. + * 0 => array( + * '#entity_type' => The name of the entity type, + * '#bundle' => The name of the bundle, + * '#field_name' => The name of the field, + * '#field_parents' => The 'parents' space for the field in the form, + * equal to the #parents property of the $form parameter, + * '#title' => The title to be displayed by the widget, + * '#default_value' => The field value for delta 0, + * '#required' => Whether the widget should be marked required, + * '#delta' => 0, + * // The remaining elements in the sub-array depend on the widget. + * '#type' => The type of the widget, + * ... + * ), + * 1 => array( + * ... + * ), + * + * // Only for multiple widgets: + * '#entity_type' => The name of the entity type, + * '#bundle' => $instance['bundle'], + * // The remaining elements in the sub-array depend on the widget. + * '#type' => The type of the widget, + * ... + * ), + * ... + * ), + * ) + * @endcode + * + * Additionally, some processing data is placed in $form_state, and can be + * accessed by field_form_get_state() and field_form_set_state(). + * + * @param EntityInterface $entity + * The entity. + * @param EntityFormDisplayInterface $display + * The form display containing the widget settings. + * @param array $form + * The form structure to fill in. This can be a full form structure, or a + * sub-element of a larger form. The #parents property can be set to + * control the location of submitted field values within + * $form_state['values']. If not specified, $form['#parents'] is set to an + * empty array, which results in field values located at the top-level of + * $form_state['values']. + * @param array $form_state + * The form state. + */ + public function attachWidgets(EntityInterface $entity, EntityFormDisplayInterface $display, array &$form, array &$form_state) { + // Set #parents to 'top-level' by default. + $form += array('#parents' => array()); + + // Let each widget generate the form elements. + foreach ($entity as $name => $items) { + if ($widget = $display->getRenderer($name)) { + $items->filterEmptyItems(); + $form += $widget->form($items, $form, $form_state); + + // Assign the correct weight. This duplicates the reordering done in + // processForm(), but is needed for other forms calling this method + // directly. + $options = $display->getComponent($name); + $form[$name]['#weight'] = $options['weight']; + } + } + + // @todo hook_field_attach_form() ? + // We still need a hook to allow alteration of "forms with widgets", because + // the regular "entity form" alter hook won't run non "non enity forms"... + // Although, there's already hook_field_widget_form_alter() to alter each widget ? + } + + /** + * Extracts field values from the submitted widget values into the entity. + * + * This accounts for drag-and-drop reordering of field values, and filtering + * of empty values. + * + * @param EntityInterface $entity + * The entity. + * @param EntityFormDisplayInterface $display + * The form display containing the widget settings. + * @param array $form + * The form structure where field elements are attached to. This might be a + * full form structure, or a sub-element of a larger form. + * @param array $form_state + * The form state. + * + * @return array + * An array whose keys and values are the keys of the top-level entries in + * $form_state['values'] that have been processed. The remaining entries, if + * any, do not correspond to widgets and should be extracted manually by + * the caller if needed. + */ + public function extractWidgetsValues(EntityInterface $entity, EntityFormDisplayInterface $display, array &$form, array &$form_state) { + $extracted = array(); + + foreach ($entity as $name => $items) { + if ($widget = $display->getRenderer($name)) { + $widget->extractFormValues($items, $form, $form_state); + $extracted[$name] = $name; + } + } + // @todo hook_field_attach_extract_form_values() ? - See above. + + return $extracted; + } + + /** + * Validates submitted widget values and sets the corresponding form errors. + * + * There are two levels of validation for fields in forms: widget validation + * and field validation. + * - Widget validation steps are specific to a given widget's own form + * structure and UI metaphors. They are executed during normal form + * validation, usually through Form API's #element_validate property. + * Errors reported at this level are typically those that prevent the + * extraction of proper field values from the submitted form input. + * - If no form / widget errors were reported for the field, field validation + * steps are performed according to the "constraints" specified by the + * field definition. Those are independent of the specific widget being + * used in a given form, and are also performed on REST entity submissions. + * + * This function performs field validation in the context of a form submission. + * It reports field constraint violations as form errors on the correct form + * elements. + * + * @param EntityInterface $entity + * The entity. + * @param EntityFormDisplayInterface $display + * The form display containing the widget settings. + * @param array $form + * The form structure where field elements are attached to. This might be a + * full form structure, or a sub-element of a larger form. + * @param array $form_state + * The form state. + */ + public function validateWidgetsValues(EntityInterface $entity, EntityFormDisplayInterface $display, array &$form, array &$form_state) { + foreach ($entity as $field_name => $items) { + // Only validate the fields that actually appear in the form, and let the + // widget assign the violations to the right form elements. + if ($widget = $display->getRenderer($field_name)) { + $violations = $items->validate(); + if (count($violations)) { + $widget->flagErrors($items, $violations, $form, $form_state); + } + } + } + } + +} diff --git a/core/lib/Drupal/Core/Entity/EntityFormController.php b/core/lib/Drupal/Core/Entity/EntityFormController.php index 342d395..9d36ea0 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormController.php +++ b/core/lib/Drupal/Core/Entity/EntityFormController.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Entity; -use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -121,9 +120,6 @@ protected function init(array &$form_state) { // Prepare the entity to be presented in the entity form. $this->prepareEntity(); - $form_display = entity_get_render_form_display($this->entity, $this->getOperation()); - $this->setFormDisplay($form_display, $form_state); - // Invoke the prepare form hooks. $this->prepareInvokeAll('entity_prepare_form', $form_state); $this->prepareInvokeAll($this->entity->getEntityTypeId() . '_prepare_form', $form_state); @@ -136,11 +132,6 @@ protected function init(array &$form_state) { */ public function form(array $form, array &$form_state) { $entity = $this->entity; - // @todo Exploit the Field API to generate the default widgets for the - // entity properties. - if ($entity->getEntityType()->isFieldable()) { - field_attach_form($entity, $form, $form_state, $this->getFormLangcode($form_state)); - } // Add a process callback so we can assign weights and hide extra fields. $form['#process'][] = array($this, 'processForm'); @@ -167,25 +158,6 @@ public function processForm($element, $form_state, $form) { // to the entity object, hence we must restore it. $this->entity = $form_state['controller']->getEntity(); - // Assign the weights configured in the form display. - foreach ($this->getFormDisplay($form_state)->getComponents() as $name => $options) { - if (isset($element[$name])) { - $element[$name]['#weight'] = $options['weight']; - } - } - - // Hide or assign weights for extra fields. - $extra_fields = field_info_extra_fields($this->entity->getEntityTypeId(), $this->entity->bundle(), 'form'); - foreach ($extra_fields as $extra_field => $info) { - $component = $this->getFormDisplay($form_state)->getComponent($extra_field); - if (!$component) { - $element[$extra_field]['#access'] = FALSE; - } - else { - $element[$extra_field]['#weight'] = $component['weight']; - } - } - return $element; } @@ -414,7 +386,7 @@ protected function prepareInvokeAll($hook, array &$form_state) { if (function_exists($function)) { // Ensure we pass an updated translation object and form display at // each invocation, since they depend on form state which is alterable. - $args = array($this->entity, $this->getFormDisplay($form_state), $this->operation, &$form_state); + $args = array($this->entity, $this->operation, &$form_state); call_user_func_array($function, $args); } } @@ -423,21 +395,6 @@ protected function prepareInvokeAll($hook, array &$form_state) { /** * {@inheritdoc} */ - public function getFormDisplay(array $form_state) { - return isset($form_state['form_display']) ? $form_state['form_display'] : NULL; - } - - /** - * {@inheritdoc} - */ - public function setFormDisplay(EntityFormDisplayInterface $form_display, array &$form_state) { - $form_state['form_display'] = $form_display; - return $this; - } - - /** - * {@inheritdoc} - */ public function getOperation() { return $this->operation; } diff --git a/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php b/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php index e63679b..5e0881d 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Entity; -use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\BaseFormIdInterface; use Drupal\Core\StringTranslation\TranslationInterface; @@ -58,30 +57,6 @@ public function setOperation($operation); public function getOperation(); /** - * Returns the form display. - * - * @param array $form_state - * An associative array containing the current state of the form. - * - * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface. - * The current form display. - */ - public function getFormDisplay(array $form_state); - - /** - * Sets the form display. - * - * Sets the form display which will be used for populating form element - * defaults. - * - * @param \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display - * The form display that the current form operates with. - * @param array $form_state - * An associative array containing the current state of the form. - */ - public function setFormDisplay(EntityFormDisplayInterface $form_display, array &$form_state); - - /** * Returns the form entity. * * The form entity which has been used for populating form element defaults. diff --git a/core/lib/Drupal/Core/Field/ConfigFieldItemList.php b/core/lib/Drupal/Core/Field/ConfigFieldItemList.php index 3c76be2..e99ad5c 100644 --- a/core/lib/Drupal/Core/Field/ConfigFieldItemList.php +++ b/core/lib/Drupal/Core/Field/ConfigFieldItemList.php @@ -100,15 +100,9 @@ public function defaultValuesFormValidate(array $element, array &$form, array &$ $widget->extractFormValues($this, $element, $form_state); $violations = $this->validate(); + // Assign reported errors to the correct form element. if (count($violations)) { - // Store reported errors in $form_state. - $field_name = $this->getFieldDefinition()->getName(); - $field_state = field_form_get_state($element['#parents'], $field_name, $form_state); - $field_state['constraint_violations'] = $violations; - field_form_set_state($element['#parents'], $field_name, $form_state, $field_state); - - // Assign reported errors to the correct form element. - $widget->flagErrors($this, $element, $form_state); + $widget->flagErrors($this, $violations, $element, $form_state); } } diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php index ffa82d1..12604fa 100644 --- a/core/lib/Drupal/Core/Field/WidgetBase.php +++ b/core/lib/Drupal/Core/Field/WidgetBase.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\NestedArray; use Symfony\Component\Validator\ConstraintViolationInterface; +use Symfony\Component\Validator\ConstraintViolationListInterface; /** * Base class for 'Field widget' plugin implementations. @@ -322,12 +323,12 @@ public function extractFormValues(FieldItemListInterface $items, array $form, ar /** * {@inheritdoc} */ - public function flagErrors(FieldItemListInterface $items, array $form, array &$form_state) { + public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, array &$form_state) { $field_name = $this->fieldDefinition->getName(); $field_state = field_form_get_state($form['#parents'], $field_name, $form_state); - if (!empty($field_state['constraint_violations'])) { + if ($violations->count()) { $form_builder = \Drupal::formBuilder(); // Locate the correct element in the the form. @@ -352,7 +353,7 @@ public function flagErrors(FieldItemListInterface $items, array $form, array &$f $is_multiple = $definition['multiple_values']; $violations_by_delta = array(); - foreach ($field_state['constraint_violations'] as $violation) { + foreach ($violations as $violation) { // Separate violations by delta. $property_path = explode('.', $violation->getPropertyPath()); $delta = array_shift($property_path); diff --git a/core/lib/Drupal/Core/Field/WidgetBaseInterface.php b/core/lib/Drupal/Core/Field/WidgetBaseInterface.php index 8c63e23..69e1d52 100644 --- a/core/lib/Drupal/Core/Field/WidgetBaseInterface.php +++ b/core/lib/Drupal/Core/Field/WidgetBaseInterface.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Field; +use Symfony\Component\Validator\ConstraintViolationListInterface; + /** * Base interface definition for "Field widget" plugins. * @@ -59,12 +61,14 @@ public function extractFormValues(FieldItemListInterface $items, array $form, ar * * @param \Drupal\Core\Field\FieldItemListInterface $items * The field values. + * @param \Symfony\Component\Validator\ConstraintViolationListInterface $violations + * The constraint violations that were detected. * @param array $form * The form structure where field elements are attached to. This might be a * full form structure, or a sub-element of a larger form. * @param array $form_state * The form state. */ - public function flagErrors(FieldItemListInterface $items, array $form, array &$form_state); + public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, array &$form_state); } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php index e67b354..1fc1e79 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php @@ -9,6 +9,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\ContentEntityFormController; +use Drupal\Core\Entity\ContentEntityFormHelper; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityStorageControllerInterface; use Drupal\Core\Language\Language; @@ -39,13 +40,15 @@ class CustomBlockFormController extends ContentEntityFormController { * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. * @param \Drupal\Core\Entity\EntityStorageControllerInterface $custom_block_storage * The custom block storage controller. * @param \Drupal\Core\Language\LanguageManager $language_manager * The language manager. */ - public function __construct(EntityManagerInterface $entity_manager, EntityStorageControllerInterface $custom_block_storage, LanguageManager $language_manager) { - parent::__construct($entity_manager); + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper, EntityStorageControllerInterface $custom_block_storage, LanguageManager $language_manager) { + parent::__construct($entity_manager, $entity_form_helper); $this->customBlockStorage = $custom_block_storage; $this->languageManager = $language_manager; } @@ -57,6 +60,7 @@ public static function create(ContainerInterface $container) { $entity_manager = $container->get('entity.manager'); return new static( $entity_manager, + $container->get('entity.content.form_helper'), $entity_manager->getStorageController('custom_block'), $container->get('language_manager') ); diff --git a/core/modules/book/book.module b/core/modules/book/book.module index 8fe9e41..ea1e939 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -561,7 +561,7 @@ function book_node_predelete(EntityInterface $node) { /** * Implements hook_node_prepare_form(). */ -function book_node_prepare_form(NodeInterface $node, $form_display, $operation, array &$form_state) { +function book_node_prepare_form(NodeInterface $node, $operation, array &$form_state) { // Get BookManager service $book_manager = \Drupal::service('book.manager'); diff --git a/core/modules/book/lib/Drupal/book/Form/BookOutlineForm.php b/core/modules/book/lib/Drupal/book/Form/BookOutlineForm.php index aae3eac..c2fd234 100644 --- a/core/modules/book/lib/Drupal/book/Form/BookOutlineForm.php +++ b/core/modules/book/lib/Drupal/book/Form/BookOutlineForm.php @@ -8,6 +8,7 @@ namespace Drupal\book\Form; use Drupal\Core\Entity\ContentEntityFormController; +use Drupal\Core\Entity\ContentEntityFormHelper; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\book\BookManager; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -36,11 +37,13 @@ class BookOutlineForm extends ContentEntityFormController { * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. * @param \Drupal\book\BookManager $book_manager * The BookManager service. */ - public function __construct(EntityManagerInterface $entity_manager, BookManager $book_manager) { - parent::__construct($entity_manager); + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper, BookManager $book_manager) { + parent::__construct($entity_manager, $entity_form_helper); $this->bookManager = $book_manager; } @@ -50,6 +53,7 @@ public function __construct(EntityManagerInterface $entity_manager, BookManager public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), + $container->get('entity.content.form_helper'), $container->get('book.manager') ); } diff --git a/core/modules/comment/lib/Drupal/comment/CommentFormController.php b/core/modules/comment/lib/Drupal/comment/CommentFormController.php index cf1d724..d73b779 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentFormController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentFormController.php @@ -12,6 +12,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Entity\ContentEntityFormController; +use Drupal\Core\Entity\ContentEntityFormHelper; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Language\Language; use Drupal\Core\Session\AccountInterface; @@ -36,6 +37,7 @@ class CommentFormController extends ContentEntityFormController { public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), + $container->get('entity.content.form_helper'), $container->get('field.info'), $container->get('current_user') ); @@ -46,13 +48,15 @@ public static function create(ContainerInterface $container) { * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager service. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. * @param \Drupal\field\FieldInfo $field_info * The field info service. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. */ - public function __construct(EntityManagerInterface $entity_manager, FieldInfo $field_info, AccountInterface $current_user) { - parent::__construct($entity_manager); + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper, FieldInfo $field_info, AccountInterface $current_user) { + parent::__construct($entity_manager, $entity_form_helper); $this->fieldInfo = $field_info; $this->currentUser = $current_user; } diff --git a/core/modules/comment/lib/Drupal/comment/Form/DeleteForm.php b/core/modules/comment/lib/Drupal/comment/Form/DeleteForm.php index f226305..24881bc 100644 --- a/core/modules/comment/lib/Drupal/comment/Form/DeleteForm.php +++ b/core/modules/comment/lib/Drupal/comment/Form/DeleteForm.php @@ -10,6 +10,7 @@ use Drupal\comment\CommentManagerInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\ContentEntityConfirmFormBase; +use Drupal\Core\Entity\ContentEntityFormHelper; use Drupal\Core\Entity\EntityManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -30,11 +31,13 @@ class DeleteForm extends ContentEntityConfirmFormBase { * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. * @param \Drupal\comment\CommentManagerInterface $comment_manager * The comment manager service. */ - public function __construct(EntityManagerInterface $entity_manager, CommentManagerInterface $comment_manager) { - parent::__construct($entity_manager); + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper, CommentManagerInterface $comment_manager) { + parent::__construct($entity_manager, $entity_form_helper); $this->commentManager = $comment_manager; } @@ -44,6 +47,7 @@ public function __construct(EntityManagerInterface $entity_manager, CommentManag public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), + $container->get('entity.content.form_helper'), $container->get('comment.manager') ); } diff --git a/core/modules/datetime/datetime.module b/core/modules/datetime/datetime.module index ccf1a0c..1172fec 100644 --- a/core/modules/datetime/datetime.module +++ b/core/modules/datetime/datetime.module @@ -1001,7 +1001,7 @@ function datetime_form_node_form_alter(&$form, &$form_state, $form_id) { /** * Implements hook_node_prepare_form(). */ -function datetime_node_prepare_form(NodeInterface $node, $form_display, $operation, array &$form_state) { +function datetime_node_prepare_form(NodeInterface $node, $operation, array &$form_state) { // Prepare the 'Authored on' date to use datetime. $node->date = DrupalDateTime::createFromTimestamp($node->getCreatedTime()); } diff --git a/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php b/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php index f6f5a94..6d3cace 100644 --- a/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php +++ b/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php @@ -7,6 +7,7 @@ namespace Drupal\edit\Form; +use Drupal\Core\Entity\ContentEntityFormHelper; use Drupal\Core\Form\FormBase; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Entity\EntityInterface; @@ -42,6 +43,13 @@ class EditFieldForm extends FormBase { protected $nodeTypeStorage; /** + * The ContentEntity form helper. + * + * @var \Drupal\Core\Entity\ContentEntityFormHelper + */ + protected $entityFormHelper; + + /** * Constructs a new EditFieldForm. * * @param \Drupal\user\TempStoreFactory $temp_store_factory @@ -50,11 +58,14 @@ class EditFieldForm extends FormBase { * The module handler. * @param \Drupal\Core\Entity\EntityStorageControllerInterface $node_type_storage * The node type storage. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. */ - public function __construct(TempStoreFactory $temp_store_factory, ModuleHandlerInterface $module_handler, EntityStorageControllerInterface $node_type_storage) { + public function __construct(TempStoreFactory $temp_store_factory, ModuleHandlerInterface $module_handler, EntityStorageControllerInterface $node_type_storage, ContentEntityFormHelper $entity_form_helper) { + $this->tempStoreFactory = $temp_store_factory; $this->moduleHandler = $module_handler; $this->nodeTypeStorage = $node_type_storage; - $this->tempStoreFactory = $temp_store_factory; + $this->entityFormHelper = $entity_form_helper; } /** @@ -64,7 +75,8 @@ public static function create(ContainerInterface $container) { return new static( $container->get('user.tempstore'), $container->get('module_handler'), - $container->get('entity.manager')->getStorageController('node_type') + $container->get('entity.manager')->getStorageController('node_type'), + $container->get('entity.content.form_helper') ); } @@ -86,7 +98,7 @@ public function buildForm(array $form, array &$form_state, EntityInterface $enti } // Add the field form. - field_attach_form($form_state['entity'], $form, $form_state, $form_state['langcode'], array('field_name' => $form_state['field_name'])); + $this->entityFormHelper->attachWidgets($entity, $form_state['form_display'], $form, $form_state); // Add a dummy changed timestamp field to attach form errors to. if ($entity instanceof EntityChangedInterface) { @@ -128,7 +140,14 @@ protected function init(array &$form_state, EntityInterface $entity, $field_name // @todo Allow the usage of different form modes by exposing a hook and the // UI for them. - $form_state['form_display'] = entity_get_render_form_display($entity, 'default'); + $display = entity_get_render_form_display($entity, 'default'); + foreach ($display->getComponents() as $name => $optipns) { + if ($name != $field_name) { + $display->removeComponent($name); + } + } + $form_state['form_display'] = $display; + // @todo Revisit what really needs to be in $form_state... } /** @@ -136,7 +155,8 @@ protected function init(array &$form_state, EntityInterface $entity, $field_name */ public function validateForm(array &$form, array &$form_state) { $entity = $this->buildEntity($form, $form_state); - field_attach_form_validate($entity, $form, $form_state, array('field_name' => $form_state['field_name'])); + + $this->entityFormHelper->validateWidgetsValues($entity, $form_state['form_display'], $form, $form_state); // Do validation on the changed field as well and assign the error to the // dummy form element we added for this. We don't know the name of this @@ -172,7 +192,7 @@ protected function buildEntity(array $form, array &$form_state) { $entity = clone $form_state['entity']; $field_name = $form_state['field_name']; - field_attach_extract_form_values($entity, $form, $form_state, array('field_name' => $field_name)); + $this->entityFormHelper->extractWidgetsValues($entity, $form_state['form_display'], $form, $form_state); // @todo Refine automated log messages and abstract them to all entity // types: http://drupal.org/node/1678002. diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index a85300f..aa4725c 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -11,15 +11,16 @@ /** * Exposes "pseudo-field" components on fieldable entities. * - * Field UI's "Manage fields" and "Manage display" pages let users re-order - * fields, but also non-field components. For nodes, these include the title - * and other elements exposed by modules through hook_form_alter(). + * Field UI's "Manage display" and "Manage form display" pages let users + * re-order fields rendered through the regular widget/formatter pipeline, but + * also other components: entity fields that are rendered through custom code, + * or other arbitrary components added through hook_form_alter() or + * hook_entity_view(). * * Fieldable entities or modules that want to have their components supported * should expose them using this hook. The user-defined settings (weight, * visible) are automatically applied on rendered forms and displayed entities - * in a #pre_render callback added by field_attach_form() and - * field_attach_view(). + * by ContentEntityFormController::form() and EntityViewBuilder::viewMultiple(). * * @see hook_field_extra_fields_alter() * @@ -162,8 +163,8 @@ function hook_field_info_alter(&$info) { * * Widgets are @link forms_api_reference.html Form API @endlink * elements with additional processing capabilities. The methods of the - * WidgetInterface object are typically called by the Field Attach API during - * the creation of the field form structure with field_attach_form(). + * WidgetInterface object are typically called by respective methods in the + * ContentEntityFormHelper class. * * @see field * @see field_types diff --git a/core/modules/field/field.attach.inc b/core/modules/field/field.attach.inc index d471223..1737045 100644 --- a/core/modules/field/field.attach.inc +++ b/core/modules/field/field.attach.inc @@ -6,8 +6,6 @@ */ use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\entity\Entity\EntityFormDisplay; /** * @defgroup field_attach Field Attach API @@ -249,22 +247,5 @@ function _field_invoke_get_field_definitions($entity_type, $bundle, $options) { } /** - * Defines a 'target function' for field_invoke_method(). - * - * Used to invoke methods on a field's widget. - * - * @param \Drupal\entity\Entity\EntityFormDisplay $form_display - * An EntityFormDisplay object. - * - * @return callable $target_function - * A 'target function' for field_invoke_method(). - */ -function _field_invoke_widget_target($form_display) { - return function (FieldDefinitionInterface $field_definition) use ($form_display) { - return $form_display->getRenderer($field_definition->getName()); - }; -} - -/** * @} End of "defgroup field_attach". */ diff --git a/core/modules/field/field.deprecated.inc b/core/modules/field/field.deprecated.inc index 1e63781..3eedd8f 100644 --- a/core/modules/field/field.deprecated.inc +++ b/core/modules/field/field.deprecated.inc @@ -176,219 +176,6 @@ function field_info_instance($entity_type, $field_name, $bundle_name) { } /** - * Adds form elements for all fields for an entity to a form structure. - * - * The form elements for the entity's fields are added by reference as direct - * children in the $form parameter. This parameter can be a full form structure - * (most common case for entity edit forms), or a sub-element of a larger form. - * - * By default, submitted field values appear at the top-level of - * $form_state['values']. A different location within $form_state['values'] can - * be specified by setting the '#parents' property on the incoming $form - * parameter. Because of name clashes, two instances of the same field cannot - * appear within the same $form element, or within the same '#parents' space. - * - * For each call to field_attach_form(), field values are processed by calling - * field_attach_extract_form_values() on the same $form element. - * - * Sample resulting structure in $form: - * @code - * '#parents' => The location of field values in $form_state['values'], - * '#entity_type' => The name of the entity type, - * '#bundle' => The name of the bundle, - * // One sub-array per field appearing in the entity, keyed by field name. - * // The structure of the array differs slightly depending on whether the - * // widget is 'single-value' (provides the input for one field value, - * // most common case), and will therefore be repeated as many times as - * // needed, or 'multiple-values' (one single widget allows the input of - * // several values, e.g checkboxes, select box...). - * 'field_foo' => array( - * '#access' => TRUE if the current user has 'edit' grants for the field, - * FALSE if not. - * 'widget' => array( - * '#field_name' => The name of the field, - * '#language' => $langcode, - * '#field_parents' => The 'parents' space for the field in the form, - * equal to the #parents property of the $form parameter received by - * field_attach_form(), - * '#required' => Whether or not the field is required, - * '#title' => The label of the field instance, - * '#description' => The description text for the field instance, - * - * // Only for 'single' widgets: - * '#theme' => 'field_multiple_value_form', - * '#cardinality' => The field cardinality, - * '#cardinality_multiple => TRUE if the field can contain multiple items, - * FALSE otherwise. - * // One sub-array per copy of the widget, keyed by delta. - * 0 => array( - * '#entity_type' => The name of the entity type, - * '#bundle' => The name of the bundle, - * '#field_name' => The name of the field, - * '#field_parents' => The 'parents' space for the field in the form, - * equal to the #parents property of the $form parameter received by - * field_attach_form(), - * '#title' => The title to be displayed by the widget, - * '#default_value' => The field value for delta 0, - * '#required' => Whether the widget should be marked required, - * '#delta' => 0, - * // The remaining elements in the sub-array depend on the widget. - * '#type' => The type of the widget, - * ... - * ), - * 1 => array( - * ... - * ), - * - * // Only for multiple widgets: - * '#entity_type' => The name of the entity type, - * '#bundle' => $instance['bundle'], - * // The remaining elements in the sub-array depend on the widget. - * '#type' => The type of the widget, - * ... - * ), - * ... - * ), - * ) - * @endcode - * - * Additionally, some processing data is placed in $form_state, and can be - * accessed by field_form_get_state() and field_form_set_state(). - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity for which to load form elements, used to initialize - * default form values. - * @param $form - * The form structure to fill in. This can be a full form structure, or a - * sub-element of a larger form. The #parents property can be set to control - * the location of submitted field values within $form_state['values']. If - * not specified, $form['#parents'] is set to an empty array, placing field - * values at the top-level of $form_state['values']. - * @param $form_state - * An associative array containing the current state of the form. - * @param $langcode - * The language the field values are going to be entered, if no language - * is provided the default site language will be used. - * @param array $options - * An associative array of additional options. See field_invoke_method() for - * details. - * - * @deprecated as of Drupal 8.0. Use the entity system instead. - * - * @see field_form_get_state() - * @see field_form_set_state() - */ -function field_attach_form(EntityInterface $entity, &$form, &$form_state, $langcode = NULL, array $options = array()) { - // Set #parents to 'top-level' by default. - $form += array('#parents' => array()); - - // Get the entity_form_display object for this form. - $form_display = $form_state['form_display']; - - $form += (array) field_invoke_method('form', _field_invoke_widget_target($form_display), $entity, $form, $form_state, $options); - - $form['#entity_type'] = $entity->getEntityTypeId(); - $form['#bundle'] = $entity->bundle(); - - // Let other modules make changes to the form. - // Avoid \Drupal::moduleHandler()->invokeAll() - // to let parameters be taken by reference. - foreach (\Drupal::moduleHandler()->getImplementations('field_attach_form') as $module) { - $function = $module . '_field_attach_form'; - $function($entity, $form, $form_state, $langcode); - } -} - -/** - * Performs field validation against form-submitted field values. - * - * There are two levels of validation for fields in forms: widget validation and - * and field validation. - * - Widget validation steps are specific to a given widget's own form structure - * and UI metaphors. They are executed through FAPI's #element_validate - * property during normal form validation. - * - Field validation steps are common to a given field type, independently of - * the specific widget being used in a given form. They are defined in the - * field type's implementation of hook_field_validate(). - * - * This function performs field validation in the context of a form submission. - * It converts field validation errors into form errors on the correct form - * elements. Fieldable entity types should call this function during their own - * form validation function. - * - * @param \Drupal\Core\Entity\ContentEntityInterface $entity - * The entity being submitted. The actual field values will be read - * from $form_state['values']. - * @param $form - * The form structure where field elements are attached to. This might be a - * full form structure, or a sub-element of a larger form. - * @param $form_state - * An associative array containing the current state of the form. - * @param array $options - * An associative array of additional options. See field_invoke_method() for - * details. - * - * @deprecated as of Drupal 8.0. Use the entity system instead. - */ -function field_attach_form_validate(ContentEntityInterface $entity, $form, &$form_state, array $options = array()) { - $has_violations = FALSE; - foreach ($entity as $field_name => $field) { - $definition = $field->getDefinition(); - if ($definition->isConfigurable() && (empty($options['field_name']) || $options['field_name'] == $field_name)) { - $field_violations = $field->validate(); - if (count($field_violations)) { - $has_violations = TRUE; - - // Place violations in $form_state. - $field_state = field_form_get_state($form['#parents'], $field_name, $form_state); - $field_state['constraint_violations'] = $field_violations; - field_form_set_state($form['#parents'], $field_name, $form_state, $field_state); - } - } - } - - if ($has_violations) { - // Map errors back to form elements. - $form_display = $form_state['form_display']; - field_invoke_method('flagErrors', _field_invoke_widget_target($form_display), $entity, $form, $form_state, $options); - } -} - -/** - * Populates an entity object with values from a form submission. - * - * Currently, this accounts for drag-and-drop reordering of field values, and - * filtering of empty values. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity being submitted. The actual field values will be read - * from $form_state['values']. - * @param $form - * The form structure where field elements are attached to. This might be a - * full form structure, or a sub-element of a larger form. - * @param $form_state - * An associative array containing the current state of the form. - * @param array $options - * An associative array of additional options. See field_invoke_method() for - * details. - * - * @deprecated as of Drupal 8.0. Use the entity system instead. - */ -function field_attach_extract_form_values(EntityInterface $entity, $form, &$form_state, array $options = array()) { - // Extract field values from submitted values. - $form_display = $form_state['form_display']; - field_invoke_method('extractFormValues', _field_invoke_widget_target($form_display), $entity, $form, $form_state, $options); - - // Let other modules act on submitting the entity. - // Avoid \Drupal::moduleHandler()->invokeAll() - // to let $form_state be taken by reference. - foreach (\Drupal::moduleHandler()->getImplementations('field_attach_extract_form_values') as $module) { - $function = $module . 'field_attach_extract_form_values'; - $function($entity, $form, $form_state); - } -} - -/** * Prepares field data prior to display. * * This function lets field types and formatters load additional data needed for diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php index 8f964e0..7f28d24 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php @@ -306,22 +306,24 @@ function testFieldAttachCache() { } /** - * Test field_attach_form(). + * Test \Drupal\Core\Entity\ContentEntityFormHelper::attachWidgets(). * * This could be much more thorough, but it does verify that the correct * widgets show up. */ - function testFieldAttachForm() { + function testFieldAttachWidgets() { + $entity_form_helper = \Drupal::service('entity.content.form_helper'); + $this->createFieldWithInstance('_2'); $entity_type = 'entity_test'; $entity = entity_create($entity_type, array('id' => 1, 'revision_id' => 1, 'type' => $this->instance->bundle)); - // When generating form for all fields. + // Test generating widgets for all fields. + $display = entity_get_form_display($entity_type, $this->instance->bundle, 'default'); $form = array(); $form_state = form_state_defaults(); - $form_state['form_display'] = entity_get_form_display($entity_type, $this->instance->bundle, 'default'); - field_attach_form($entity, $form, $form_state); + $entity_form_helper->attachWidgets($entity, $display, $form, $form_state); $this->assertEqual($form[$this->field_name]['widget']['#title'], $this->instance->getLabel(), "First field's form title is {$this->instance->getLabel()}"); $this->assertEqual($form[$this->field_name_2]['widget']['#title'], $this->instance_2->getLabel(), "Second field's form title is {$this->instance_2->getLabel()}"); @@ -334,12 +336,16 @@ function testFieldAttachForm() { $this->assertEqual($form[$this->field_name_2]['widget'][$delta]['value']['#type'], 'textfield', "Second field's form delta $delta widget is textfield"); } - // When generating form for a single field (the second field). - $options = array('field_name' => $this->field_name_2); + // Test generating widgets for all fields. + $display = entity_get_form_display($entity_type, $this->instance->bundle, 'default'); + foreach ($display->getComponents() as $name => $options) { + if ($name != $this->field_name_2) { + $display->removeComponent($name); + } + } $form = array(); $form_state = form_state_defaults(); - $form_state['form_display'] = entity_get_form_display($entity_type, $this->instance->bundle, 'default'); - field_attach_form($entity, $form, $form_state, NULL, $options); + $entity_form_helper->attachWidgets($entity, $display, $form, $form_state); $this->assertFalse(isset($form[$this->field_name]), 'The first field does not exist in the form'); $this->assertEqual($form[$this->field_name_2]['widget']['#title'], $this->instance_2->getLabel(), "Second field's form title is {$this->instance_2->getLabel()}"); @@ -350,19 +356,21 @@ function testFieldAttachForm() { } /** - * Test field_attach_extract_form_values(). + * Test \Drupal\Core\Entity\ContentEntityFormHelper::extractWidgetsValues(). */ function testFieldAttachExtractFormValues() { + $entity_form_helper = \Drupal::service('entity.content.form_helper'); + $this->createFieldWithInstance('_2'); $entity_type = 'entity_test'; $entity_init = entity_create($entity_type, array('id' => 1, 'revision_id' => 1, 'type' => $this->instance->bundle)); // Build the form for all fields. + $display = entity_get_form_display($entity_type, $this->instance->bundle, 'default'); $form = array(); $form_state = form_state_defaults(); - $form_state['form_display'] = entity_get_form_display($entity_type, $this->instance->bundle, 'default'); - field_attach_form($entity_init, $form, $form_state); + $entity_form_helper->attachWidgets($entity_init, $display, $form, $form_state); // Simulate incoming values. // First field. @@ -400,9 +408,9 @@ function testFieldAttachExtractFormValues() { $form_state['values'][$this->field_name] = $values; $form_state['values'][$this->field_name_2] = $values_2; - // Call field_attach_extract_form_values() for all fields. + // Extract values for all fields. $entity = clone($entity_init); - field_attach_extract_form_values($entity, $form, $form_state); + $entity_form_helper->extractWidgetsValues($entity, $display, $form, $form_state); asort($weights); asort($weights_2); @@ -422,16 +430,20 @@ function testFieldAttachExtractFormValues() { $this->assertIdentical($entity->{$this->field_name_2}->getValue(), $expected_values_2, 'Submit filters empty values'); // Call field_attach_extract_form_values() for a single field (the second field). - $options = array('field_name' => $this->field_name_2); + foreach ($display->getComponents() as $name => $options) { + if ($name != $this->field_name_2) { + $display->removeComponent($name); + } + } $entity = clone($entity_init); - field_attach_extract_form_values($entity, $form, $form_state, $options); + $entity_form_helper->extractWidgetsValues($entity, $display, $form, $form_state); $expected_values_2 = array(); foreach ($weights_2 as $key => $value) { if ($key != 1) { $expected_values_2[] = array('value' => $values_2[$key]['value']); } } - $this->assertTrue($entity->{$this->field_name}->isEmpty(), 'The first field does is empty in the entity object'); + $this->assertTrue($entity->{$this->field_name}->isEmpty(), 'The first field is empty in the entity object'); $this->assertIdentical($entity->{$this->field_name_2}->getValue(), $expected_values_2, 'Submit filters empty values'); } diff --git a/core/modules/field/lib/Drupal/field/Tests/FormTest.php b/core/modules/field/lib/Drupal/field/Tests/FormTest.php index 81ec2ff..9c0e384 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FormTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FormTest.php @@ -531,10 +531,10 @@ function testFieldFormAccess() { // apart from #access. $entity = entity_create($entity_type, array('id' => 0, 'revision_id' => 0)); + $display = entity_get_form_display($entity_type, $entity_type, 'default'); $form = array(); $form_state = form_state_defaults(); - $form_state['form_display'] = entity_get_form_display($entity_type, $entity_type, 'default'); - field_attach_form($entity, $form, $form_state); + \Drupal::service('entity.content.form_helper')->attachWidgets($entity, $display, $form, $form_state); $this->assertEqual($form[$field_name_no_access]['widget'][0]['value']['#entity_type'], $entity_type, 'The correct entity type is set in the field structure.'); $this->assertFalse($form[$field_name_no_access]['#access'], 'Field #access is FALSE for the field without edit access.'); diff --git a/core/modules/field/tests/modules/field_test/field_test.entity.inc b/core/modules/field/tests/modules/field_test/field_test.entity.inc index af13590..7ce0e03 100644 --- a/core/modules/field/tests/modules/field_test/field_test.entity.inc +++ b/core/modules/field/tests/modules/field_test/field_test.entity.inc @@ -39,85 +39,3 @@ function field_test_entity_info_translatable($entity_type = NULL, $translatable } return $stored_value; } - -/** - * Form combining two separate entities. - * - * @deprecated Use \Drupal\field_test\Form\FieldTestForm::testEntityNestedForm() - */ -function field_test_entity_nested_form($form, &$form_state, EntityInterface $entity_1, EntityInterface $entity_2) { - // First entity. - foreach (array('id', 'type') as $key) { - $form[$key] = array( - '#type' => 'value', - '#value' => $entity_1->$key->value, - ); - } - $form_state['form_display'] = entity_get_form_display($entity_1->getEntityTypeId(), $entity_1->bundle(), 'default'); - field_attach_form($entity_1, $form, $form_state); - - // Second entity. - $form['entity_2'] = array( - '#type' => 'details', - '#title' => t('Second entity'), - '#tree' => TRUE, - '#parents' => array('entity_2'), - '#weight' => 50, - ); - foreach (array('id', 'type') as $key) { - $form['entity_2'][$key] = array( - '#type' => 'value', - '#value' => $entity_2->$key->value, - ); - } - $form_state['form_display'] = entity_get_form_display($entity_1->getEntityTypeId(), $entity_1->bundle(), 'default'); - field_attach_form($entity_2, $form['entity_2'], $form_state); - - $form['save'] = array( - '#type' => 'submit', - '#value' => t('Save'), - '#weight' => 100, - ); - - return $form; -} - -/** - * Validate handler for field_test_entity_nested_form(). - */ -function field_test_entity_nested_form_validate($form, &$form_state) { - $entity_1 = entity_create('entity_test', array( - 'id' => $form_state['values']['id'], - 'type' => $form_state['values']['type'], - )); - field_attach_extract_form_values($entity_1, $form, $form_state); - field_attach_form_validate($entity_1, $form, $form_state); - - $entity_2 = entity_create('entity_test', array( - 'id' => $form_state['values']['entity_2']['id'], - 'type' => $form_state['values']['entity_2']['type'], - )); - field_attach_extract_form_values($entity_2, $form['entity_2'], $form_state); - field_attach_form_validate($entity_2, $form['entity_2'], $form_state); -} - -/** - * Submit handler for field_test_entity_nested_form(). - */ -function field_test_entity_nested_form_submit($form, &$form_state) { - $entity_1 = entity_create('entity_test', array( - 'id' => $form_state['values']['id'], - 'type' => $form_state['values']['type'], - )); - field_attach_extract_form_values($entity_1, $form, $form_state); - $entity_1->save(); - - $entity_2 = entity_create('entity_test', array( - 'id' => $form_state['values']['entity_2']['id'], - 'type' => $form_state['values']['entity_2']['type'], - )); - field_attach_extract_form_values($entity_2, $form['entity_2'], $form_state); - $entity_2->save(); - - drupal_set_message(t('test_entities @id_1 and @id_2 have been updated.', array('@id_1' => $entity_1->id(), '@id_2' => $entity_2->id()))); -} diff --git a/core/modules/field/tests/modules/field_test/field_test.routing.yml b/core/modules/field/tests/modules/field_test/field_test.routing.yml index b7ba00c..cfd60fa 100644 --- a/core/modules/field/tests/modules/field_test/field_test.routing.yml +++ b/core/modules/field/tests/modules/field_test/field_test.routing.yml @@ -2,7 +2,7 @@ field_test.entity_nested_form: path: '/test-entity/nested/{entity_1}/{entity_2}' defaults: _title: 'Nested entity form' - _content: '\Drupal\field_test\Form\FieldTestForm::testEntityNestedForm' + _form: '\Drupal\field_test\Form\NestedEntityTestForm' options: parameters: entity_1: diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Form/FieldTestForm.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Form/FieldTestForm.php deleted file mode 100644 index 8c9e913..0000000 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Form/FieldTestForm.php +++ /dev/null @@ -1,25 +0,0 @@ -entityFormHelper = $entity_form_helper; + } + + /** + * {@inheritdoc] + */ + public static function create(ContainerInterface $container) { + return new static($container->get('entity.content.form_helper')); + } + + /** + * {@inheritdoc] + */ + public function getFormId() { + return 'field_test_entity_nested_form'; + } + + /** + * {@inheritdoc] + */ + public function buildForm(array $form, array &$form_state, EntityInterface $entity_1 = NULL, EntityInterface $entity_2 = NULL) { + // First entity. + $form_state['entity_1'] = $entity_1; + $form_state['form_display_1'] = entity_get_render_form_display($entity_1, 'default'); + $this->entityFormHelper->attachWidgets($entity_1, $form_state['form_display_1'], $form, $form_state); + + // Second entity. + $form_state['entity_2'] = $entity_2; + $form_state['form_display_2'] = entity_get_render_form_display($entity_2, 'default'); + $form['entity_2'] = array( + '#type' => 'details', + '#title' => t('Second entity'), + '#tree' => TRUE, + '#parents' => array('entity_2'), + '#weight' => 50, + ); + $this->entityFormHelper->attachWidgets($entity_2, $form_state['form_display_2'], $form['entity_2'], $form_state); + + $form['save'] = array( + '#type' => 'submit', + '#value' => t('Save'), + '#weight' => 100, + ); + + return $form; + } + + /** + * {@inheritdoc] + */ + public function validateForm(array &$form, array &$form_state) { + $entity_1 = $form_state['entity_1']; + $this->entityFormHelper->extractWidgetsValues($entity_1, $form_state['form_display_1'], $form, $form_state); + $this->entityFormHelper->validateWidgetsValues($entity_1, $form_state['form_display_1'], $form, $form_state); + + $entity_2 = $form_state['entity_2']; + $this->entityFormHelper->extractWidgetsValues($entity_2, $form_state['form_display_2'], $form['entity_2'], $form_state); + $this->entityFormHelper->validateWidgetsValues($entity_2, $form_state['form_display_2'], $form['entity_2'], $form_state); + } + + /** + * {@inheritdoc] + */ + public function submitForm(array &$form, array &$form_state) { + $entity_1 = $form_state['entity_1']; + $entity_1->save(); + + $entity_2 = $form_state['entity_2']; + $entity_2->save(); + + drupal_set_message($this->t('test_entities @id_1 and @id_2 have been updated.', array('@id_1' => $entity_1->id(), '@id_2' => $entity_2->id()))); + } + +} diff --git a/core/modules/menu/menu.module b/core/modules/menu/menu.module index 64a171b..cc31bb4 100644 --- a/core/modules/menu/menu.module +++ b/core/modules/menu/menu.module @@ -420,7 +420,7 @@ function menu_node_predelete(EntityInterface $node) { /** * Implements hook_node_prepare_form(). */ -function menu_node_prepare_form(NodeInterface $node, $form_display, $operation, array &$form_state) { +function menu_node_prepare_form(NodeInterface $node, $operation, array &$form_state) { if (empty($node->menu)) { // Prepare the node for the edit form so that $node->menu always exists. $node_type_config = \Drupal::config('menu.entity.node.' . $node->getType()); diff --git a/core/modules/node/lib/Drupal/node/Form/NodeDeleteForm.php b/core/modules/node/lib/Drupal/node/Form/NodeDeleteForm.php index 93148d4..0b8ca77 100644 --- a/core/modules/node/lib/Drupal/node/Form/NodeDeleteForm.php +++ b/core/modules/node/lib/Drupal/node/Form/NodeDeleteForm.php @@ -9,6 +9,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\ContentEntityConfirmFormBase; +use Drupal\Core\Entity\ContentEntityFormHelper; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Routing\UrlGeneratorInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -30,11 +31,13 @@ class NodeDeleteForm extends ContentEntityConfirmFormBase { * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * The URL generator. */ - public function __construct(EntityManagerInterface $entity_manager, UrlGeneratorInterface $url_generator) { - parent::__construct($entity_manager); + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper, UrlGeneratorInterface $url_generator) { + parent::__construct($entity_manager, $entity_form_helper); $this->urlGenerator = $url_generator; } @@ -44,6 +47,7 @@ public function __construct(EntityManagerInterface $entity_manager, UrlGenerator public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), + $container->get('entity.content.form_helper'), $container->get('url_generator') ); } diff --git a/core/modules/node/node.api.php b/core/modules/node/node.api.php index d53673c..cde8286 100644 --- a/core/modules/node/node.api.php +++ b/core/modules/node/node.api.php @@ -32,13 +32,12 @@ * - Entity hooks: Generic hooks for "entity" operations. These are always * invoked on all modules. * - * Here is a list of the node and entity hooks that are invoked, field - * operations, and other steps that take place during node operations: + * Here is a list of the node and entity hooks that are invoked, and other + * steps that take place during node operations: * - Instantiating a new node: * - hook_node_create() (all) * - hook_entity_create() (all) * - Creating a new node (calling $node->save() on a new node): - * - field_attach_presave() * - hook_node_presave() (all) * - hook_entity_presave() (all) * - Node and revision records are written to the database @@ -47,7 +46,6 @@ * - hook_node_access_records() (all) * - hook_node_access_records_alter() (all) * - Updating an existing node (calling $node->save() on an existing node): - * - field_attach_presave() * - hook_node_presave() (all) * - hook_entity_presave() (all) * - Node and revision records are written to the database @@ -94,7 +92,6 @@ * existing node, it will already be loaded; see the Loading section above): * - hook_node_prepare_form() (all) * - hook_entity_prepare_form() (all) - * - field_attach_form() * - Validating a node during editing form submit (calling * node_form_validate()): * - hook_node_validate() (all) @@ -601,8 +598,6 @@ function hook_node_access(\Drupal\node\NodeInterface $node, $op, $account, $lang * * @param \Drupal\node\NodeInterface $node * The node that is about to be shown on the form. - * @param $form_display - * The current form display. * @param $operation * The current operation. * @param array $form_state @@ -610,7 +605,7 @@ function hook_node_access(\Drupal\node\NodeInterface $node, $op, $account, $lang * * @ingroup node_api_hooks */ -function hook_node_prepare_form(\Drupal\node\NodeInterface $node, $form_display, $operation, array &$form_state) { +function hook_node_prepare_form(\Drupal\node\NodeInterface $node, $operation, array &$form_state) { if (!isset($node->my_rating)) { $node->my_rating = \Drupal::config("my_rating_{$node->bundle()}")->get('enabled'); } diff --git a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutFormController.php b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutFormController.php index 5f72679..85464d7 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/ShortcutFormController.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/ShortcutFormController.php @@ -8,6 +8,7 @@ namespace Drupal\shortcut; use Drupal\Core\Entity\ContentEntityFormController; +use Drupal\Core\Entity\ContentEntityFormHelper; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Form\FormBuilderInterface; @@ -47,6 +48,8 @@ class ShortcutFormController extends ContentEntityFormController { * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager * The path alias manager. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator @@ -54,8 +57,8 @@ class ShortcutFormController extends ContentEntityFormController { * @param \Drupal\Core\Form\FormBuilderInterface $form_builder * The form builder. */ - public function __construct(EntityManagerInterface $entity_manager, AliasManagerInterface $alias_manager, UrlGeneratorInterface $url_generator, FormBuilderInterface $form_builder) { - $this->entityManager = $entity_manager; + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper, AliasManagerInterface $alias_manager, UrlGeneratorInterface $url_generator, FormBuilderInterface $form_builder) { + parent::__construct($entity_manager, $entity_form_helper); $this->aliasManager = $alias_manager; $this->urlGenerator = $url_generator; $this->formBuilder = $form_builder; @@ -67,6 +70,7 @@ public function __construct(EntityManagerInterface $entity_manager, AliasManager public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), + $container->get('entity.content.form_helper'), $container->get('path.alias_manager'), $container->get('url_generator'), $container->get('form_builder') diff --git a/core/modules/system/entity.api.php b/core/modules/system/entity.api.php index 4ea960c..3905040 100644 --- a/core/modules/system/entity.api.php +++ b/core/modules/system/entity.api.php @@ -592,8 +592,6 @@ function hook_entity_display_alter(\Drupal\Core\Entity\Display\EntityViewDisplay * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity that is about to be shown on the form. - * @param $form_display - * The current form display. * @param $operation * The current operation. * @param array $form_state @@ -601,7 +599,7 @@ function hook_entity_display_alter(\Drupal\Core\Entity\Display\EntityViewDisplay * * @see \Drupal\Core\Entity\EntityFormController::prepareEntity() */ -function hook_entity_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $form_display, $operation, array &$form_state) { +function hook_entity_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, array &$form_state) { if ($operation == 'edit') { $entity->label->value = 'Altered label'; $form_state['mymodule']['label_altered'] = TRUE; diff --git a/core/modules/system/lib/Drupal/system/Tests/Form/ArbitraryRebuildTest.php b/core/modules/system/lib/Drupal/system/Tests/Form/ArbitraryRebuildTest.php index b09fc9b..decdde5 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Form/ArbitraryRebuildTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Form/ArbitraryRebuildTest.php @@ -76,7 +76,7 @@ function testUserRegistrationMultipleField() { 'name' => 'foo', 'mail' => 'bar@example.com', ); - $this->drupalPostForm('user/register', $edit, t('Add another item'), array('query' => array('field' => TRUE))); + $this->drupalPostForm('user/register', $edit, t('Add another item')); $this->assertText('Test a multiple valued field', 'Form has been rebuilt.'); $this->assertFieldByName('name', 'foo', 'Entered user name has been kept.'); $this->assertFieldByName('mail', 'bar@example.com', 'Entered mail address has been kept.'); diff --git a/core/modules/system/tests/modules/form_test/form_test.module b/core/modules/system/tests/modules/form_test/form_test.module index 922bc78..c3c76be 100644 --- a/core/modules/system/tests/modules/form_test/form_test.module +++ b/core/modules/system/tests/modules/form_test/form_test.module @@ -1972,13 +1972,6 @@ function form_test_form_user_register_form_alter(&$form, &$form_state) { '#value' => t('Rebuild'), '#submit' => array('form_test_user_register_form_rebuild'), ); - // If requested, add the test field by attaching the node page form. - if (\Drupal::request()->request->has('field')) { - $node = entity_create('node', array( - 'type' => 'page', - )); - field_attach_form($node, $form, $form_state); - } } /** diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/TermFormController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/TermFormController.php index 07db84e..ee20c44 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/TermFormController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/TermFormController.php @@ -10,6 +10,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Entity\ContentEntityFormController; +use Drupal\Core\Entity\ContentEntityFormHelper; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Language\Language; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -31,11 +32,13 @@ class TermFormController extends ContentEntityFormController { * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. * @param \Drupal\Core\Config\ConfigFactory $config_factory * The config factory. */ - public function __construct(EntityManagerInterface $entity_manager, ConfigFactory $config_factory) { - parent::__construct($entity_manager); + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper, ConfigFactory $config_factory) { + parent::__construct($entity_manager, $entity_form_helper); $this->configFactory = $config_factory; } @@ -45,6 +48,7 @@ public function __construct(EntityManagerInterface $entity_manager, ConfigFactor public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), + $container->get('entity.content.form_helper'), $container->get('config.factory') ); } diff --git a/core/modules/user/lib/Drupal/user/AccountFormController.php b/core/modules/user/lib/Drupal/user/AccountFormController.php index 6002298..f1c8b92 100644 --- a/core/modules/user/lib/Drupal/user/AccountFormController.php +++ b/core/modules/user/lib/Drupal/user/AccountFormController.php @@ -8,6 +8,7 @@ namespace Drupal\user; use Drupal\Core\Entity\ContentEntityFormController; +use Drupal\Core\Entity\ContentEntityFormHelper; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Language\Language; @@ -41,13 +42,15 @@ * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. * @param \Drupal\Core\Entity\Query\QueryFactory * The entity query factory. */ - public function __construct(EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, QueryFactory $entity_query) { - parent::__construct($entity_manager); + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper, LanguageManagerInterface $language_manager, QueryFactory $entity_query) { + parent::__construct($entity_manager, $entity_form_helper); $this->languageManager = $language_manager; $this->entityQuery = $entity_query; } @@ -58,6 +61,7 @@ public function __construct(EntityManagerInterface $entity_manager, LanguageMana public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), + $container->get('entity.content.form_helper'), $container->get('language_manager'), $container->get('entity.query') ); diff --git a/core/modules/user/lib/Drupal/user/Form/UserCancelForm.php b/core/modules/user/lib/Drupal/user/Form/UserCancelForm.php index 5d14e09..74b7030 100644 --- a/core/modules/user/lib/Drupal/user/Form/UserCancelForm.php +++ b/core/modules/user/lib/Drupal/user/Form/UserCancelForm.php @@ -9,6 +9,7 @@ use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Entity\ContentEntityConfirmFormBase; +use Drupal\Core\Entity\ContentEntityFormHelper; use Drupal\Core\Entity\EntityManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -43,11 +44,13 @@ class UserCancelForm extends ContentEntityConfirmFormBase { * * @param \Drupal\Core\Config\ConfigFactory $config_factory * The config factory. + * @param \Drupal\Core\Entity\ContentEntityFormHelper $entity_form_helper + * The ContentEntity form helper. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. */ - public function __construct(EntityManagerInterface $entity_manager, ConfigFactory $config_factory) { - parent::__construct($entity_manager); + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper, ConfigFactory $config_factory) { + parent::__construct($entity_manager, $entity_form_helper); $this->configFactory = $config_factory; } @@ -57,6 +60,7 @@ public function __construct(EntityManagerInterface $entity_manager, ConfigFactor public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), + $container->get('entity.content.form_helper'), $container->get('config.factory') ); } diff --git a/core/modules/user/lib/Drupal/user/ProfileFormController.php b/core/modules/user/lib/Drupal/user/ProfileFormController.php index 325f02b..5134e6d 100644 --- a/core/modules/user/lib/Drupal/user/ProfileFormController.php +++ b/core/modules/user/lib/Drupal/user/ProfileFormController.php @@ -11,6 +11,8 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Language\LanguageManager; +use Drupal\Core\Entity\ContentEntityFormHelper; + /** * Form controller for the profile forms. @@ -20,8 +22,8 @@ class ProfileFormController extends AccountFormController { /** * {@inheritdoc} */ - public function __construct(EntityManagerInterface $entity_manager, LanguageManager $language_manager, QueryFactory $entity_query) { - parent::__construct($entity_manager, $language_manager, $entity_query); + public function __construct(EntityManagerInterface $entity_manager, ContentEntityFormHelper $entity_form_helper, LanguageManager $language_manager, QueryFactory $entity_query) { + parent::__construct($entity_manager, $entity_form_helper, $language_manager, $entity_query); } /** diff --git a/core/modules/user/lib/Drupal/user/RegisterFormController.php b/core/modules/user/lib/Drupal/user/RegisterFormController.php index ba95171..f676bc2 100644 --- a/core/modules/user/lib/Drupal/user/RegisterFormController.php +++ b/core/modules/user/lib/Drupal/user/RegisterFormController.php @@ -50,9 +50,6 @@ public function form(array $form, array &$form_state) { // Start with the default user account fields. $form = parent::form($form, $form_state, $account); - // Attach field widgets. - field_attach_form($account, $form, $form_state); - if ($admin) { // Redirect back to page which initiated the create request; usually // admin/people/create.