wieni / wmdummy_data
提供Drupal服务和Drush 9命令,以便轻松创建虚拟数据。
Requires
- php: ^7.3 || ^8.0
- drupal/core: ^9.3 || ^10.0
- fakerphp/faker: ^1.10
- wieni/wmmodel_factory: ^2.0
Requires (Dev)
- composer-runtime-api: ^2.0.0
- drush/drush: ^10.6
- ergebnis/composer-normalize: ^2.0
- fenetikm/autoload-drupal: dev-master#4503484
- wieni/wmcodestyle: ^1.9
- wieni/wmcontent: ^2.0
README
提供Drupal服务和Drush 9命令,以便轻松创建虚拟数据。
安装
此包需要PHP 7.1和Drupal 8.7.7或更高版本。可以使用Composer进行安装。
composer require wieni/wmdummy_data
它是如何工作的?
创建工厂和状态
要了解如何定义您的工厂和状态类,请参阅wieni/wmmodel_factory文档。
为了访问以下功能,您需要扩展EntityFactoryBase或EntityStateBase,而不是扩展由wmmodel_factory提供的类。
使用wieni/wmcontent生成内容
要使用wmcontent模块生成实体的内容,请确保您的生成器实现了ContentGenerateInterface。
示例
<?php namespace Drupal\my_module\Entity\ModelFactory\Factory\Node; use Drupal\wmcontent\Entity\WmContentContainer; use Drupal\wmdummy_data\EntityFactoryBase; /** * @EntityFactory( * entity_type = "node", * bundle = "page", * ) */ class PageFactory extends EntityFactoryBase implements ContentGenerateInterface { public function make(): array { return [ 'title' => $this->faker->sentence(4), 'menu_link' => null, ]; } public function generateContent(WmContentContainer $container): array { $entityType = $container->getChildEntityType(); $bundles = $container->getChildBundles() ?: $container->getChildBundlesAll(); $amount = $this->faker->numberBetween(1, 10); return array_map( fn () => $this->faker->entityWithType($entityType, $this->faker->randomElement($bundles)), array_fill(0, $amount, null) ); } }
生成要引用的实体
要使用新或现有实体填充实体引用字段,请在您的生成器中使用以下方法
$this->faker->entity()(传递实体捆绑类类的类名)$this->faker->entityWithType()(传递实体类型ID和可选捆绑,作为字符串)
示例
<?php namespace Drupal\my_module\Entity\ModelFactory\Factory\Node; use Drupal\my_module\Entity\TaxonomyTerm\Tag; use Drupal\wmdummy_data\EntityFactoryBase; /** * @EntityFactory( * entity_type = "node", * bundle = "page", * ) */ class PageFactory extends EntityFactoryBase { public function make(): array { return [ 'title' => $this->faker->sentence(4), 'menu_link' => null, 'field_tag' => [ 'entity' => $this->faker->entity(Tag::class), ], ]; } }
生成Vimeo和YouTube视频的链接
要在生成器中访问Vimeo和YouTube视频的现有链接,请使用以下方法
$this->faker->vimeoUrl$this->faker->youTubeUrl
示例
<?php namespace Drupal\my_module\Entity\ModelFactory\Factory\ContentBlock; use Drupal\wmdummy_data\EntityFactoryBase; /** * @EntityFactory( * entity_type = "content_block", * bundle = "video", * ) */ class VideoFactory extends EntityFactoryBase { public function make(): array { $data = [ 'field_video_title' => $this->faker->optional()->sentence($this->faker->numberBetween(4, 8)), 'field_video_type' => $this->faker->randomElement(['youtube', 'vimeo']), ]; switch ($data['field_video_type']) { case 'vimeo': $data['field_video_vimeo'] = $this->faker->vimeoUrl; break; case 'youtube': $data['field_video_youtube'] = $this->faker->youTubeUrl; break; } return $data; } }
使用权重从数组中选择随机元素
要在生成器中从具有不同概率被返回的元素的数组中获取随机元素,请使用以下方法
$this->faker->randomElementWithWeight()
传递一个数组,其中值作为键,权重作为值,形式为整数。
示例
<?php namespace Drupal\my_module\Entity\ModelFactory\Factory\Node; use Drupal\node\NodeInterface; use Drupal\wmdummy_data\EntityFactoryBase; /** * @EntityFactory( * entity_type = "node", * bundle = "page", * ) */ class PageFactory extends EntityFactoryBase { public function make(): array { return [ 'title' => $this->faker->sentence(4), 'menu_link' => null, 'status' => $this->faker->randomElementWithWeight([ NodeInterface::NOT_PUBLISHED => 70, NodeInterface::PUBLISHED => 30, ]), ]; } }
生成HTML字符串
要在生成器中使用以下方法生成随机HTML字符串
$this->faker->htmlBlock(生成包含标题、段落、列表和表格的HTML块)$this->faker->htmlHeading(生成介于H1和H6之间的随机标题,也可以传递一个数字来自定义级别)$this->faker->htmlParagraph(包含随机文本的p标签,也包含strong和a标签)$this->faker->htmlOrdenedList(包含随机列表项的ol标签)$this->faker->htmlUnordenedList(包含随机列表项的ul标签)$this->faker->htmlEmbed(包含随机url的iframe标签,不一定是真实的url)$this->faker->htmlAnchor(包含随机url的a标签,不一定是真实的url)$this->faker->htmlTable(包含标题和几行/列的table标签)
Drush命令
此包提供了一些Drush命令来管理虚拟数据
wmdummy-data:generate:生成实体wmdummy-data:delete:删除生成的实体
有关命令别名、参数、选项和用法示例的更多信息,请使用-h / --help参数调用命令
用户界面
如果您更喜欢使用Drupal管理界面而不是使用CLI,您可以使用位于/admin/config/development/wmdummy-data的表单。您也可以通过管理菜单(配置 > 开发 > 虚拟数据)找到此页面。
只有具有生成虚拟数据和/或删除虚拟数据权限的用户才能通过管理界面使用这些功能。
事件
您可以通过订阅以下事件来将自定义逻辑附加到虚拟数据生成过程:
Drupal\wmdummy_data\DummyDataEvents::MAKE
将在实体生成后触发。
Drupal\wmdummy_data\DummyDataEvents::CREATE
将在实体生成并持久化到数据库后触发。
变更日志
本项目所有显著更改将记录在变更日志文件中。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件security@wieni.be联系,而不是使用问题跟踪器。
许可证
在MIT许可证下分发。更多信息请参阅许可证文件。