Feature request: Interpret NULL value in field settings to prevent creation.
- This topic has 5 replies, 2 voices, and was last updated 3 years, 9 months ago by
Long Nguyen.
-
AuthorPosts
-
June 25, 2021 at 4:47 PM #29127
Jugibur
ParticipantHi,
in PHP it's so hard to setup an multi associated array with dynamically, different conditions.
So it would be great if in Meta Box a NULL Value could be interpreted as unvalid option / field, so we could the short if version (see $slider_activated below):
'fields' => [ [ 'name' => 'Art der Bilddarstellung', 'id' => 'imagepool_type', 'type' => 'radio', 'options' => [ 'global' => 'Wie globale Einstellung', 'single' => 'Ein Einzelbild', 'multiple' => 'Bildreihe', 'slider' => $slider_activated ? 'Überblendung / Karussell' : NULL, ], 'std' => 'global', ],
So if $slider_activated is false, the NULL value would prevent that Meta Box creats this 'slider' option here. If someone likes an empty label, he could still use an empty string '' for this purpose.
Unfortunately, currently with this NULL value we have just an radio option with an empty label.I'm curious what you guys think about it.
Juergen
June 27, 2021 at 10:12 AM #29142Long Nguyen
ModeratorHi,
To simplify this case, I think you can use conditional logic to show/hide another field slider based on the value of
$slider_activated
. Have you tried to use it?
https://docs.metabox.io/extensions/meta-box-conditional-logic/June 27, 2021 at 12:58 PM #29146Jugibur
ParticipantHi,
thanks for your reply.
Unfortunately conditional logic isn't for the same purposes.
My case is for my customor library, so if someone wants some options, I can active them for his theme.
And the conditional logic only allows me to set a new field for toggle and is needful at runtime – but for a setup configuration (and for adding / removing options in the same field) it isn't made I think.June 28, 2021 at 11:21 AM #29160Long Nguyen
ModeratorHi,
Thanks for your additional information.
So you can check the condition before register the meta box. Like this
if( $slider_activated == true ) { $meta_boxes[] = [ ... 'fields' => [ [ 'name' => 'Art der Bilddarstellung', 'id' => 'imagepool_type', 'type' => 'radio', 'options' => [ 'global' => 'Wie globale Einstellung', 'single' => 'Ein Einzelbild', 'multiple' => 'Bildreihe', 'slider' => 'Überblendung / Karussell', ], ], ]; ] }
June 28, 2021 at 12:11 PM #29162Jugibur
ParticipantThanks Long, yes this could be a way – but please note that my original question was only about controlling the entries in the options field.
So with your way I must repeat the stuff for the radio field just to add the "slider" option (in my example) to the imagepool_type field. But the imagepool_type should always been there. Hope you understand what I mean.
And extending an array down to the "options" is a bit harder / error-prone, but anyway – if it's not possible to extend the behavoir of Meta Box with the null check I try to write an "array builder helper" for my purpose. It was just a suggestion and thought from me for Meta Box.June 28, 2021 at 4:31 PM #29168Long Nguyen
ModeratorHi,
Have you tried to use the
select_tree
orcheckbox_tree
field type? The radio option is not supported to show trees.
https://docs.metabox.io/custom-select-checkbox-tree/ -
AuthorPosts
- You must be logged in to reply to this topic.