Log1x / acf-composer

Compose ACF Fields, Blocks, Widgets, and Option Pages with ACF Builder on Sage 10.
https://github.com/Log1x/acf-composer
MIT License
419 stars 57 forks source link

Flexible content with partials - Can't get it to work #175

Closed PetteriSeoka closed 1 year ago

PetteriSeoka commented 1 year ago

Hi!

First of all, great tool for ACF!

I have a slight problem. I'm trying to create a pagebuilder where I keep the layouts in separate partials. I have read your readme file and searched in endlessly in the Sage community without solving this issue. Could you please point me to the right direction?

Much appreciated,

Petteri


Test.php

<?php

namespace App\Fields;

use Log1x\AcfComposer\Field;
use StoutLogic\AcfBuilder\FieldsBuilder;
use App\Fields\Partials\Builder;
use App\Fields\Partials\Text;
use App\Fields\Partials\ListItems;

class Test extends Field
{
    /**
     * The field group.
     *
     * @return array
     */
    public function fields()
    {

        $test = new FieldsBuilder('Sidbyggare');

        $test
            ->setLocation('post_type', '==', 'page')
            ->and('post', '==', '2');

        $test
            ->addFields($this->get(Builder::class))

            ->addFields($this->get(ListItems::class));

        return $test->build();
    }
}

Builder.php

<?php

namespace App\Fields\Partials;

use Log1x\AcfComposer\Partial;
use StoutLogic\AcfBuilder\FieldsBuilder;
use App\Fields\Partials\Text;

class Builder extends Partial
{
    /**
     * The partial field group.
     *
     * @return \StoutLogic\AcfBuilder\FieldsBuilder
     */
    public function fields()
    {

        $builder = new FieldsBuilder('builder');

        $builder
            ->addFlexibleContent('komponenter', [
                'button_label' => 'Lägg till komponent',
            ])

            ->addFields($this->get(Text::class))
            ->endFlexibleContent();

        return $builder;
    }
}          

Text.php

<?php

namespace App\Fields\Partials;

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

class Text extends Partial
{
    /**
     * The partial field group.
     *
     * @return \StoutLogic\AcfBuilder\FieldsBuilder
     */
    public function fields()
    {
        $text = new FieldsBuilder('text');

        $text
            ->addLayout('layout', [
                'label' => 'Layout',
                'display' => '',
                'sub_fields' => [],
                'min' => '',
                'max' => '',
            ])
            ->addText('title')
            ->setWidth('30')
            ->addWysiwyg('content')
            ->setWidth('30')
            ->addRange('width');

        return $text;
    }
}
Log1x commented 1 year ago

I haven't used flexible content fields in awhile but as far as I know you need to remove ->addLayout() from $text:

$text = new FieldsBuilder('text');

$text
    ->addText('title')
    ->setWidth('30')
    ->addWysiwyg('content')
    ->setWidth('30')
    ->addRange('width');

return $text;

and then use ->addLayout on your $builder:

$builder = new FieldsBuilder('builder');

$builder
    ->addFlexibleContent('komponenter', [
        'button_label' => 'Lägg till komponent',
    ])
        ->addLayout($this->get(Text::class))
    ->endFlexibleContent();
PetteriSeoka commented 1 year ago

Great! This worked! Thanks! :)