Default Date for Cloned Date Field

Support General Default Date for Cloned Date FieldResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #24910
    MartinMartin
    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.

    #24918
    Long NguyenLong 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
    MartinMartin
    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 NguyenLong 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 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.