Using ACF Builder with Sage

I am using this package, and it’s just brilliant! Thanks a lot for making my life easier!
It’s the natural much simpler update to the original guide.

Just a little off-topic, but using acf composer I am seeing how great and useful is acorn, but there is no documentation or little guide even from a forum post. How can I learn the absolute basics of Acorn? The only option is to look at the code, or is there some tips you can share to make the process easier?

Hi @Log1x,

I’ve got a quick question using Roots Sage 9.

For some reason, multiple location conditions do not work at all for me, whereas the same logic would work when applied through the ACF GUI.

$builder
->setLocation('post_type', '==', 'post')
	->and('post_category', '==', 'videos');

Would you happen to know why this doesn’t work? Any pointers would be greatly appreciated. :blush:

@manuel try passing the category ID instead of the slug.

1 Like

Cheers, that did the trick, I just threw in the category object. Makes sense in hindsight. :smile:

$builder
	->setLocation('post_type', '==', 'post')
		->and('post_category', '==', get_category_by_slug('videos'));
2 Likes

When using conditional logic. How do I use a field name from another Fieldsbuilder.
I have a page_type field in the side position, and I want a field inside a banner to only be visible if a certain page_type is selected.
However selecting a field name that isn’t in the same php/fieldsbuilder file causes a 504 error.

In ACF UI, it uses the database field name.

 'conditional_logic' => array(
     array(
     	array(
     		'field' => 'field_615d918eef50b',
     		'operator' => '==',
     		'value' => 'value',
     		),
     	),
 ),

Thanks

I’ve followed the guide, but I don’t actually see anyplace that the custom field has been added on the post /page in the backend. Is there some kidn of step missing in this guide?

It looks like the fields aren’t registering period. Using Sage 9.

Hi everyone :wave:t2:
I’ve got an issue in setup.php after following the guide.
When I load the page, i got an error 500…

Fatal error: Uncaught Error: Call to undefined function App\acf_add_local_field_group() in /Users/baba/Sites/dlmc/web/app/themes/website/app/setup.php:144 Stack trace: #0 [internal function]: App\{closure}(Object(StoutLogic\AcfBuilder\FieldsBuilder), 0) #1 /Users/baba/Sites/dlmc/web/app/themes/website/vendor/illuminate/support/Collection.php(932): array_map(Object(Closure), Array, Array) #2 /Users/baba/Sites/dlmc/web/app/themes/website/app/setup.php(146): Illuminate\Support\Collection->map(Object(Closure)) #3 /Users/baba/Sites/website/web/wp/wp-includes/class-wp-hook.php(307): App\{closure}('') #4 /Users/baba/Sites/website/web/wp/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters(NULL, Array) #5 /Users/baba/Sites/website/web/wp/wp-includes/plugin.php(474): WP_Hook->do_action(Array) #6 /Users/baba/Sites/website/web/wp/wp-settings.php(587): do_action('init') #7 /Users/baba/Sites/website/web/wp-config.php(9): require_once('/Users/bantunes...') #8 /Users/baba/Sites/website/web/wp/wp-load.php(55): require_once in /Users/baba/Sites/website/web/app/themes/dlmc/app/setup.php on line 144

I hit the composer require acfbuilder within template folder, does it matter ? Cause we can install it bedrock or sage.

Thank you !

Solved by installing ACF Pro as a mu-plugin.
I don’t know why I thought ACF Builder was including the ACF stuff…

Hey Davey, did you ever get this sorted? In the same boat as you…

Hi bonlando,

had some trouble with this too. After some debugging I’ve figured out that you have to change \Roots\app_path() with app()->basePath(). So it looks like this:

add_action('init', function () {
    collect(glob(app()->basePath().'/app/fields/*.php'))->map(function ($field) {
        return require_once($field);
    })->map(function ($field) {
        if ($field instanceof FieldsBuilder) {
            acf_add_local_field_group($field->build());
        }
    });
});

Hope this helps.

Hi, I recently started using Sage and I am loving it. Also these composer packages for ACF are awesome. Theme development will never be the same again :slight_smile:

I’m struggling with a really simply issue I think but I can’t figure out what I am doing wrong.

I want to add a custom field to the post type ‘attachment’ For testing purposes I added the field to pages and post too, and it seems to be working because it shows up on posts and pages but not on an attachment page in the WordPress admin.

$gallery_options = new FieldsBuilder('gallery_options');

$gallery_options
    ->addUrl('custom_link')
    ->setLocation('post_type', '==', 'attachment' || 'post_type', '==', 'page' || 'post_type', '==', 'post');

add_action('acf/init', function () use ($gallery_options) {
    acf_add_local_field_group($gallery_options->build());
});

If you use ACF Composer to generate the field group –using wp acorn acf:field Example – then you should just be able to replace the post slug with your CPT slug:

<?php

namespace App\Fields;

use Log1x\AcfComposer\Field;
use StoutLogic\AcfBuilder\FieldsBuilder;

class Example extends Field
{
    /**
     * The field group.
     *
     * @return array
     */
    public function fields()
    {
        $example = new FieldsBuilder('example');

        $example
            ->setLocation('post_type', '==', 'attachment');

        $example
            ->addRepeater('items')
                ->addText('item')
            ->endRepeater();

        return $example->build();
    }
}
2 Likes

I’ll look into ACF composer. But I don’t know how this solves my issue. Because it’s my opinion that it should also be possible to do it like in my example. And I’m trying to figure out why this is not working.

When I look at the json output of this field it looks like there might be some required extra parameters, like filetype/extension. There is nothing in the docs like an example of how to write this. Is this a limitation of the way I’m building my fields now? Do i need to use ACF composer to get fields to show up on attachment posts types?

        "location": [
            [
                {
                    "param": "attachment",
                    "operator": "==",
                    "value": "image"
                }
            ]
        ],

Ah… I think I just answered my own question. I got confused because for a lot of locations you need to set the post type to equal == post_type_name but for attachments it’s not post_type => 'posttype' it is 'attachment' == 'attachmenttype/array'

->setLocation('attachment', '==', 'image' || 'post_type', '==', 'page' || 'post_type', '==', 'post');

I’m trying to up my theme development game and start using acf-composer. I have however run into a wall. According to the documentation I can create fields, partials, and blocks. The docs give an ‘Example’ of how I can add reusable partials to a field. But even the default example does not work. Or I am missing something? How do I add those fields to a block? ->addFields(defaultcontent::class) that does not add the fields to a block.

Is there some more documentation with other examples t hat shows a little bit more about how this is done? Also, should this be compatible with ACF 6? Because that’s what i’m using.

If I add the field directly to a block everthing works fine, but I can’t even get the Example to work with partials.

You have to use $this->get(Partial::class) (the docs mention this). I regret the naming convention but…

Try ->addFields($this->get(DefaultContent::class))

Thanks for the fast reply. When I use ->addFields(...) inside my block it works for a normal field. But when I use ->addFields() inside a field and try to get the fields within the partial they wont show.

So the syntax is right. But somehow the partial is not added to the field where I did addFields($this->get(DefaultContent::class))

I removed all fields and created them again with camelcases just to be sure.

This is the field where I call the partial.

class BlockDefaultContent extends Field
{
    /**
     * The field group.
     *
     * @return array
     */
    public function fields()
    {
        $blockdefaultcontent = new FieldsBuilder('blockdefaultcontent');

        $blockdefaultcontent
            ->setLocation('block', '==', 'acf/textandmedia');

        $blockdefaultcontent
            ->addText('title', [
                'label' => 'Titel',
                'instructions' => 'Titel van het blok',
                'required' => 1,
            ])
            ->addFields($this->get(DefaultContent::class));

        return $blockdefaultcontent->build();
    }
}

And this is My block where only the title from the above class shows up but not the fields inside the partial.

    public function fields()
    {
        $textandmedia = new FieldsBuilder('textandmedia');

        $textandmedia
            ->addFields($this->get(BlockDefaultContent::class));

        return $textandmedia->build();
    }

Just return the variable inside of the partial, don’t pass it to ->build().

Thanks for the quick reply,

I have.

First I created Fields/BlockDefaultContent
Second I created Fields/Partials/Defaultcontent
Third I created Blocks/TextAndMedia

The Field

<?php

namespace App\Fields;

use Log1x\AcfComposer\Field;
use StoutLogic\AcfBuilder\FieldsBuilder;

class BlockDefaultContent extends Field
{
    /**
     * The field group.
     *
     * @return array
     */
    public function fields()
    {
        $blockdefaultcontent = new FieldsBuilder('block_default_content');

        $blockdefaultcontent
            ->setLocation('block', '==', 'acf/text-and-media');

        $blockdefaultcontent
            ->addText('title', [
                'label' => 'Titel',
                'instructions' => 'Titel van het blok',
                'required' => 1,
            ])
            ->addFields($this->get(DefaultContent::class));


        return $blockdefaultcontent->build();
    }
}

The Partial

<?php

namespace App\Fields\Partials;

use Log1x\AcfComposer\Partial;
use StoutLogic\AcfBuilder\FieldsBuilder;

class DefaultContent extends Partial
{
    /**
     * The partial field group.
     *
     * @return \StoutLogic\AcfBuilder\FieldsBuilder
     */
    public function fields()
    {
        $defaultContent = new FieldsBuilder('default_content');
        $defaultContent
            ->addImage('image', [
                'label' => 'Afbeelding',
                'instructions' => 'Afbeelding voor het blok',
                'required' => 1,
            ]);

        return $defaultContent;
    }
}

The Block

<?php

...
    /**
     * The block field group.
     *
     * @return array
     */
    public function fields()
    {
        $textAndMedia = new FieldsBuilder('text_and_media');

        $textAndMedia
            ->addFields($this->get(BlockDefaultContent::class));

        return $textAndMedia->build();
    }

    /**
     * Return the items field.
     *
     * @return array
     */
    public function items()
    {
        return get_field('items') ?: $this->example['items'];
    }

    /**
     * Assets to be enqueued when rendering the block.
     *
     * @return void
     */
    public function enqueue()
    {
        //
    }
}

Pretty much the same thing I said in my last post. I suggest not over-complicating things where you don’t need too, but in any case, your BlockDefaultContent class is still returning build() and is not extending Partial despite you trying to use it in your block as a partial.