I want to display the register form in a modal.
For that, I created a custom plugin block which returns the rendering of the form register.
/**
* {@inheritdoc}
*/
public function build() {
$entity = \Drupal::entityTypeManager()->getStorage('user')->create(array());
$formObject = \Drupal::entityTypeManager()
->getFormObject('user', 'register')
->setEntity($entity);
return $this->formBuilder->getForm($formObject);
}
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
if ($account->isAnonymous()) {
return AccessResult::allowed()
->addCacheContexts(['route.name', 'user.roles:anonymous']);
}
return AccessResult::forbidden();
}
I am using ajax command for the modal rendering process and in it I have this code:
// Create an instance of the block.
$plugin_block = $this->blockManager->createInstance($block_id);
// Some blocks might implement access check.
$access_result = $plugin_block->access($this->currentUser);
// Return empty render array if user doesn't have access.
// $access_result can be boolean or an AccessResult class.
if (is_object($access_result) && $access_result->isForbidden() || is_bool($access_result) && !$access_result) {
$modal_content = "";
}
else {
$build_block = $plugin_block->build();
$modal_content = $this->renderer->render($build_block);
}
My problem is that the block is well rendered in the modal with the form but that the form does not work. And when I made a comparison with a normal rendering of the block (eg directly in a region of the page via the admin block layout), the difference is that in the modal rendering, I have like action="form_action_p_pvdeGsVG5zNF_XLGPTvYSKCf43t8qZYSwcfZl2uzM"
Anyone have an idea or a better solution?