StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
795 stars 62 forks source link

Xdebug throws Exception about field not found #78

Open djaevlen opened 5 years ago

djaevlen commented 5 years ago

Hi there,

We are having a basic theme options setup like this:

use StoutLogic\AcfBuilder\FieldsBuilder;

$options = new FieldsBuilder('options', [
    'style' => 'seamless'
]);

$options
    ->setLocation('options_page', '==', 'theme-general-settings');

$options
    ->addTab('Social Media')
        ->addGroup('social')
            ->addText('facebook_url')
            ->addText('instagram_url')
        ->endGroup()
    ->addTab('Contact')
        ->addText('street')
        ->addText('city')
        ->addText('country')
        ->addText('email')
        ->addText('phone')
    ->addTab('Defaults')
        ->addImage('hero_image_default', ['preview_size' => 'hero-full']);

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

Which outputs this:

(
    [key] => group_options
    [title] => Options
    [style] => seamless
    [fields] => Array
        (
            [0] => Array
                (
                    [type] => tab
                    [name] => social_media_tab
                    [label] => Social Media
                    [key] => field_options_social_media_tab
                )

            [1] => Array
                (
                    [type] => group
                    [name] => social
                    [label] => Social
                    [key] => field_options_social
                    [sub_fields] => Array
                        (
                            [0] => Array
                                (
                                    [type] => text
                                    [name] => facebook_url
                                    [label] => Facebook Url
                                    [key] => field_options_social_facebook_url
                                )

                            [1] => Array
                                (
                                    [type] => text
                                    [name] => instagram_url
                                    [label] => Instagram Url
                                    [key] => field_options_social_instagram_url
                                )

                        )

                )

            [2] => Array
                (
                    [type] => tab
                    [name] => contact_tab
                    [label] => Contact
                    [key] => field_options_contact_tab
                )

            [3] => Array
                (
                    [type] => text
                    [name] => street
                    [label] => Street
                    [key] => field_options_street
                )

            [4] => Array
                (
                    [type] => text
                    [name] => city
                    [label] => City
                    [key] => field_options_city
                )

            [5] => Array
                (
                    [type] => text
                    [name] => country
                    [label] => Country
                    [key] => field_options_country
                )

            [6] => Array
                (
                    [type] => text
                    [name] => email
                    [label] => Email
                    [key] => field_options_email
                )

            [7] => Array
                (
                    [type] => text
                    [name] => phone
                    [label] => Phone
                    [key] => field_options_phone
                )

            [8] => Array
                (
                    [type] => tab
                    [name] => defaults_tab
                    [label] => Defaults
                    [key] => field_options_defaults_tab
                )

            [9] => Array
                (
                    [type] => image
                    [name] => hero_image_default
                    [label] => Hero Image Default
                    [key] => field_options_hero_image_default
                    [preview_size] => hero-full
                )

        )

    [location] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [param] => options_page
                            [operator] => ==
                            [value] => theme-general-settings
                        )

                )

        )

)

If we turn on Xdebug, we get an Exception thrown called FieldNotFoundException. See stack trace here:

screenshot 2019-02-25 at 13 39 51

Everything works fine with Xdebug turned off. We can access the field data and everything, but Xdebug will throw an Exception, if we enable it.

And it's not only on our options ACF. It's on all our ACF fields we try to register.

What could cause this? From what we can see, is that when getFieldIndex($field) tries to find the index, the $this->getFields array is somehow empty, resulting in Exception to be thrown.
https://github.com/StoutLogic/acf-builder/blob/master/src/FieldManager.php#L198

stevep commented 5 years ago

Thanks for submitting, can you tell me what versions of ACF Builder and PHP you're using?

djaevlen commented 5 years ago

Yes sure, they are: ACF Builder 1.8.0 WordPress 5.0.3 PHP 7.2 We are running with an Bedrock installation setup.

djaevlen commented 5 years ago

Note that we are using "Local by Flywheel" to run our local dev env.

stevep commented 5 years ago

Looking through the code, https://github.com/StoutLogic/acf-builder/blob/master/src/FieldManager.php#L131-L140 calls getFieldIndex to determine if it the field name exists or not and then catches the exception if it doesn't. Since it gets caught, I don't know enough about XDebug to know why it is still complaining.

And while is is probably not the best idea to control logic with exceptions, that is what it is doing currently, fieldNameExists is piggy-backing on the very getFieldIndex which does have legit reasons for throwing the exception.

Our options seem to be figure out why XDebug is doing this, or rewriting fieldNameExists to not rely on catching exceptions.

csaborio001 commented 5 years ago

Thank goodness I found this, thought I was the only one and was going mad.

Looking at old threads (https://tinyurl.com/y6m23odj) seems like it's an issue with Xdebug. I tried setting xdebug.show_exception_trace=0 and the problem still persists - wondering if you found a workaround, @djaevlen ?

csaborio001 commented 5 years ago

Using VS Code, found a way for the exception not to explode in my face, so am back to happy debugging:

https://lh5.googleusercontent.com/L-2TjAY1OcAFxpWZS-8svW2lp-t9gktbbgVt1gu2dwMwUBLi3ebKcITtSHw

Basically bring up the breakpoints window and de-selecting the everything option.

Source: https://tinyurl.com/y6qzyk7b