PHPackages                             happycog/craftcms-block-injector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. happycog/craftcms-block-injector

ActiveCraft-plugin[Utility &amp; Helpers](/categories/utility)

happycog/craftcms-block-injector
================================

Inject matrix blocks based on various rules.

0.4.0(4y ago)1452MITPHP

Since May 25Pushed 4y agoCompare

[ Source](https://github.com/happycog/craftcms-block-injector)[ Packagist](https://packagist.org/packages/happycog/craftcms-block-injector)[ RSS](/packages/happycog-craftcms-block-injector/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (2)Versions (5)Used By (0)

Block Injector plugin for Craft CMS 3.x
=======================================

[](#block-injector-plugin-for-craft-cms-3x)

Inject matrix blocks based on various rules.

Extending with Events
---------------------

[](#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

[](#examples)

#### `modules/Module.php`

[](#modulesmodulephp)

```
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`

[](#modulesrulebehaviorphp)

```
