Home › Forums › Feature Requests › Local fields defined by PHP : auto-generate unique keys
It would be nice if there was an option to let ACF automatically generate field/group/layout unique keys.
It should be possible to come up with a system where it just appends the group’s name, every parent field’s name, and the field’s own key, to form a key that’s more or less unique, and lastly add a number in the unlikely case where two fields fit the same bill.
Really, I love the local fields feature in that it allows me to create “fragments” which I can then repeat in several fields/field groups, but the one part that’s annoying is managing the unique keys.
The main issue I’ve run into when doing this is that the key must remain the same for each instance. Field keys can be dynamically generated, but once they are for a specific field they must then remain the same forever. What happens when you remove one, then some dynamic value changes and all the keys end up different. Or maybe you add a new one that gets generated before one that is already added and the field keys for the new one use the field keys for the old one and the old fields get new keys. When generating fields using PHP you also cannot depend on the field being defined already so you can’t even use get_field_object($field_key)
to see if it has been defined already.
Not only do field keys need to be unique and never change, but depending on where you’re adding the fields, the field names may also need to be unique and never change. The only time you can safely repeat a field name is if that field is a sub field of a repeater, flex or group field.
This is why it has to be optional. I’d see it as an option on the field group :
'auto_keys' => true
Of course, it could break if you’re not careful. But it wouldn’t break on its own. It would only break if you do something silly like name two field groups, or two fields in the same field group, the exact same. Which is just a bad idea in the first place in most cases. Also, ACF could warn you: “These fields have the same key. We put a number so it should be fine for now, but watch out if…”
But the worst that can happen is that you lose your data and it gets saved under a new key from that moment forward… which always could happen with ACF if you decided to rename a field anyway.
Also it could be overwritable. If I know this field in particular is possibly going to have the same key as this other field, I could set their keys manually.
I’m not the developer, but one thing I can probably safely say is that Elliot is not going to add something that can be easily broken. Also, right now when ACF generates field keys it uses uniqid()
which just generates a string based on the current time.
To auto generate unique keys that will not change, ACF would need to know the parent, whatever that might be.
You might want to bring this up with the developer by contacting him here https://www.advancedcustomfields.com/contact/
I would create a function that takes a prefix/suffix for a field key
function custom_add_field($prefix) {
$field = array(
'key' => 'field_'.$prefix.'_12345678',
//.....
);
}
You might want to look into the way the ACF generates field keys for clone fields.
The topic ‘Local fields defined by PHP : auto-generate unique keys’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.