wieni / wmcustom_entity_form
按包使用自定义实体表单
1.4.0
2023-09-12 12:16 UTC
Requires
- php: ^7.1 || ^8.0
README
Drupal 自定义实体表单
按包使用自定义实体表单。
安装
composer require wieni/wmcustom_entity_form
drush en wmcustom_entity_form
原因
因为你喜欢根据客户需求构建表单,同时你也喜欢保持代码库整洁。
使用
这个基于插件的模块将在你的模块的 Form/Entity
目录中查找实体表单。
类必须实现 @EntityForm
注解。你给这个注解提供的 id
将用于匹配实体类型和包。
注意事项
这仅在由 EntityFormBuilder 创建的表单上工作。如果你的表单是使用内联实体表单显示的,则不会触发。在这种情况下,你仍然需要使用 HOOK_inline_entity_form_entity_form_alter
示例
<?php namespace Drupal\mymodule\Form\Entity; use Drupal\Core\Form\FormStateInterface; use Drupal\wmcustom_entity_form\Annotation\EntityForm; use Drupal\node\NodeForm; use Symfony\Component\DependencyInjection\ContainerInterface; /** * @EntityForm( * id="node.article" * ) */ class ArticleForm extends NodeForm { public static function create(ContainerInterface $container) { // use dependency injection return new static(...); } public function form(array $form, FormStateInterface $formState) { $form = parent::form($form, $formState); // alter form without using any hooks // use afterbuilds with the :: notation for non-static afterbuilds $form['#after_build'][] = '::myAfterBuild'; // access $this->entity; ( powerful combined with wieni/wmmodel ) if ($this->entity->getWhatever()) { $form['field_foo_bar']['#access'] = false; } return $form; } public function myAfterBuild(array $form, FormStateInterface $formState) { // access to $this scope and all your injected dependencies } // override protected methods of the original entity form protected function actions(array $form, FormStateInterface $formState) { return parent::actions($form, $formState); } }
也请参阅
- wieni/wmmodel 以在每包中有一个模型类。
- wieni/wmcontroller 以在每包中有一个控制器。