Drupal 8 custom registration form

bambamboole

I try to build a custom registration form which should be displayed in a custom block and I don't want to insert the normal registration form and alter it via a hook or use an extension like form_block, because I want to learn the ways how drupal 8 works with forms.

My block looks like this:

<?php

namespace Drupal\ajax_registration_form\Plugin\Block;

use Drupal\ajax_registration_form\Form\AjaxRegistrationForm;
use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'AjaxRegistrationBlock' block.
 *
 * @Block(
 *  id = "ajax_registration_block",
 *  admin_label = @Translation("Ajax registration block"),
 * )
 */
class AjaxRegistrationBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {

    $content = \Drupal::formBuilder()->getForm(AjaxRegistrationForm::class);

    return $content;
  }

}

My custom registration form looks like this:

<?php

namespace Drupal\ajax_registration_form\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\user\RegisterForm;



class AjaxRegistrationForm extends RegisterForm {


  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {

    return parent::form($form, $form_state);
  }
}

I just tried to extend the normal RegisterForm and in the first step I just wanted to return the parent form to see if it works. But it doesn't...

Error message:

Fatal error: Call to a member function getEntityTypeId() on null in /Users/*******/Sites/priv/radweiser/web/core/lib/Drupal/Core/Entity/EntityForm.php on line 77

I think it's the missing user entity in the form, but I don't know how I can "put" this entity in my form.

bambamboole

I found the solution in the code of the formblock module.

I altered my block to something like this:

<?php

namespace Drupal\ajax_registration_form\Plugin\Block;

use Drupal\Core\Annotation\Translation;
use Drupal\Core\Block\Annotation\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a 'AjaxRegistrationBlock' block.
 *
 * @Block(
 *  id = "ajax_registration_block",
 *  admin_label = @Translation("Ajax registration block"),
 * )
 */
class AjaxRegistrationBlock extends BlockBase implements ContainerFactoryPluginInterface {


  /**
   * The entity manager
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface.
   */
  protected $entityManager;

  /**
   * The entity form builder
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface.
   */
  protected $entityFormBuilder;

  /**
   * Constructs a new UserRegisterBlock plugin
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
   *   The entity manager.
   * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entityFormBuilder
   *   The entity form builder.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entityManager, EntityFormBuilderInterface $entityFormBuilder) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityManager = $entityManager;
    $this->entityFormBuilder = $entityFormBuilder;
  }

  /**
   * @inheritdoc
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity.manager'),
      $container->get('entity.form_builder')
    );
  }


  /**
   * Implements \Drupal\block\BlockBase::build().
   */
  public function build() {
    $build = array();

    $account = $this->entityManager->getStorage('user') ->create(array());
    $build['form'] = $this->entityFormBuilder->getForm($account, 'register');

    $build['form']['account']['mail']['#description'] = t('');
    kint($build['form']['account']);

    return $build;
  }

  /**
   *Implements \Drupal\block\BlockBase::blockAccess().
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *
   * @return bool|\Drupal\Core\Access\AccessResult
   */
  public function blockAccess(AccountInterface $account) {
    return ($account->isAnonymous()) && (\Drupal::config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY);
  }

}

Now I can alter the form and implement ajax logic using the Form Api (alter the mail input description as example)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Drupal Custom registration form save

From Dev

Drupal custom registration Form element not showing on frontend

From Dev

Drupal 8 Custom form with popup

From Dev

Django custom registration form

From Dev

Change and add fields to registration form drupal 7

From Dev

Django Registration Redux custom registration form

From Dev

Drupal custom form field permission

From Dev

Wordpress | Woocommerce custom registration form

From Dev

Custom Wordpress Registration Form with Validation

From Dev

Remove custom registration form on wordpress

From Dev

Laravel custom validation on registration form

From Dev

Manipulate form element in Drupal 8

From Dev

ckeditor in Drupal 8 custom module

From Dev

Drupal 8 Custom Module Libraries

From Dev

Devise custom validation for a custom field in the registration form

From Dev

how to add registration form in drupal block in a popup box in home page

From Dev

How to customise drupal 7 user registration, login, password reset form

From Dev

Adding Drupal Field Collection element to a custom form

From Dev

Drupal 7 Form api Custom javascript with Jquery

From Dev

how to add custom HTML form in drupal

From Dev

Creating Custom user registration form Django

From Dev

Creating a custom registration form in Google Course Builder

From Dev

Eventbrite API - custom form registration in website

From Dev

Drupal 8, how build a hierarchical form?

From Dev

Drupal 8 custom module add php classes

From Dev

How to get custom block content in drupal 8

From Dev

Custom 404 template file in Drupal 8

From Dev

Drupal 8 Custom Field // Primitive error

From Dev

Drupal 8 custom menu item different URI

Related Related

HotTag

Archive