Support

Account

Home Forums Gutenberg Setting the gutenberg block render callback dynamically Reply To: Setting the gutenberg block render callback dynamically

  • You can not only pass a string as the render_callback. You could also pass an anonymous function, plus PHP treats an [$object, 'methodName'] array as a callable.

    Here’s the Block class i came up with. Check register() to see how it specifies an instance method as the render_callback.

    
    namespace Foo;
    
    use Timber\Timber;
    
    abstract class Block
    {
        abstract public function getName(): string;
        abstract protected function getTitle(): string;
        abstract protected function getDescription(): string;
        abstract protected function getCategory(): string;
        abstract protected function getIcon(): string;
        abstract protected function getKeywords(): array;
    
        abstract protected function getTemplateFilename(): string;
    
        abstract protected function defineFields(): array;
    
        protected function filterSupports(array $supports): array
        {
            return $supports;
        }
    
        protected function filterContext(array $context): array
        {
            return $context;
        }
    
        public function register()
        {
            acf_register_block_type([
                'name' => $this->getName(),
                'title' => $this->getTitle(),
                'description' => $this->getDescription(),
                'category' => $this->getCategory(),
                'icon' => $this->getIcon(),
                'keywords' => $this->getKeywords(),
                'render_callback' => [$this, 'render'],
                'mode' => 'edit',
                'supports' => $this->filterSupports([
                    'align' => false,
                    'align_text' => false,
                    'alignt_content' => false,
                    'mode' => true,
                    'multiple' => true,
                ]),
            ]);
    
            $this->registerFields();
        }
    
        private function registerFields()
        {
            acf_add_local_field_group([
                'key' => 'group_block_' . $this->getName(),
                'title' => $this->getTitle(),
                'fields' => $this->defineFields(),
                'active' => true,
                'location' => [
                    [
                        [
                            'param' => 'block',
                            'operator' => '==',
                            'value' => 'acf/' . $this->getName(),
                        ],
                    ],
                ],
            ]);
        }
    
        public function compile(array $block): string
        {
            $context = $this->filterContext($block['data']);
    
            return Timber::compile($this->getTemplateFilename(), $context);
        }
    
        public function render(array $block)
        {
            echo $this->compile($block);
        }
    
        public function dynamicRender(string $content, array $block): string
        {
            return $content;
        }
    }