Support

Account

Forum Replies Created

  • 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;
        }
    }
    
  • You don’t seem to understand what @saltnpixels is trying to achieve. The result of the render_callback specified when registering a block through ACF is stored to database on save of the post.

    The desired behaviour is to have the content generated on request. Thus the example of the latest three posts, which might be different each time a user views the post.

    The solution to the problem is using the render_block filter: https://developer.wordpress.org/reference/hooks/render_block

    The second argument to this hook, $block, is an array. You can compare $block['blockName'] against your block name (beware of the acf/ prefix) and return dynamic content when it matches.

    $block['attrs']['data'] is an array of all custom fields you defined for the block.

Viewing 2 posts - 1 through 2 (of 2 total)