ACF addFlexibleContent- having the layouts as partials stored in classes

Folks, Im really starting to lose my mind :smiley:

Im using the ACF builder, but not ACF Composer-
I’m trying to build a wrapper class that dynamically builds the flexiblecontent which all partials that have been passed through. All “partials” are stored within their own classes.

My issue is, that whenever I break the chain with a semicolon it says “addLayout” is not available.

$fieldsBuilder
        ->addFlexibleContent('flexible_content_field', [
            'label' => $args['label'] ?? 'Flexible Content',
            'instructions' => $args['instructions'] ?? '',
            'button_label' => $args['button_label'] ?? 'Add Row',
            'min' => $args['min'] ?? '',
            'max' => $args['max'] ?? ''
        ])
        ->addLayout('layout', [
            'label'      => 'Layout',
            'display'    => 'block',
            'sub_fields' => ['text_example'],
            'min'        => '',
            'max'        => '',
        ])
        ->addText('text_example', [
            'label'             => 'Text',
            'instructions'      => '',
            'required'          => 0,
            'conditional_logic' => [],
            'wrapper'           => [
                'width' => '',
                'class' => '',
                'id'    => '',
            ],
            'default_value'     => '',
            'placeholder'       => '',
            'prepend'           => '',
            'append'            => '',
            'maxlength'         => '',
        ])
        ->endFlexibleContent()
        ;

My example Layout Class

<?php

namespace App\Partials;

use StoutLogic\AcfBuilder\FieldsBuilder;

class Content
{
    public static function getLayout($fieldsBuilder)
    {


       return $fieldsBuilder->addLayout('content', [
                'label' => 'Content',
                'display' => 'block',
                'sub_fields' => [],
                'min' => '',
                'max' => '',
            ])
            ->addText('text_example', [
                'label' => 'Text',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => [],
                'wrapper' => [
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ],
            ]);
    }
}

my wrapper class:

<?php

namespace App\Partials\Base;

use StoutLogic\AcfBuilder\FieldsBuilder;

class PartialBuilder
{
    public static function buildFlexibleContent($fieldsBuilder, array $partials, $args = [])
    {
        $test= 'addFlexibleContent';
        $fieldsBuilder
        ->addFlexibleContent('flexible_content_field', [
            'label' => $args['label'] ?? 'Flexible Content',
            'instructions' => $args['instructions'] ?? '',
            'button_label' => $args['button_label'] ?? 'Add Row',
            'min' => $args['min'] ?? '',
            'max' => $args['max'] ?? ''
        ])
        ;

        if ($partials) {
            foreach ($partials as $partial) {
                $methodName = "\\App\\Partials\\" . ucfirst($partial) . "::getLayout";
                if (is_callable($methodName)) {
                    call_user_func($methodName, $fieldsBuilder);
                }
            }
        }

        // dd($fieldsBuilder); 
        return $fieldsBuilder;
    }
}

Have you checked out @Log1x 's ACF Builder cheatsheat? You might find some clues there.

2 Likes

I don’t really get it, would appreciate your help :smiling_face_with_tear:

The way its used in the docs ends up in a “no such function: addLayout” Error

acf-builder-wiki/Flexible-Content.md at f0ca8bce07faf3ebde65c91e896cf495e17779ba · StoutLogic/acf-builder-wiki · GitHub might also be helpful.

2 Likes

I found an old project that uses flexibile content and it looks like I added->getField('flexible_content_field') before addLayout:

 $fieldsBuilder
        ->addFlexibleContent('flexible_content_field', [
            'label' => $args['label'] ?? 'Flexible Content',
            'instructions' => $args['instructions'] ?? '',
            'button_label' => $args['button_label'] ?? 'Add Row',
            'min' => $args['min'] ?? '',
            'max' => $args['max'] ?? ''
        ])
        ->getField('flexible_content_field')
            ->addLayout('layout', [
               'label'      => 'Layout',
               'display'    => 'block',
               'sub_fields' => ['text_example'],
                'min'        => '',
                'max'        => '',
        ])
            ->addText('text_example', [
                'label'             => 'Text',
                'instructions'      => '',
                'required'          => 0,
                'conditional_logic' => [],
                'wrapper'           => [
                'width' => '',
                'class' => '',
                'id'    => '',
            ],
            'default_value'     => '',
            'placeholder'       => '',
            'prepend'           => '',
            'append'            => '',
            'maxlength'         => '',
        ])
        ->endFlexibleContent()
        ;
1 Like

Best practise for Flexible Content · Issue #257 · Log1x/acf-composer · GitHub has some info on this for ACF Composer.

1 Like
        ->getField('flexible_content_field')

That one was missing, thank you :heart:
Now it works like a charm