rareloop/pebble-acf-blocks

There is no license information available for the latest version (dev-master) of this package.

dev-master 2020-04-28 15:50 UTC

This package is auto-updated.

Last update: 2024-03-29 01:04:23 UTC


README

This package provides a way to create ACF Blocks that use Twig templates and simultaneously integrate with both Lumberjack and Primer.

Installation

composer require rareloop/pebble-acf-blocks

Once installed, register the Service Provider in config/app.php:

'providers' => [
    ...

    Rareloop\Lumberjack\AcfBlocks\AcfBlocksProvider::class,

    ...
],

Copy the example config/acfblocks.php file to you theme directory.

Usage

To create a block, first create a child of the AcfBlock. This should sit in the same folder as your Primer component, for example blocks/my-block. The name of the class should also be the Upper Camel Case version of the folder name, in our case MyBlock.

Note: Pebble maps the namespace \Patterns to the directory my-theme/resources/patterns

<?php

namespace Patterns\Blocks\MyBlock;

use Rareloop\Lumberjack\AcfBlocks\AcfBlock;

class MyBlock extends AcfBlock
{
    /**
     * Provide the data to pass to the template
     *
     * @return array
     */
    public function context(): array
    {
        return [
            'name' => get_field('test_field'),
        ];
    }

    /**
     * Provide the config required to register this block
     * https://www.advancedcustomfields.com/resources/acf_register_block_type/
     *
     * @return array
     */
    public static function blockConfig(): array
    {
        return [
            'name'              => 'mytestblock',
            'title'             => __('Test Block'),
            'description'       => __('A first go with a block.'),
            'category'          => 'formatting',
            'icon'              => 'admin-comments',
            'keywords'          => ['testimonial', 'quote'],
        ];
    }
}

The blockConfig() function is what ACF uses to register the block with WordPress. For more configuration options please see the ACF documentation.

The context() function is where you provide the data for your patterns template.twig file when used in WordPress. Within this function, all calls to get_field() will be scoped to the current Gutenberg block, as is the case with other ACF Blocks.

The final step is to add the class to your config/acfblocks.php file:

<?php

return [
    'blocks' => [
        \Patterns\Blocks\MyBlock\MyBlock::class,
    ]
];

Additional Parameters

You have access to the following additional parameters from within your ACF class:

  • $this->content - The block inner HTML (empty)
  • $this->isPreview - Whether or not the block is being shown in preview
  • $this->postId - The ID of the post that the block is attached to