Drupal 8自定义注册表

时间:2016-06-03 17:55:05

标签: php symfony drupal drupal-8 drupal-forms

我尝试构建一个自定义注册表单,该表单应该显示在自定义块中,我不想插入正常的注册表单并通过钩子更改它或使用像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

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

2 个答案:

答案 0 :(得分:2)

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

我把我的块改成了这样的东西:

<?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逻辑(以邮件输入描述为例)

答案 1 :(得分:0)

我已将此块放置在首页内容中,但它不可见

<span data-big-pipe-placeholder-id="callback=Drupal%5Cblock%5CBlockViewBuilder%3A%3AlazyBuilder&amp;args%5B0%5D=userregistrationform&amp;args%5B1%5D=full&amp;args%5B2%5D&amp;token=-MyZMBSv_tseO1_TExVECdGUQnyGrlvE9eST64je7Ho"></span>

它像这样显示

仅供参考,我已将权限更改为isAnonymous()