Problems when trying to create a block that has a blade template in a subfolder

I want to put acf blocks in deeper folder but then for some reason, fields does not get assigned.

so for example i have a block class that looks like this:

<?php

namespace App\Blocks\about;

use Log1x\AcfComposer\Block;
use Roots\Acorn\Application;
use StoutLogic\AcfBuilder\FieldsBuilder;

class Herox extends Block
{
    public function __construct(Application $app)
    {
        $this->name = __('Herox', 'sage');
        $this->slug = 'about-herox';
        $this->category = 'blocks';
        $this->icon = 'editor-ul';
        $this->keywords = [
            'hero',
        ];

        parent::__construct($app);
    }

    public function fields(): FieldsBuilder|array
    {
        $builder = new FieldsBuilder('about-herox');

        $builder
            ->addText('titlex');

        return $builder->build();
    }

    public function with(): array
    {
        return [
            'title' => get_field('title'),
        ];
    }
}

and in this case it works fine, it takes the blade file from

:web/app/themes/my-theme/resources/views/blocks/about-herox.blade.php

but i wanted to move the blade block to its own folder like this:

:web/app/themes/my-theme/resources/views/blocks/about/herox.blade.php

in this case, i changed the slug to: $this->slug = ‘about.herox’; and the template worked, but when i try to edit fields in it, it says that the fields are not defined. What could be the problem, sorry if it’s a rookie mistake im making, but i want to find the solution. (tried the gpt but it didn’t helped)

thanks!

You will need to add the $view property to your block and set it to blocks.about.herox (dot-notation for your block view path).

You should also capitalize About in your namespace.

- namespace App\Blocks\about;
+ namespace App\Blocks\About;
1 Like

Thank you for you fast response. Sadly it didn’t helped.
the blade view is being found automatically, but the problem is that it does not find the decalred fields:

    public function fields(): FieldsBuilder|array
    {
        $builder = new FieldsBuilder('about.herox');

        $builder
            ->addText('titlex');

        return $builder->build();
    }

I did tried multiple namings like just herox or about.herox but the problem keeps persisting

oh nevermind i misunderstood the $view, i used $this->view before. anyway thank you very much, it worked!

Realize this is an old post, but my question is relevant. I am on a sage 10 site that was upgraded from Sage 9. The existing blocks use the GUI for managing fields. I want to use log1x acf composer moving forward and wanted to create a new subdirectory called log1x-blocks to hold the new block templates.

So the directory structure is like so: views/log1x-blocks/new-block.blade.php

I have set $path = ‘logix-blocks.new-block’, but I cannot get the block to appear as an option on the admin side. Does a custom block directory need to be in the blocks directory?

As stated above, you have to set the $view-property, not $path, in the Block-class.

The custom blocks directory does not have to be in in the blocks-directory so if you put your view file in resources/views/log1x-blocks/new-block.blade.php and set $view in the block class to log1x-blocks.new-block, it should work.

Right. That’s what I’ve been doing. Not sure why they blocks aren’t registering though. Fields still register. Just not blocks.

I suggest that you add a die()-statement to the fields-function of your block class to find out if the block is picked up or not. Or if you have another preferred way to detect if a piece of code is executed.

If you still cant find the issue based on the outcome of that, it is probably better to create a separate thread about it with the relevant code for your block class included. I don’t think that your issue is closely related to the original issue in this thread which dealt with fields not being displayed whereas your issue is that the block is not picked up at all.