anubarak/craft-seeder
Composer 安装命令:
composer require anubarak/craft-seeder
包简介
Easy entries seeder for Craft CMS
README 文档
README
Usage
Seeder allows you to quickly create dummy entries through the command line. And you can just as easily remove the dummy data when you're done building the site.
With the plugin installed, running php craft element-seeder/generate/entries create entries
Installation
To install the plugin, follow these instructions.
-
Open your terminal and go to your Craft project:
cd /path/to/project -
Then tell Composer to load the plugin:
composer require anubarak/craft-seeder -
In the Control Panel, go to Settings → Plugins and click the “Install” button for "Seeder".
Entries (Section ID/handle, count)
Use the command below, followed by the --section option and the --count of entries you want to create (defaults to 20 if ommited). This command works with both section ID and handle.
php craft element-seeder/generate/entries --section=news --count=15
Clean up
Once you're done building out the site, the plugin gives you an easy way to remove the dummy data (entries, assets, categories and users). This can be done through the CP (click the Seeder section the sidebar) or through the command line with the following command:
php craft element-seeder/clean-up
Configuration options
you can include custom configurations for each layout
<?php use anubarak\seeder\models\EntryConfig; use anubarak\seeder\models\FieldCallback; use anubarak\seeder\models\Settings; use craft\base\ElementInterface; use craft\base\FieldInterface; $config = (new Settings()) ->fieldsConfig([ new EntryConfig( 'news', [ (new FieldCallback('date')) ->setCallable( static function( \Faker\Generator $faker, FieldInterface $field, ElementInterface $element ) { return new DateTime(); } ), (new FieldCallback('date2')) ->setCallable( static function( \Faker\Generator $faker, FieldInterface $field, ElementInterface $element ) { $date = (clone $element->getFieldValue('date')); $date->modify('+1 day'); return $date; } ), (new FieldCallback('headline')) ->setFakerMethod('text'), ] ) ]); // required for Craft since they'll do an array_merge return $config->toArray([], ['fieldsConfig'], false);
Would create a custom callback while seeding fields for entries in the section news for fields
date, date2 and text.
Text would call the Faker function $faker->text and date and date2 would call the callback function in order to
populate fields with custom conditions
Populate Elements via CP
Matrix with unique Value
When an entry has a matrix field you can populate these matrix fields with blocks or with unique values in case their field layout allows it. For example when a block has a Dropdown field with 3 options and a Lightswitch field (with 2 options) you can generate 6 unique value combinations. In that case it will automatically create 6 blocks with each combination. Otherwise it will create the number of blocks set in the number field
Populate fields
Another option is to seed content in general.
You can choose which fields should be populated individually and Craft will seed it
Populate multiple elements
The same can be done with multiple elements via element index
Events
Register Field Type Event
to include custom fields, you can use the anubarak\seeder\events\RegisterFieldTypeEvent event.
\yii\base\Event::on( \anubarak\seeder\services\SeederService::class, \anubarak\seeder\services\SeederService::REGISTER_FIELD_TYPES, static function(\anubarak\seeder\events\RegisterFieldTypeEvent $event){ $event->types['my\field\Class'] = MyCustomField::class; } );
My Custom field could then look like the following
use craft\base\ElementInterface; use craft\base\FieldInterface; use anubarak\seeder\services\fields\BaseField class PlainText extends BaseField { /** * @inheritDoc */ public function generate(\craft\fields\PlainText|FieldInterface $field, ElementInterface $element = null) { if(!$field->multiline){ return $this->factory->text($field->charLimit ?: 200); } return $this->factory->realText($field->charLimit ?: 200); } }
Register Unique Field Event
To register a unique field that should be able to the unique matrix fields
\yii\base\Event::on( \anubarak\seeder\services\UniqueFields::class, \anubarak\seeder\services\UniqueFields::EVENT_REGISTER_UNIQUE_FIELDS, static function(\anubarak\seeder\events\RegisterUniqueFieldEvent $event){ $event->fields[] = MyCustomUniqueField::class; } );
class DropdownUniqueField implements UniqueFieldInterface { /** * @param \craft\fields\BaseOptionsField $field * * @inheritDoc */ public function getDescription(Field $field): string { $options = []; foreach ($field->options as $option) { $options[] = '<code>' . $option['value'] . '</code>'; } return 'Options: ' . join(' | ', $options); } /** * @param BaseOptionsField $field * * @inheritDoc */ public function getValues(Field $field): array { $options = []; foreach ($field->options as $option) { $options[] = $option['value']; } return $options; } /** * @inheritDoc */ public function getFieldClass(): string { return BaseOptionsField::class; } }
You can take a look at the existing unique Fields
anubarak/craft-seeder 适用场景与选型建议
anubarak/craft-seeder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.11k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cms」 「seeder」 「Craft」 「craftcms」 「craft-plugin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 anubarak/craft-seeder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anubarak/craft-seeder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 anubarak/craft-seeder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Expand, collapse, change the status of, or delete multiple blocks in a Matrix field simultaneously.
Philippines region, province, cities/municipalities and barangays Laravel migration and table seeder.
GraphQL authentication for your headless Craft CMS applications.
Set Links with a specific language parameter
Supercharged text field validation.
Versioned, environment-based Seeders in Laravel. Used while original does not update to Laravel 5.7
统计信息
- 总下载量: 1.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-04




