MWDelaney / sage-acf-wp-blocks

Composer library for generating ACF Gutenberg blocks from templates. Intended for use with Roots/Sage (http://roots.io)
346 stars 66 forks source link

Use Sober controller #5

Open stephanschonbeck opened 5 years ago

stephanschonbeck commented 5 years ago

Is it possible to use Sober controller with this?

I have tried but it doesn't work.

Thanks.

MWDelaney commented 5 years ago

So far it does not appear possible. Adding support will probably require @darrenjacoby’s insight and assistance.

tombroucke commented 4 years ago

I am used to pass my data through the sage/blocks/$slug/data filter, but this is not the best way imo. I am trying out some other ways to pass data. This is what I came up with (consider a block "Title" with just one text field):

your-theme/app/Blocks/Title.php:

<?php

namespace App\Blocks;

class Title {

    public static function title() {
        return get_field( 'title' );
    }
}

your-theme/resources/views/blocks/title.blade.php:

{{--
  Title: Title
  Description: Custom block Title
  Keywords: Title
  Mode: edit
  Align: full
  SupportsAlign: full
  SupportsMode: true
  SupportsMultiple: true
--}}

@use(\App\Blocks\Title)

<h1>{{ Title::title() }}</h1>

your-theme/app/setup.php:

    /**
     * Create @use() Blade directive
     */
    sage('blade')->compiler()->directive('use', function ($full_class_path) {
        return "<?php use {$full_class_path}; ?>";
    });

The only downside could be the missing $block array inside the Title object, but when needed, this could be passed as an argument.

Any thoughts?

intelligence commented 3 years ago

@tombroucke Could you show an example how you use the filter aswell? I can't wrap my head around it.

Thanks!

tombroucke commented 3 years ago

@intelligence The use of the filter is described in the readme file (Filter block data): https://github.com/MWDelaney/sage-acf-wp-blocks/blob/master/README.md

broskees commented 2 years ago

I had a similar idea to @tombroucke

app/setup.php

// acf block controllers
add_action('acf/init', function () {
    require_once(config('theme.dir') . '/app/BlockController.php');

    collect(glob(config('theme.dir') . '/app/BlockControllers/*.php'))->map(function ($block) {
        return require_once($block);
    })->map(function ($block) {
        if ($block instanceof BlockController) {
            add_filter($block->getFilter(), $block->getFunction());
        }
    });
});

/**
 * Create @extract() Blade directive
 */
sage('blade')->compiler()->directive('extract', function ($array) {
    return "<?php extract({$array}); ?>";
});

app/BlockController.php

<?php

namespace App;

class BlockController
{
    private $filterFunction;
    private $blockSlug;

    public function __construct($slug, $function)
    {
        $this->blockSlug = "sage/blocks/${slug}/data";
        $this->filterFunction = $function;
    }

    public function getFunction()
    {
        return $this->filterFunction;
    }

    public function getFilter()
    {
        return $this->blockSlug;
    }
}

app/BlockControllers/block-slug.php

<?php

namespace App;

use App\BlockController;

$controller = new BlockController('block-slug', function ($block) {
  $controller_data = [];

  // data manipulation

  $block['controller_data'] = $controller_data;

  return $block;
});

views/blocks/block-slug.blade.php

{{--
  Title: Testimonial
  Description: Customer testimonial
  Category: formatting
  Icon: admin-comments
  Keywords: testimonial quote
  Mode: edit
  Align: left
  PostTypes: page post
  SupportsAlign: left right
  SupportsMode: false
  SupportsMultiple: false
  EnqueueStyle: styles/style.css
  EnqueueScript: scripts/script.js
  EnqueueAssets: path/to/asset
--}}

@extract($block['controller_data'])

<!-- Block Markup -->