Coder Social home page Coder Social logo

acf-block-loader's Introduction

ACF-Block-Loader

Wrapper class for easy ACF Gutenberg Block registration

Install

composer require wp63/acf-block-loader

Usage

By default, Class file are to be stored inside Blocks directory in theme root directory (or app\Blocks in Sage 9). File name should be the same as Classname as in PSR-4.

  1. Create a class extending WP63\Block
  2. Class default namespace is WP63\Blocks\
  3. Class must have at least 2 methods: register() and static render()
<?php
namespace App\Blocks;

use WP63\Block;

class MyBlock extends Block {
  protected function register() {
    return [
      'name' => $name,
      'title' => $title,
      'category' => $category,
      'fields' => $fields,
    ];
  }

  public static function render( $options ) {
    // You can directly render HTML within `render()` method
    ...
    display block html
    ...

    // Or alternatively, return an array for Blade template engine in Sage 9
    return [
      'name' => 'value', // $name in template file
      'foo' => 'bar', // $foo in template file
    ];
  }
}

register() method will return an array with 3 keys

  • $name block-unique-name
  • $title Block Title
  • $category Block category. Predefined categories are [ common | formatting | layout | widgets | embed ] (Optional)
  • $fields ACF fields StoutLogic\AcfBuilder\FieldsBuilder object. Output of FieldsBuilder::build() method.

render() is the static method for rendering actual block. Every output generated inside this method will be part of block html. Render method will have access to 4 callback arguments from ACF via $options arguments

  • array $options->block The block settings and attributes.
  • string $options->content The block inner HTML (empty).
  • boolean $options->is_preview True during AJAX preview.
  • int|string $options->post_id The post ID this block is saved to.

Filters

  • wp63/acf_block_namespace Change block namespace. Default: App\Blocks\
  • wp63/acf_block_directory Change directory name. Default: ./Blocks
  • wp63/acf_block_template_directory Change Blade template directory. relative to /views directory Default: blocks

Actions

  • wp63/before_block_render Before each block is rendered
  • wp63/before_block_render/{$block_name} Before $block_name is rendered
  • wp63/after_block_render After each block is rendered
  • wp63/after_block_render/{$block_name} After $block_name is rendered

Use with Blade template engine in Sage 9

  1. In class render() method, instead of echo output directly, return an array contains all data to be exposed to Blade template file.
  2. Create template file in views/blocks the file name must be the same as block nam3 ($name). for example: views/blocks/hero-banner.blade.php

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.