我尝试构建一个自定义注册表单,应该显示在自定义块中,我不想通过钩子或使用类似form_block的扩展来插入普通的注册表单并进行修改,因为我想学习Drupal 8如何处理表单。
我的块看起来像这样:
<?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;
}
}
我的自定义注册表单长这样:
<?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);
}
}
我刚试图扩展普通的RegisterForm
,在第一步中,我只想返回父表单以查看它是否有效。但是它没有...
错误信息:
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
我认为这是表单中缺少的用户实体,但我不知道如何在我的表单中“放置”这个实体。