Custom Fields
Re-Envisioned
Support General Default Date for Cloned Date Field

  • Creator
    Topic
  • #24910
    Martin
    Participant

    Hi,

    I have a date field (job_update_date) in a cloned group (group_updates). I need to be able to set this field to default to the current date each time a new clone is created as it seems the Today button behaviour is only to highlight entry in the popup calendar and not to actually populate the date field (unlike the time field which populates properly when the Now button is pressed).

    This is part of the code so far.

                [
                    'id'            => $prefix . 'group_updates',
                    'type'          => 'group',
                    'fields'        => [
                        [
                            'id'   => $prefix . 'job_update_time',
                            'name' => esc_html__( 'Task Completion Time', 'text-domain' ),
                            'type' => 'time',
                        ],
                        [
                            'id'            => $prefix . 'job_update_date',
                            'type'          => 'date',
                            'name'          => esc_html__( 'Task Date', 'text-domain' ),
                            'clone_default' => true,
                        ],
    

    I’m using Builder if that makes any difference.

    What is the best way to achieve this?

    Thanks.

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

    Hi Martin,

    You should use the code to set the default value (current date) for the first field, then clone fields will inherit the default value.

    [
        'id'            => $prefix . 'job_update_date',
        'type'          => 'date',
        'name'          => esc_html__( 'Task Date', 'text-domain' ),
        'clone'         => true,
        'clone_default' => true,
        'std'           => date("Y-m-d"),
    ],

    Get more details about cloneable sub-fields here https://docs.metabox.io/extensions/meta-box-group/#clone-default-values.

    #24920
    Martin
    Participant

    Thanks for that.

    A follow up question (probably a bit of a beginners’ question) – is it possible to set the value for the std in the Builder?

    I’ve popped it in both the Default value box for the field and as a Custom attribute but both result in the function being set in single quotes in the code and therefore the field value becomes the quoted string rather than the result of the function.

    #24924
    Long Nguyen
    Moderator

    Hi,

    No, it’s not possible. The Builder does not support using a function in the input field settings. So I recommend using the code to run a PHP function.

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