PHPackages                             brandcom/cakephp-content-blocks - 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. [Framework](/categories/framework)
4. /
5. brandcom/cakephp-content-blocks

ActiveCakephp-plugin[Framework](/categories/framework)

brandcom/cakephp-content-blocks
===============================

ContentBlocks plugin for CakePHP

v0.3.2(3y ago)0128MITPHP

Since Apr 21Pushed 3y ago1 watchersCompare

[ Source](https://github.com/brandcom/cakephp-content-blocks)[ Packagist](https://packagist.org/packages/brandcom/cakephp-content-blocks)[ RSS](/packages/brandcom-cakephp-content-blocks/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (2)Versions (14)Used By (0)

ContentBlocks plugin for CakePHP
================================

[](#contentblocks-plugin-for-cakephp)

Installation
------------

[](#installation)

You can install this plugin into your CakePHP application using [composer](https://getcomposer.org).

The recommended way to install composer packages is:

```
composer require brandcom/cakephp-content-blocks

```

Load the plugin:

```
bin/cake plugin load ContentBlocks

```

... and run the migrations:

```
bin/cake migrations migrate --plugin ContentBlocks

```

Getting Started
---------------

[](#getting-started)

### 1. Create Blocks

[](#1-create-blocks)

The plugin does not come with any blocks, so you will have to create them on your own.

Let's create a simple TextContentBlock with a `title` and a `content` field.

> **Note:** Every ContentBlock must end with `*ContentBlock`

1. In your database, create a table `text_content_blocks` with the fields `title` (Varchar 255) and `content` (Text). **Note:** You will also need an `id` field and a field `content_blocks_block_id` as `int(11), unsigned`.
2. Then, run the `bin/cake bake model text_content_blocks` command. You won't need any templates or controllers.
3. Edit the baked `TextContentBlock.php` file and make the class extend `ContentBlocks\Model\Entity\Block` instead of `Entity`.
4. Modify your `TextContentBlocksTable.php` so that your `TextContentBlocksTable` extends `ContentBlocks\Model\Entity\BlocksTable`, and modify the class as follows:

Set the relation:

```
$this->belongsTo('Blocks', [
    'foreignKey' => 'content_blocks_block_id',
    'className' => 'ContentBlocks.Blocks',
]);

```

Fix the `buildRules()` method, set `table` to `Blocks`:

```
$rules->add($rules->existsIn(['content_blocks_block_id'], 'Blocks'));

```

You will find more on customizing your Block below, but let's now add it to one of your pages.

### 2. Admin interface

[](#2-admin-interface)

To add your TextContentBlock to an Entity, e.g., `Article`, add the `BlocksAdmin` cell to your edit template:

```

```

This will render a table representing the BlockArea for the respective entity.

**New in version 0.3:** You can also create Areas for custom keys, e.g. on other places on your site or for index pages:

```

```

At the bottom of the table, there will be a button for each of your ContentBlocks. You should already find a button with the title `Text`.

If you click on the button, a new Block will be added to the Block Area. You can enter content and save.

### 3. Create a template for your block

[](#3-create-a-template-for-your-block)

ContentBlock templates are elements. In your `Template/Element` folder, create the folder `content_blocks/` and the file `text.ctp`.

The template file name is always the lower\_case\_underscore version of your model name, omitting the content\_block.

So the template for `MyCoolHeroHeaderContentBlock` will be in `/Template/Element/content_blocks/my_cool_hero_header.ctp`.

Your `TextContentBlock` Entity will be available as the `$block` variable.

Now you can create your template:

```

```

> **Note:** You can change the rendering logic in your `TextContentBlock` Entity by overriding the `render()` method.

### 4. Display the blocks

[](#4-display-the-blocks)

To render your block area for an entity, add this cell to your template:

```

```

### 5. Modifying the Admin interface

[](#5-modifying-the-admin-interface)

If you want to change how fields are displayed, you can override the `getFields()` method in your `TextContentBlock` Entity.

The method should return an array of all editable fields with the field names as keys and an array as value which is passed to `FormHelper::control` as `$options`:

```
public function getFields(): array
{
    return array_merge(
        parent::getFields(),
        [
            'title' => [
                'label' => __("Block Title"),
            ],
            'style' => [
                'label' => __("Choose a style for this block."),
                'options' => [
                    'default' => __("Default Style"),
                    'funky' => __("Other cool Style"),
                ],
            ],
        ]
    );
}

```

Special field options:

- `beforeControl`: Will be rendered before the respective control.
- `afterControl`: Will be rendered after the respective control.

To change the hidden fields, override `Block::getHiddenFields()`:

```
public function getHiddenFields(): array
{
    return array_merge(
        parent::getHiddenFields(),
        [
            'some_field',
            'another_hidden_field',
        ]
    );
}

```

E.g., `content_blocks_block_id` is hidden by default, and you may want to make it editable.

### 6. Containing associated Models

[](#6-containing-associated-models)

Define a `beforeFind()` method in your `TextContenBlocksTable`

```
public function beforeFind(Event $event, Query $query): Query
{
    return $query->contain([
        'Images',
    ]);
}

```

### 7. Edit related models

[](#7-edit-related-models)

The plugin supports an admin interface even for related models.

#### HasMany relations

[](#hasmany-relations)

Say you have a `SliderContentBlock` with several slides. This means, you will have e.g. a `SliderBlockSlide` entity and a `SliderBlockSlidesTable`.

1. Let your Slide entity use the `ContentBlocks\Model\Entity\Traits\BelongsToBlockTrait`
2. Contain the Slide as shown in **6. Containing associated Models**
3. In your `SliderContentBlock`, override the `Block`'s method `getManagedModels()`. This should return an array of all related models which shall be editable in the admin form.

```
public function getManagedModels(): array
{
    return [
        "SliderBlockSlides,"
    ];
}

```

4. To customize the appearance in the admin form, you can override the methods from the `BelongsToBlockTrait`, e.g. to define a nicer title or control what fields are available for editing (similarly as in **5. Modifying the Admin interface**).

### 8. Pass custom data to the block template

[](#8-pass-custom-data-to-the-block-template)

By default, the containing model of the block area is passed to the template as `$owner`, so, e.g., if you have an `Article` entity which has a `TextContentBlock`, your article entity will be accessible in the `text.ctp` as `$owner`.

You can override this - and add more view variables - by overriding the `getViewVariables()` method in your `*ContentBlocksTable`.

If you want to rename `$owner` and add more data, you can do that like so:

```
public function getViewVariables($entity): array
{
    $vars = parent::getViewVariables($entity);

    return [
        'article' => $vars['owner'],
        'random' => "This is just a string",
        'someOtherVariable' => $this->getSomeOtherVariable($entity),
    ];
}

```

`$entity`, the instance of your `*ContentBlock`, will be passed to the method.

### 9. Allow or disallow the ContentBlock on specific Entities

[](#9-allow-or-disallow-the-contentblock-on-specific-entities)

If an Entity's model is listed in ContentBlock::getDisallowedEntities(), it will never be visible in the Block list.

```
protected function getDisallowedEntities(): array
{
    return [
        "Articles",
        "Jobs",
    ];
}

```

You can also define a list of allowed Entities:

```
protected function getAllowedEntities(): array
{
    return [
        "BlogPosts",
    ];
}

```

> **Note:** If a model is listed in both, it will be disallowed.

### 10. Block HTML-Anchors and (custom) preview-link

[](#10-block-html-anchors-and-custom-preview-link)

**New in 0.2.0:** Every Block has a field for an HTML-Anchor. A link to the anchor is displayed below.

To override the default route, you can define a url on your `$owner` entity which holds the Area, e.g. `Articles`. This can be useful, if your URL uses a `slug` or other parameters. The `$block` instance will be passed to the method:

```
public function getContentBlocksViewUrl(Block $block): array
{
    $anchor = $block->html_anchor ?: "content-block-" . $block->id;

    return [
        'prefix' => false,
        'plugin' => false,
        'controller' => 'Articles',
        'action' => 'view',
        $this->slug,
        '#' => $anchor,
    ];
}

```

Contribution
------------

[](#contribution)

You can contribute to this project via pull requests or issues.

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~19 days

Recently: every ~46 days

Total

12

Last Release

1323d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/77938819?v=4)[Pascal Schneider](/maintainers/passchn)[@passchn](https://github.com/passchn)

![](https://www.gravatar.com/avatar/86ac90232d8367beb9bc60f0a438881c9e44b5e9b17b740b5166b5119f9ae737?d=identicon)[brandcom](/maintainers/brandcom)

---

Top Contributors

[![passchn](https://avatars.githubusercontent.com/u/77938819?v=4)](https://github.com/passchn "passchn (88 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/brandcom-cakephp-content-blocks/health.svg)

```
[![Health](https://phpackages.com/badges/brandcom-cakephp-content-blocks/health.svg)](https://phpackages.com/packages/brandcom-cakephp-content-blocks)
```

###  Alternatives

[cakephp/debug_kit

CakePHP Debug Kit

86314.7M171](/packages/cakephp-debug-kit)[cakephp/bake

Bake plugin for CakePHP

11212.0M202](/packages/cakephp-bake)[dereuromark/cakephp-queue

The Queue plugin for CakePHP provides deferred task execution.

308954.9k25](/packages/dereuromark-cakephp-queue)[dereuromark/cakephp-ide-helper

CakePHP IdeHelper Plugin to improve auto-completion

1882.3M44](/packages/dereuromark-cakephp-ide-helper)[cakephp/twig-view

Twig powered View for CakePHP

155.7M19](/packages/cakephp-twig-view)[dereuromark/cakephp-tinyauth

A CakePHP plugin to handle user authentication and authorization the easy way.

131240.2k13](/packages/dereuromark-cakephp-tinyauth)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
