Custom Fields
Re-Envisioned
Support General Feature request: Interpret NULL value in field settings to prevent creation.

  • Creator
    Topic
  • #29127
    Jugibur
    Participant

    Hi,

    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #29142
    Long Nguyen
    Moderator

    Hi,

    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/

    #29146
    Jugibur
    Participant

    Hi,
    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.

    #29160
    Long Nguyen
    Moderator

    Hi,

    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',
                    ],
                ],
            ];
        ]
    }
    #29162
    Jugibur
    Participant

    Thanks 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.

    #29168
    Long Nguyen
    Moderator

    Hi,

    Have you tried to use the select_tree or checkbox_tree field type? The radio option is not supported to show trees.
    https://docs.metabox.io/custom-select-checkbox-tree/

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.