Support

Account

Home Forums Backend Issues (wp-admin) page_link empty when adding layout via hook

Solving

page_link empty when adding layout via hook

  • Hi.

    I’m adding layouts to a flexible content field with the following code:

    add_filter('acf/load_field/key=field_557ff18bab6a7', 'add_test_layout');
    
    function add_test_layout($field) {
        array_push($field['layouts'], 
            array (
                'key' => 'test_layout',
                'name' => 'test_layout',
                'label' => 'test_layout',
                'display' => 'row',
                'sub_fields' => array(
                        array (
                            'key' => 'field_test_link',
                            'label' => 'Link',
                            'name' => 'test_link',
                            'prefix' => '',
                            'type' => 'page_link',
                            'instructions' => '',
                            'required' => 0,
                            'conditional_logic' => 0,
                            'wrapper' => array (
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'post_type' => array(),
                            'taxonomy' => array(),
                            'allow_null' => 0,
                            'multiple' => 0,
                        ),
                    ),
                'min' => '',
                'max' => '',
            )
        );
        return $field;
    }

    The fieldgroup and the first layout is created with the ACF GUI (at least in this test-case). If the ‘test_layout’ contains a page_link field it doesn’t work. I’m not able to choose a page.

    I tried this on a clean WordPress installation with 4.2.2 and ACF Pro 5.2.6

  • Hi @panic175

    Have you looked at the values of a page_link element created from the GUI to make sure you’re not missing any parameters or values in your own?

  • Hi @Jonathan.

    There is no difference.
    When I export the working page_link element from the GUI I get this code:

    array (
        'key' => 'field_557ff199ab6a8',
        'label' => 'Link_aus_GUI',
        'name' => 'link_aus_gui',
        'type' => 'page_link',
        'instructions' => '',
        'required' => 0,
        'conditional_logic' => 0,
        'wrapper' => array (
            'width' => '',
            'class' => '',
            'id' => '',
        ),
        'post_type' => array (),
        'taxonomy' => array (),
        'allow_null' => 0,
        'multiple' => 0,
    ),
  • Hi.

    Sadly I still could not resolve my problem. I found out that a similar problem also occurs in the gallery-field.
    There, I’m not able to edit a photo after I inserted it.

    Seems to be some bigger problem with the Plugin-JavaScript and hooked fields in general.

  • Hi @panic175

    Okay. I’ll make a bug issue for this on the Github repo and we’ll see what happens 🙂

    It’s probably as you say that the javascripts for the media module and select2 dropdown aren’t firing when creating a field in this way. Do you get any JS errors in the console that I could post?

  • Hi @jonathan

    Thanks for your help.
    No, my console stays blank.

  • Hi @panic175

    Thanks for the bug report.

    The issue here is quite simple.
    During your ajax request, the flexible content field is never loaded, therefore, your extra page layout and page_link field is never registered.

    I have a few questions:
    – Is your field group registered via code?
    – Can this layout and field be added in a different way (not within a load_field filter)?

    Thanks
    E

  • Hi @elliot

    – Is your field group registered via code?

    I have tried both, neither worked. In production I want to register it via code.

    – Can this layout and field be added in a different way (not within a load_field filter)?

    Not that I know of. I try to achieve that the individual layouts can be loaded with a single call. So that every layout is a bower component which includes field layout, include of the JavaScript and other necessary functions.

    Greetings
    Patrick

  • During your ajax request, the flexible content field is never loaded, therefore, your extra page layout and page_link field is never registered.

    Is there a way to manually load the fields with another ajax request?

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘page_link empty when adding layout via hook’ is closed to new replies.