• Resolved dimizu

    (@dimizu)


    I use your wunderfull CMB2 toolkit for a new project. I’m very happy with it. But now, I run into a problem with repeating fields.

    I cannot handle repeating fields with some third plugins, because your repeating fields store multiple data in one custom field (in json format). This is a good idea for save multiple data, but it’s a problem for me, because I have situations in there I must have separate fields for all data.

    My question is:

    a) Is there an option to say: please store repeatable fields in separate custom fields? (Similar Advanced Custom Fields. ACF save the repeater field in a wp_postmeta table. Example repeater field = gallery and contains 2 sub fields called “image” and “description”:

    // meta_key                  meta_value
    gallery                      2                 // number of rows
    gallery_0_image              6                 // sub field value
    gallery_0_description        "some text"       // sub field value
    gallery_1_image              7                 // sub field value
    gallery_1_description        "some text"       // sub field value

    b) If a) is not possible, how can build and hook a callback function with this functionality?
    On change of field X, get the the data of field X, modify the data and store the modified data in field Y.
    Is this possible?

    https://www.ads-software.com/plugins/cmb2/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If you’re for sure dealing with repeatable FIELDS and not GROUPS, then there is this parameter available to store in separate rows in the database:

    'multiple' => true

    If you need to get more meticulous in how it’s all saved, more than what’s present and what above provides, there is this filter you can use to save as you need:

    /**
     * Filter whether to override saving of meta value.
     * Returning a non-null value will effectively short-circuit the function.
     *
     * @since 2.0.0
     *
     * @param null|bool $check  Whether to allow updating metadata for the given type.
     *
     * @param array $args {
     *     Array of data about current field including:
     *
     *     @type string $value    The value to set
     *     @type string $type     The current object type
     *     @type int    $id       The current object ID
     *     @type string $field_id The ID of the field being updated
     *     @type bool   $repeat   Whether current field is repeatable
     *     @type bool   $single   Whether current field is a single database row
     * }
     *
     * @param array $field_args All field arguments
     *
     * @param CMB2_Field object $field This field object
     */
    $override = apply_filters( 'cmb2_override_meta_save', null, $a, $this->args(), $this );
    Thread Starter dimizu

    (@dimizu)

    ‘multiple’ => true sounds good.

    But I see no difference with multiple => true or false

    I have this:

    $cmb->add_field( array(
    		'name' => __( 'Datum', 'cmb2' ),
    		'id' => $prefix . 'event_datum',
            'desc'       => __( 'Datum Events', 'cmb2' ),
    		'type' => 'text_date_timestamp',
    		'repeatable' => true,
    		'multiple' => true,
    		'date_format' => 'd.m.Y'
    	) );

    I insert 2 dates:

    10.02.2016
    11.02.2016

    print_r of the return from get_post_meta():

    ["dmz_event_datum"]=>
      array(1) {
        [0]=>
        string(40) "a:2:{i:0;i:1455062400;i:1;i:1455148800;}"
      }

    You see the 2 dates are store in 1 custom field and not in 2 custom fields. I would like to have an output like this:

    ["dmz_event_datum_0"]=>
      array(1) {
        [0]=>
        string(10) "1455062400"
      }
      ["dmz_event_datum_1"]=>
      array(1) {
        [0]=>
        string(10) "1455148800"
      }

    Is this not possible?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    It’s possible it’s not for that specific type of field, text_date_timestamp, which also would make sense. Keeping the specific example above together would be a case of keeping the start and end date for the event. Splitting that would make a row for just the start, and then just the end.

    Having a 2nd event should create the second row in the db with its own start/end pairing.

    Also there was the filter I pointed out that could help if you’re still intent to put the start and end dates in their own rows, but I don’t have any specific example available for what to do with it.

    Thread Starter dimizu

    (@dimizu)

    Ok I understand that the multiple option is not possibile for date fields. Correct? Also not for date fields with text_time?

    The solution with start and end date is not ideal for me. Because I have 1, 2, 3, 4, 5… X dates. Not only one for start and one for end.

    You filter seems the solution. Can you tell me the code for a functio n that can save all dates from this field:

    $cmb->add_field( array(
    		'name' => __( 'Datum', 'cmb2' ),
    		'id' => 'event_datum',
            'desc'       => __( 'Datum Events', 'cmb2' ),
    		'type' => 'text_date_timestamp',
    		'repeatable' => true,
    		'date_format' => 'd.m.Y'
    	) );

    in X separate meta fields with the name ‘event_datum_0’, ‘event_datum_1′,’event_datum_2′,’event_datum_3’,… ‘event_datum_3’

    That would solve my problem. That would be nice. (Unfortunately, I find no similar example.)

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I would need to test to confirm, but it makes sense that you’d want to keep start/end dates together like that. Regarding your second part, you would be able to have multiple start/end date pairs.

    Start End
    Row 1: May 10th – May 12th //event_datum_0
    Row 2: May 20th – May 22nd //event_datum_1
    Row 3: May 30th – June 1st. //event_datum_2

    The way you were describing wanting would be like so:

    Row 1: May 10th
    Row 2: May 12th
    Row 3: May 20th
    … and so on.

    To be fair to you, it does look like it’s not behaving as expected, and it is only a single date that you’re setting per row, not pairings. So the following may be best route.

    From the looks of it, the 2nd argument passed in to the filter, in today’s case the $a variable, has a number of things associated:

    https://cloudup.com/cePJqez7_oC

    Post type, post ID, field ID, repeat and single which are probably from the field params, and then the value array which will hold the individual inputs from my quick example that I added to my local dev.

    function custom_saving( $empty, $a, $args, $object ) {
    	// Save $a['value'] indexes as post meta in whatever fashion you want.
    }
    add_filter( 'cmb2_override_meta_save', 'custom_saving', 10, 4 );
    Thread Starter dimizu

    (@dimizu)

    Ok, thanks for the reply.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Save repeatable fields separately’ is closed to new replies.