Support

Account

Home Forums General Issues Dynamically create a field associated with another field and merge output.

Solving

Dynamically create a field associated with another field and merge output.

  • This is somewhat complicated but I’ll be super grateful for any help…

    I’m using ACF Pro and I’m trying to add the option of attaching a unit dropdown to Number fields, to use in cases where the value is for CSS. I can’t figure out how to get it to work though.

    At the moment I have what you see in the attached image, so now I’m trying to get it to work.

    So far I have:

    • Added a custom setting to the Number field type called “Enable Units” via acf_render_field_setting(). It’s working – I can toggle “Yes” or “No” to display or not display the Unit dropdown.
    • Written a function containing a basic HTML <select> dropdown and hooked it in after the field, using acf/render_field/type=number. I’ve just done this for testing purposes, to see if the hook worked and the dropdown would appear after the field. It does.

    I’m stuck after that. Obviously the value does not save, so my next step is to figure out how to get the value to save, in such a way that it’s somehow associated with the Number field, and then call it to be used in theme files.

    What I was thinking was that I could somehow:

    1. Manually create an ACF select field using acf_add_local_field() with the unit values, and give it a key that follows a naming convention using the Number field’s ID, like $field['id'] . '_units' or something.
    2. Use acf/format_value to merge the saved number and units together for use in get_field().

    I think I could probably figure out #2 (assuming it would be the correct hook), but I’m stuck on #1. I can’t seem to find a way to dynamically create a field that can be used in acf/render_field/type=number to display the Units dropdown next to the Number field, and which saves correctly.

    The other option I thought of would be to save whatever the user selects as normal WordPress post meta, by hooking into however Gutenberg saves post meta… I have no idea how to do that but if the above idea won’t work then I’ll set off researching it. Perhaps a hidden input field…

    Obviously I know I could add a normal select field in ACF after the number field, but I’m testing this inside a Gutenberg block (using ACF 5.8 beta 3) and I’d like to format it a certain way, and I plan to need a Units field associated with a lot of number fields, so ideally I’d like not to have to manually add a field and remember which class to use every time I need it. It’d be so easy if I could just toggle my “Enable Units” setting for the field and that’d be it. In the end I’ll do that if I have to, I was just hoping to find a more efficient way.

    Thanks for your help, hope this makes sense!

  • Any word on this? Iam trying to do the same thing but with a repeater field.

  • There isn’t any real way to do what you’re trying to do. Each ACF field only saves a single value. From the user entering the information perspective, that “units” drop down would need to be an entirely different ACF field.

    Rather than using a single field I would use 2 fields and use the width settings to put them side by side rather then attempt to use a single field.

    You could probably do this if you created a new field type https://www.advancedcustomfields.com/resources/creating-a-new-field-type/

  • Thank you very much for your input, I used your input and created a new field type called repeater-column-select, see attachment for preview.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.