vinkla / extended-acf

Register advanced custom fields with object-oriented PHP
MIT License
445 stars 61 forks source link

Use ACF Extended with Extended ACF (whats in the name?) #105

Closed nielsvr closed 2 years ago

nielsvr commented 2 years ago

Hi,

We love your implementation of ACF. But we are also a big fan of https://www.acf-extended.com. We are just not sure how to activate this programmaticly. We are currently extending the FlexibleContent class to add the settings the plugin uses;

class OurFlexibleContent extends Field
{
    function isExtended()
    {
        $this->settings['acfe_flexible_advanced'] = 1;
        $this->settings['acfe_flexible_stylised_button'] = 1;
        $this->settings['acfe_flexible_layouts_templates'] = 0;
        $this->settings['acfe_flexible_layouts_placeholder'] = 0;
        $this->settings['acfe_flexible_layouts_thumbnails'] = 1;
        $this->settings['acfe_flexible_layouts_settings'] = 0;
        $this->settings['acfe_flexible_layouts_state'] = 'user';
        $this->settings['acfe_flexible_modal_edit'] = array(
            'acfe_flexible_modal_edit_enabled' => '0',
            'acfe_flexible_modal_edit_size' => 'large',
        );

        $this->settings['acfe_flexible_async'] = array(
            0 => 'layout',
        );

        $this->settings['acfe_flexible_add_actions'] = array(
            0 => 'title',
            1 => 'toggle',
            2 => 'copy',
            3 => 'close',
        );

        return $this;
    }
}

Using this extended class works, except that ACF Extended settings are not visible. Is it even a possibility?

vinkla commented 2 years ago

I'm glad you like the package Niels 🙌 This project is not in any way connected to the ACF Extended package. With that said, it is probably not that hard to extend this package and make it work with ACF Extended. Could you share the output from the generate PHP from the dashboard?

nielsvr commented 2 years ago

I understand! And thanks for your time.

Here is an example output; where I see that my settings are different then I provide by code

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
    'key' => 'group_65adc059',
    'title' => 'Pagebuilder',
    'fields' => array(
        array(
            'key' => 'field_9fd6b59a',
            'label' => 'Secties',
            'name' => 'sections',
            'type' => 'flexible_content',
            'instructions' => 'Beheer de paginablokken',
            'required' => false,
            'conditional_logic' => false,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'button_label' => 'Blok toevoegen',
            'layouts' => array(
                array(
                    'key' => 'layout_4621e40b',
                    'name' => 'hero',
                    'label' => 'Hero',
                    'display' => 'block',
                    'sub_fields' => array(
                        array(
                            'key' => 'field_f9683f39',
                            'label' => 'Image',
                            'name' => 'image',
                            'type' => 'image',
                            'instructions' => '',
                            'required' => false,
                            'conditional_logic' => false,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'return_format' => 'array',
                            'preview_size' => 'medium',
                            'library' => 'all',
                            'min_width' => 0,
                            'min_height' => 0,
                            'min_size' => 0,
                            'max_width' => 0,
                            'max_height' => 0,
                            'max_size' => 0,
                            'mime_types' => '',
                        ),
                        array(
                            'key' => 'field_e07d64b2',
                            'label' => 'Heading',
                            'name' => 'heading',
                            'type' => 'textarea',
                            'instructions' => '',
                            'required' => false,
                            'conditional_logic' => false,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'new_lines' => 'br',
                            'rows' => 2,
                            'default_value' => '',
                            'maxlength' => '',
                            'placeholder' => '',
                        ),
                        array(
                            'key' => 'field_59d720db',
                            'label' => 'Text',
                            'name' => 'text',
                            'type' => 'textarea',
                            'instructions' => '',
                            'required' => false,
                            'conditional_logic' => false,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'new_lines' => 'br',
                            'rows' => 2,
                            'default_value' => '',
                            'maxlength' => '',
                            'placeholder' => '',
                        ),
                        array(
                            'key' => 'field_ce59dc1c',
                            'label' => 'Button 1',
                            'name' => 'button_1',
                            'type' => 'link',
                            'instructions' => '',
                            'required' => false,
                            'conditional_logic' => false,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'return_format' => 'array',
                        ),
                        array(
                            'key' => 'field_d159e0d5',
                            'label' => 'Button 2',
                            'name' => 'button_2',
                            'type' => 'link',
                            'instructions' => '',
                            'required' => false,
                            'conditional_logic' => false,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'return_format' => 'array',
                        ),
                    ),
                    'min' => '',
                    'max' => '',
                    'acfe_flexible_render_template' => false,
                    'acfe_flexible_render_style' => false,
                    'acfe_flexible_render_script' => false,
                    'acfe_flexible_thumbnail' => false,
                    'acfe_flexible_settings' => false,
                    'acfe_flexible_settings_size' => 'medium',
                    'acfe_flexible_modal_edit_size' => false,
                    'acfe_flexible_category' => false,
                ),
            ),
            'min' => '',
            'max' => '',
            'acfe_flexible_advanced' => false,
            'acfe_flexible_stylised_button' => false,
            'acfe_flexible_hide_empty_message' => false,
            'acfe_flexible_empty_message' => '',
            'acfe_flexible_layouts_templates' => false,
            'acfe_flexible_layouts_previews' => false,
            'acfe_flexible_layouts_placeholder' => false,
            'acfe_flexible_layouts_thumbnails' => false,
            'acfe_flexible_layouts_settings' => false,
            'acfe_flexible_async' => array(
            ),
            'acfe_flexible_add_actions' => array(
            ),
            'acfe_flexible_remove_button' => array(
            ),
            'acfe_flexible_layouts_state' => false,
            'acfe_flexible_modal_edit' => array(
                'acfe_flexible_modal_edit_enabled' => false,
                'acfe_flexible_modal_edit_size' => 'large',
            ),
            'acfe_flexible_modal' => array(
                'acfe_flexible_modal_enabled' => false,
                'acfe_flexible_modal_title' => false,
                'acfe_flexible_modal_size' => 'full',
                'acfe_flexible_modal_col' => '4',
                'acfe_flexible_modal_categories' => false,
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'page',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => array(
    ),
    'active' => true,
    'description' => '',
    'show_in_rest' => false,
));

endif;
vinkla commented 2 years ago

The output looks pretty straight forward and you're on the right track.

Using this extended class works, except that ACF Extended settings are not visible. Is it even a possibility?

I'm not sure why but ACF only displays field groups registered within the dashboard. That is a trade-off of registering field groups with PHP.

nielsvr commented 2 years ago

It seems my little extension doesn't work; if I add a setting called 'test'; this isn't in the PHP output from the dashboard. It seems that the acfe_ settings are already there as a default; and not by my code. Do I need to do anything else to trigger the extension? I call it by it's extended name;

OurFlexibleContent::make('Secties', 'sections')
>isExtended()
use WordPlate\Acf\Fields\FlexibleContent as Field;

class OurFlexibleContent extends Field
{
    public function isExtended(): self
    {

        $this->settings['test'] = 'test';

        return $this;
    }
}

Dashboard:

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
    'key' => 'group_65adc059',
    'title' => 'Pagebuilder',
    'fields' => array(
        array(
            'key' => 'field_9fd6b59a',
            'label' => 'Secties',
            'name' => 'sections',
            'type' => 'flexible_content',
            'instructions' => 'Beheer de paginablokken',
            'required' => false,
            'conditional_logic' => false,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'button_label' => 'Blok toevoegen',
            'layouts' => array(
                array(
                    'key' => 'layout_4621e40b',
                    'name' => 'hero',
                    'label' => 'Hero',
                    'display' => 'block',
                    'sub_fields' => array(
                        array(
                            'key' => 'field_f9683f39',
                            'label' => 'Image',
                            'name' => 'image',
                            'type' => 'image',
                            'instructions' => '',
                            'required' => false,
                            'conditional_logic' => false,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'return_format' => 'array',
                            'preview_size' => 'medium',
                            'library' => 'all',
                            'min_width' => 0,
                            'min_height' => 0,
                            'min_size' => 0,
                            'max_width' => 0,
                            'max_height' => 0,
                            'max_size' => 0,
                            'mime_types' => '',
                        ),
                        array(
                            'key' => 'field_e07d64b2',
                            'label' => 'Heading',
                            'name' => 'heading',
                            'type' => 'textarea',
                            'instructions' => '',
                            'required' => false,
                            'conditional_logic' => false,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'new_lines' => 'br',
                            'rows' => 2,
                            'default_value' => '',
                            'maxlength' => '',
                            'placeholder' => '',
                        ),
                        array(
                            'key' => 'field_59d720db',
                            'label' => 'Text',
                            'name' => 'text',
                            'type' => 'textarea',
                            'instructions' => '',
                            'required' => false,
                            'conditional_logic' => false,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'new_lines' => 'br',
                            'rows' => 2,
                            'default_value' => '',
                            'maxlength' => '',
                            'placeholder' => '',
                        ),
                        array(
                            'key' => 'field_ce59dc1c',
                            'label' => 'Button 1',
                            'name' => 'button_1',
                            'type' => 'link',
                            'instructions' => '',
                            'required' => false,
                            'conditional_logic' => false,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'return_format' => 'array',
                        ),
                        array(
                            'key' => 'field_d159e0d5',
                            'label' => 'Button 2',
                            'name' => 'button_2',
                            'type' => 'link',
                            'instructions' => '',
                            'required' => false,
                            'conditional_logic' => false,
                            'wrapper' => array(
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'return_format' => 'array',
                        ),
                    ),
                    'min' => '',
                    'max' => '',
                    'acfe_flexible_render_template' => false,
                    'acfe_flexible_render_style' => false,
                    'acfe_flexible_render_script' => false,
                    'acfe_flexible_thumbnail' => false,
                    'acfe_flexible_settings' => false,
                    'acfe_flexible_settings_size' => 'medium',
                    'acfe_flexible_modal_edit_size' => false,
                    'acfe_flexible_category' => false,
                ),
            ),
            'min' => '',
            'max' => '',
            'acfe_flexible_advanced' => false,
            'acfe_flexible_stylised_button' => false,
            'acfe_flexible_hide_empty_message' => false,
            'acfe_flexible_empty_message' => '',
            'acfe_flexible_layouts_templates' => false,
            'acfe_flexible_layouts_previews' => false,
            'acfe_flexible_layouts_placeholder' => false,
            'acfe_flexible_layouts_thumbnails' => false,
            'acfe_flexible_layouts_settings' => false,
            'acfe_flexible_async' => array(
            ),
            'acfe_flexible_add_actions' => array(
            ),
            'acfe_flexible_remove_button' => array(
            ),
            'acfe_flexible_layouts_state' => false,
            'acfe_flexible_modal_edit' => array(
                'acfe_flexible_modal_edit_enabled' => false,
                'acfe_flexible_modal_edit_size' => 'large',
            ),
            'acfe_flexible_modal' => array(
                'acfe_flexible_modal_enabled' => false,
                'acfe_flexible_modal_title' => false,
                'acfe_flexible_modal_size' => 'full',
                'acfe_flexible_modal_col' => '4',
                'acfe_flexible_modal_categories' => false,
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'page',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => array(
    ),
    'active' => true,
    'description' => '',
    'show_in_rest' => false,
));

endif;
vinkla commented 2 years ago

...this isn't in the PHP output from the dashboard.

That is correct. It wont show in the dashboard. It will still work though. You can test it if you call the get function:

print_r(
    OurFlexibleContent::make('Secties', 'sections')->isExtended()->get()
);
nielsvr commented 2 years ago

Found the solution, when you use $this->config->set() it works!

use WordPlate\Acf\Fields\FlexibleContent as Field;

class OurFlexibleContent extends Field
{
    public function isExtended(): self
    {
        $this->config->set('acfe_flexible_advanced', 1);
        $this->config->set('acfe_flexible_stylised_button', 1);
        $this->config->set('acfe_flexible_layouts_templates', 0);

        $this->config->set('acfe_flexible_layouts_placeholder', 0);
        $this->config->set('acfe_flexible_layouts_thumbnails', 1);
        $this->config->set('acfe_flexible_layouts_settings', 0);
        $this->config->set('acfe_flexible_layouts_state','user');
        $this->config->set('acfe_flexible_modal_edit', array(
            'acfe_flexible_modal_edit_enabled' => '0',
            'acfe_flexible_modal_edit_size' => 'large',
        ));

        $this->config->set('acfe_flexible_async', array(
            0 => 'layout',
        ));

        $this->config->set('acfe_flexible_add_actions', array(
            0 => 'title',
            1 => 'toggle',
            2 => 'copy',
            3 => 'close',
        ));

        return $this;
    }
}

This way you can setup ACFE programmaticly.

vinkla commented 2 years ago

Oh, you're using an older version of the package. That is why you've to use ->config instead of ->settings.

nielsvr commented 2 years ago

Ah, I see now! I'll try to upgrade.