php - Drupal 8 自定义注册表单

标签 php symfony drupal drupal-8 drupal-forms

我尝试构建一个应该显示在自定义 block 中的自定义注册表单,我不想插入普通注册表单并通过钩子(Hook)更改它或使用像 form_block 这样的扩展,因为我想了解如何使用 drupal 8 与表格一起工作。

我的 block 看起来像这样:

<?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

我认为这是表单中缺少的用户实体,但我不知道如何将这个实体“放入”我的表单中。

最佳答案

我在 formblock 的代码中找到了解决方案模块。

我把我的 block 改成这样:

<?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);
  }

}

现在我可以使用 Form Api 更改表单并实现 ajax 逻辑(例如更改邮件输入描述)

关于php - Drupal 8 自定义注册表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37620708/

相关文章:

php - 在 ubuntu 中将 symfony 命令作为服务运行

drupal - 如何创建自定义访问功能以在 Drupal 中编辑节点类型?

php - Float left 在 Drupal 表单中无法正常工作

php - 由于出站端口被阻止,无法访问 phpMyAdmin

GitHub:是否可以从仓库中获取仓库?

php - 如何从数据库中检索数据并能够在summernote编辑器中进行编辑?

php - Symfony2 翻译验证消息问题

php - 对于程序员团队来说,哪种企业级开源 CMS 的学习曲线最陡(最简单)?

php - 使用 PHP 检查数据库的时间间隔

php - 如果收到 x 则更新列,否则更新 y