acf-extended / ACF-Extended

🚀 All-in-one enhancement suite that improves WordPress & Advanced Custom Fields
https://www.acf-extended.com
238 stars 27 forks source link

acfe_form does not load existing values for relationship field #90

Closed stasou closed 2 years ago

stasou commented 2 years ago

Describe the bug When rendering a form, the relationship field renders correctly, with all the available options. However, existing values do not load. When saving, however, the values save (after some processing in the theme).

To Reproduce

Expected behavior The form should load with the field containing existing values

Screenshots In wp-admin (post.php) image

In template image

WordPress & ACF WordPress version: 5.8.1 ACF Pro version: 5.10.2

Add any other context about the problem here.

There is a request when loading the field in the template. This request only brings back the options without the values: image image

acf-extended commented 2 years ago

Hello,

Thanks for the feedback!

I think you confused ACF Form and ACFE Form. While ACF Extended Form might looks similar ACF Form, it has a lot more features and is managed mainly from the UI.

In your case, you tried to apply the ACF Form logic (using the post_id argument to load values from a post), to ACFE Form. This is not how you load values with ACFE Form. I would recommend to first take a look at the integration documentation.

You should render your form using the form name or ID, and not override the post_id argument (unless you relly know what you're doing). Usage example:

acfe_form('my-form');

In order to Save or Load values in fields, you have to add a "Post Action" (if you want to save or load values from a post), then under the "Load" tab, enable "Load Values", choose the value source (Current post, or a custom post id), and finally check the checkboxes of the fields you want to load. See screenshot.

If you want to use a "Source" based on a PHP value, you will have to pass a custom data during the form render, like this:

acfe_form(array(
    'name' => 'my-form',
    'custom_key' => 'custom_value',
));

And then use the Template Tag {form:custom_key} in the "Post Action" "Source" setting. As explained in the documentation.

Hope it helps!

Have a nice day!

Regards.

stasou commented 2 years ago

I know this is closed, but for the history, this does not help.

The post I'm trying to edit is loaded in an Elementor template. The post id in the main loop is the ID of the Elementor page. The ID of the post that is edited is different and dynamic.

ID: Elementor Page (main loop)
    |------------------ Index page
        |-------------- Edit post from index
            |---------- different post ID

So, if there is no way to dynamically set the post ID, the only way to go around this is with a hack:

I use the helper to render the form, I load the values inline using PHP and, then, enqueue a script to dynamically add the existing values.

It works, but it is very hacky.

acf-extended commented 2 years ago

Hello,

In your original report you use the acfe_form() function to render the form, so I used the exact same method to explain what is wrong in your logic and how to use it. Now you say that you don't really use it, you have different requirements, and my answer does not help.

This is not really fair don't you think? If you have specific requirements, post it in the initial report because I cannot guess it. Also note that my initial answer is still valid, you should not confuse ACF Form and ACFE Form, and changing the post_id argument will not load values in the form.

Please read the Integration Documentation I linked in my previous answer, you can pass a custom data to the form render using acfe/form/load hook, even with the shortcode, and then use that value in the UI.

Regards.

stasou commented 2 years ago

Sorry for the inconvenience, I should have noted that I want to pick values not from main post, but from different ID.

I do use acfe_form() to render the form. I just pick the values with custom logic, since there is no possibility to do this out of the box. I am not saying this is wrong, I just thought there was something I had missed or something missing.

I understand that using the acf_form syntax to render the form is null effort, though the form still, miraculously, renders correctly to the possible extents.

Your answer is valid, I understand that my case scenario, to render values from dynamic post id, is not covered and this is ok.

acf-extended commented 2 years ago

Hello,

Please read the Integration Documentation. you can pass a dynamic data to the form render using acfe/form/load hook, which will work with the [acfe_form] shortcode.

Then you can use the Template Tag {form:custom_key} in the "Post Action" "Source" setting in the UI to retrieve that value. As explained in the documentation.

Regards.

JoKolov commented 2 years ago

Hello,

just in case someone is looking for the same subject.

I was able to load values in the form using the acfe documentation like this :

$field1_value = get_field( 'field1_name', $some_ID );
$field2_value = get_field( 'field2_name', $some_ID );

acfe_form( [
    'name' => 'myform',
    'some_id' => $some_ID, // ID of the post object to retrieve it in form actions
    'map' => [
        'field1_key' => [
            'value' => $field1_value ?: 'default value',
        ],
        'field2_key' => [
            'value' => $field2_value ?: 'default value',
        ],
    ],
    // extra settings
] );

NOTE 1 : in 'map' setting, you must use field KEY, not the field name. This could be an improvement for future. NOTE 2 : you could use get_field_object() instead get_field() to access field key and value dynamically and prepare the 'map' setting before.

Hope this helps. Enjoying using ACF Extended :)

Best regards.