-
-
Save davebeach/0e01e9a089bc4770022f5c74e85eab53 to your computer and use it in GitHub Desktop.
Extend drupal 8 user register form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\my_module\Form; | |
use Drupal\Component\Datetime\TimeInterface; | |
use Drupal\Core\Entity\EntityFormBuilderInterface; | |
use Drupal\Core\Entity\EntityManagerInterface; | |
use Drupal\Core\Entity\EntityTypeBundleInfoInterface; | |
use Drupal\Core\Extension\ModuleHandlerInterface; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Language\LanguageManagerInterface; | |
use Drupal\user\Entity\User; | |
use Drupal\user\RegisterForm; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Provides a user login form. | |
*/ | |
class NewUserRegisterForm extends RegisterForm { | |
public function __construct(EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, ModuleHandlerInterface $moduleHandler) { | |
$this->setEntity(new User([], 'user')); | |
$this->setModuleHandler($moduleHandler); | |
parent::__construct($entity_manager, $language_manager, $entity_type_bundle_info, $time); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('entity.manager'), | |
$container->get('language_manager'), | |
$container->get('entity_type.bundle.info'), | |
$container->get('datetime.time'), | |
$container->get('module_handler') | |
); | |
} | |
public function form(array $form, FormStateInterface $form_state) { | |
$form = parent::form($form, $form_state); | |
$form['test'] = [ | |
'#markup' => '<p>Test extended form</p>', | |
]; | |
return $form; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\my_module\Routing; | |
use Drupal\Core\Routing\RouteSubscriberBase; | |
use Symfony\Component\Routing\RouteCollection; | |
/** | |
* Listens to the dynamic route events. | |
*/ | |
class RouteSubscriber extends RouteSubscriberBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function alterRoutes(RouteCollection $collection) { | |
if ($route = $collection->get('user.register')) { | |
$route->setDefault('_form', '\Drupal\my_module\Form\NewUserRegisterForm'); | |
} | |
} | |
} |
I am getting
The website encountered an unexpected error. Please try again later.
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'uuid' cannot be null: INSERT INTO {users} (uid, uuid, langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array
(
[:db_insert_placeholder_0] => 26
[:db_insert_placeholder_1] =>
[:db_insert_placeholder_2] => en
)
When I try submit the form? Any suggestions?
For those how have the same problem, this is the solution :
-> You need to create a new instance of user in the contructor as this below
public function __construct(
EntityRepositoryInterface $entity_repository,
LanguageManagerInterface $language_manager,
EntityTypeBundleInfoInterface $entityTypeBundleInfo = NULL,
TimeInterface $time = NULL,
ModuleHandlerInterface $moduleHandler
) {
$user = User::create();
$this->setEntity($user);
$this->setModuleHandler($moduleHandler);
parent::__construct($entity_repository, $language_manager, $entityTypeBundleInfo, $time);
}
Hope it will help !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is actually a bit of a hack. You shouldn't need to implement __constuct() and create() if you're just changing markup.
If all you want to do is extend the form but not override the route, implement hook_entity_type_alter in your .module file like so:
And in your NewRegisterForm.php you don't need to implement __construct or create, as it inherits those from RegisterForm.php. In my case, I just wanted to change the submit button text, so I have:
The class that is used to build the form is set on the User object in annotations in User.php, so you need to change it there.