diff --git a/inline_entity_form.module b/inline_entity_form.module index 9f06bec..6ecfe5e 100644 --- a/inline_entity_form.module +++ b/inline_entity_form.module @@ -29,6 +29,10 @@ function inline_entity_form_entity_type_build(array &$entity_types) { $entity_types['node']->setHandlerClass('inline_form', '\Drupal\inline_entity_form\Form\NodeInlineForm'); } + if (isset($entity_types['user']) && !$entity_types['user']->getHandlerClass('inline_form')) { + $entity_types['user']->setHandlerClass('inline_form', '\Drupal\inline_entity_form\Form\UserInlineForm'); + } + foreach ($entity_types as &$entity_type) { if (!$entity_type->hasHandlerClass('inline_form')) { $entity_type->setHandlerClass('inline_form', '\Drupal\inline_entity_form\Form\EntityInlineForm'); diff --git a/p.patch b/p.patch new file mode 100644 index 0000000..e69de29 diff --git a/src/Form/UserInlineForm.php b/src/Form/UserInlineForm.php new file mode 100644 index 0000000..f53f05f --- /dev/null +++ b/src/Form/UserInlineForm.php @@ -0,0 +1,82 @@ +<?php + +namespace Drupal\inline_entity_form\Form; + +use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Render\Element; + +/** + * User inline form handler. + */ +class UserInlineForm extends EntityInlineForm { + + /** + * {@inheritdoc} + */ + public function getEntityTypeLabels() { + return [ + 'singular' => $this->entityType->getSingularLabel(), + 'plural' => $this->entityType->getPluralLabel(), + ]; + } + + /** + * {@inheritdoc} + */ + public function entityForm(array $entity_form, FormStateInterface $form_state) { + $user_form_object = $this->getUserFormObject($entity_form['#entity'], $form_state, $entity_form['#form_mode']); + + // Copy all standard user form fields which are not rendered by the + // widgets into the main entity form object. + $user_form = $user_form_object->form($entity_form, $form_state); + foreach (Element::children($user_form) as $field_name) { + $entity_form[$field_name] = $user_form[$field_name]; + } + + return parent::entityForm($entity_form, $form_state); + } + + /** + * Builds an updated entity object based upon the submitted form values. + */ + protected function buildEntity(array $entity_form, ContentEntityInterface $entity, FormStateInterface $form_state) { + parent::buildEntity($entity_form, $entity, $form_state); + + // Use standard user form handlers to copy values from the form state + // into the user entity object. + // Note that we need to copy each value of the object separately to + // make sure that all references to this entity persist. + $user_form_object = $this->getUserFormObject($entity, $form_state, $entity_form['#form_mode']); + $user_entity = $user_form_object->buildEntity($entity_form, $form_state); + foreach ($user_entity->getFields() as $field_name => $field_value) { + $entity->set($field_name, $field_value->getValue()); + } + } + + /** + * Internal helper. + * + * Returns handler for the user form. + * + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * User account entity. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Current form state. + * @param $form_mode + * User form mode (usually it's "register"). + * + * @return \Drupal\user\RegisterForm + */ + protected function getUserFormObject(ContentEntityInterface $entity, FormStateInterface $form_state, $form_mode) { + /** @var \Drupal\user\RegisterForm $user_form_object */ + $user_form_object = $this->entityTypeManager->getFormObject($entity->getEntityTypeId(), $form_mode); + $user_form_object->setEntity($entity); + + $form_display = $this->getFormDisplay($entity, $form_mode); + $user_form_object->setFormDisplay($form_display, $form_state); + + return $user_form_object; + } + +}