Score:1

Register form block into modal

de flag

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?

in flag
it sounds like you might be trying to display a form within a form, which Drupal doesn't like. If the modal is set up such that it's markup is rendered within a parent form, then, effectively, the modal form's `<form>` element won't be rendered. If this is the case, see if you can render the modal form outside of the parent form's markup and, as needed, use CSS to position it as you like.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.