happycog/craftcms-block-injector
Composer 安装命令:
composer require happycog/craftcms-block-injector
包简介
Inject matrix blocks based on various rules.
README 文档
README
Inject matrix blocks based on various rules.
Extending with Events
\happycog\blockinjector\behaviors\InjectableBehavior::EVENT_BEFORE_APPLY_RULES- Useful for modifying blocks before rules are applied. See "Splitting Blocks" example.
\happycog\blockinjector\behaviors\InjectableBehavior::EVENT_AFTER_APPLY_RULES- Useful for any operations done after rules are applied. See "Debugging Block Types" example.
\happycog\blockinjector\Rule::EVENT_DEFINE_BEHAVIORS, or any other events inherited from\craft\base\Component- Useful for adding your own rule methods, see "Adding Custom Rule Methods" example.
Examples
modules/Module.php
namespace modules; use Craft; use craft\elements\db\MatrixBlockQuery; use craft\elements\MatrixBlock; use craft\events\DefineBehaviorsEvent; use craft\helpers\Stringy; use happycog\blockinjector\behaviors\InjectableBehavior; use happycog\blockinjector\events\InjectionEvent; use happycog\blockinjector\Rule; use Illuminate\Support\Collection; use yii\base\Event; class Module extends \yii\base\Module { public function init() { parent::init(); // Adding Custom Rule Methods Event::on( Rule::class, Rule::EVENT_DEFINE_BEHAVIORS, function (DefineBehaviorsEvent $event) { $event->behaviors = [ RuleBehavior::class ]; } ); // Debugging Block Types // Injection rules can get confusing. This dumps out all the block types so you can see where things are getting injected. Event::on( MatrixBlockQuery::class, InjectableBehavior::EVENT_AFTER_APPLY_RULES, function (InjectionEvent $event) { \Craft::dd($event->blocks->pluck('type.handle')); } ); // Splitting Blocks // This splits blocks with a rich text field into their own blocks with a single paragraph, so you can perform injection rules on them. // Additionally, if a paragraph is less that 200 characters, it is combined with the preceding paragraph. Event::on( MatrixBlockQuery::class, InjectableBehavior::EVENT_BEFORE_APPLY_RULES, function (InjectionEvent $event) { $event->blocks = $event->blocks->flatMap(function ($block) { if ($block->type->handle !== 'copy' || !$block->text) { return new Collection([$block]); } $splitOn = "\n\n"; $paragraphs = new Collection(Stringy::create($block->text)->split($splitOn)); $tryAppend = false; return $paragraphs->reduce(function ($blocks, Stringy $paragraph) use (&$tryAppend, $splitOn) { if ($paragraph->length() < 200) { if ($tryAppend) { $lastBlock = $blocks->pop(); if ($lastBlock) { return $blocks->push($lastBlock->append($splitOn)->append($paragraph)); } } $tryAppend = true; } else { $tryAppend = false; } return $blocks->push($paragraph); }, new Collection()) ->map(function ($text) use ($block): MatrixBlock { $newBlock = clone $block; $newBlock->setFieldValue('text', (string) $text); return $newBlock; }); }); } ); } }
modules/RuleBehavior.php
<?php namespace modules; use craft\elements\MatrixBlock; use yii\base\Behavior; class RuleBehavior extends Behavior { public function atParagraphInterval(int $interval) { return $this->owner->atInterval($interval, function (MatrixBlock $block) { return $block->type->handle === 'copy'; }); } public function containsType(string $typeHandle): bool { return $this->owner->blocks->contains(function ($block) use ($typeHandle) { return self::blockIsType($block, $typeHandle); }); } public static function blockIsType(?MatrixBlock $block, string $type) { return $block && $block->type->handle === $type; } }
happycog/craftcms-block-injector 适用场景与选型建议
happycog/craftcms-block-injector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 452 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 happycog/craftcms-block-injector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 happycog/craftcms-block-injector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 452
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-28