PHPackages                             helick/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. helick/blocks

ActiveLibrary

helick/blocks
=============

The Helick blocks

v1.2.3(6y ago)62301[1 issues](https://github.com/helick/blocks/issues)MITPHPPHP &gt;=7.1

Since Jul 31Pushed 6y agoCompare

[ Source](https://github.com/helick/blocks)[ Packagist](https://packagist.org/packages/helick/blocks)[ RSS](/packages/helick-blocks/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (6)Dependencies (4)Versions (7)Used By (0)

Helick Block
============

[](#helick-block)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2ea237e08c948c9e696a808e22548996ac3b3d215d3fd1046aa4dbb9810b6dd8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68656c69636b2f626c6f636b732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/helick/blocks)[![Total Downloads](https://camo.githubusercontent.com/5efed7b220bdc643f8a07596a82b083081b0353431fd8ee60a70646184a1f60d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68656c69636b2f626c6f636b732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/helick/blocks)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Quality Score](https://camo.githubusercontent.com/aae571f67fa5068c1f7a783f55a2bc6f61b2a1cfea9cd1bff06e24a518efda92/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f68656c69636b2f626c6f636b732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/helick/blocks)

The package assists you in easily creating Gutenberg blocks with Carbon Fields.

Requirements
------------

[](#requirements)

Make sure all dependencies have been installed before moving on:

- [PHP](http://php.net/manual/en/install.php) &gt;= 7.1
- [Composer](https://getcomposer.org/download/)
- [Carbon Fields](https://docs.carbonfields.net/#/quickstart) &gt;= 3.0

Install
-------

[](#install)

Install via Composer:

```
$ composer require helick/blocks
```

Usage
-----

[](#usage)

Within your theme declare your block, attach its fields, and provide data for your template:

```
use Carbon_Fields\Field;
use Helick\Blocks\Block;
use WP_Query;

final class ExampleBlock extends Block
{
    /**
     * The block's display name.
     *
     * @var string
     */
    protected $name = 'Example';

    /**
     * The block's description.
     *
     * @var string
     */
    protected $description = 'This is an example block';

    /**
     * The block's template.
     *
     * @var string|string[]
     */
    protected $template = 'partials/blocks/example.php';

    /**
     * Fields to be attached to the block.
     *
     * @return array
     */
    public function fields(): array
    {
        return [
            Field::make('text', 'heading', 'Heading'),
            Field::make('image', 'image', 'Image'),
            Field::make('rich_text', 'content', 'Content'),
            Field::make('association', 'associations', 'Associations')
                 ->set_types([
                     [
                         'type'      => 'post',
                         'post_type' => 'post',
                     ]
                 ])
        ];
    }

    /**
     * Data to be passed to the rendered block.
     *
     * @param array $fields
     *
     * @return array
     */
    public function with(array $fields): array
    {
        return [
            'associations' => $this->queryAssociations($fields['associations'])
        ];
    }

    /**
     * Query the associations.
     *
     * @param array $associations
     *
     * @return WP_Query
     */
    private function queryAssociations(array $associations): WP_Query
    {
        $associationIds = array_column($associations, 'id');
        $associationIds = array_map('intval', $associationIds);

        return new WP_Query([
            'no_found_rows' => true,
            'post__in'      => $associationIds,
            'orderby'       => 'post__in',
        ]);
    }
}
```

Create your block template:

```
