Drupal 8 使用 display_form 模式

Cif*_*ren 1 php forms drupal block drupal-8

我正在尝试使用表单显示模式创建一个带有表单的块/页面

看起来很棒,即使是权限字段似乎对我想做的事情也有好处,但挑战并不存在......

我希望能够使用 form_display 模式创建一个带有表单的块,当然还使用核心处理程序来保存它。

我找到了一些来源,但我不太确定在哪里打

任何人都可以给我一个提示?

Cif*_*ren 5

问题中的所有部分。

我将展示一个关于用户实体的例子,使用我创建的模块

步骤将是:

  • 备份!!
  • 在管理界面中配置显示表单模式
  • 创建自定义模块
  • 为我们的实体用户设置我们的 formClass
  • 创建块类
  • 将块添加到您的页面
  • 享受 !!(再次备份!!永远不知道)

1 - 配置显示表单模式

转到此页面并按照步骤操作。它会非常快,就像输入一个字段一样......简单!!请保留您将创建的项目的 machine_name,我的是nouvea。将显示模式激活为“配置 > 用户参数 > 管理字段 > 高级设置”

2 - 创建自定义模块

转到这个页面,这里没什么可做的

  • 创建文件夹
  • 放一个 module_name.info.yml
  • 另一个module_name.module在里面,我们将在之后使用这个文件
  • 完毕 !

3 - 将我们的 formClass 设置为我们的实体用户

正如在此评论中所说,您需要为您的实体 User Add 声明您的表单类到您的 module_name.module

<?php 


/**
 * Implements hook_entity_type_build().
 */ 
function form_display_block_entity_type_build(array &$entity_types) {
  $entity_types['user']->setFormClass('nouvea', 'Drupal\user\RegisterForm');
}
Run Code Online (Sandbox Code Playgroud)

有关hook_entity_type_build() 的更多详细信息

我正在使用“Drupal\user\RegisterForm”,因为它是唯一一种我可以快速轻松地访问它而无需创建自己的表单(不确定后果......)

4 - 创建块类

我们将使用用户注册表单将创建的类复制到formblock 模块中

我将这个类添加到我的“模块文件夹 > src > 插件 > 块”中

<?php

namespace Drupal\form_display_block\Plugin\Block;

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

/**
 * Provides a block for the coach registration form.
 *
 * @Block(
 *   id = "formblock_coach_register",
 *   admin_label = @Translation("Coach registration form"),
 *   provider = "user",
 *   category = @Translation("Forms")
 * )
 *
 * Note that we set module to contact so that blocks will be disabled correctly
 * when the module is disabled.
 */
class CoachRegisterBlock 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, 'nouvea');

    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function blockAccess(AccountInterface $account) {
    return AccessResult::allowedIf($account->isAnonymous() && (\Drupal::config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY))
      ->addCacheContexts(['user.roles'])
      ->addCacheTags(\Drupal::config('user.settings')->getCacheTags());
  }
}
Run Code Online (Sandbox Code Playgroud)

我用我的新表单模式 display nouvea的名称替换了register

在 build() 函数中

$build['form'] = $this->entityFormBuilder->getForm($account, 'register');
Run Code Online (Sandbox Code Playgroud)

取而代之

$build['form'] = $this->entityFormBuilder->getForm($account, 'nouvea');
Run Code Online (Sandbox Code Playgroud)

5 - 将块添加到您的界面

现在最后一步是清除您的缓存(drush cr 或界面链接)并使用管理界面将创建的块(您应该找到admin_label,在我的情况下为“教练注册表”)添加到您的页面。

并且假设,它工作得很好!!