Custom Fields
Re-Envisioned
Support Meta Box Group Adding unique key to rows

  • Creator
    Topic
  • #2055
    killwil
    Participant

    Hello,

    I would like to add Unique keys to a text field named row_id in a data group.
    This filter is working for the first row but not for the second and others…
    Could you help ?

    add_filter ( 'rwmb_normalize_row_id_field', 'somefunction' );
    function somefunction($field) {
    	if ($field['std']=='') $field['std'] = uniqid();
    	return $field;	
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #2056
    killwil
    Participant

    I try with _value filter to save a blank field with a default value but no more luck…

    add_filter('rwmb_row_id_value', 'rwmb_row_id',10,3);
    function rwmb_row_id ( $new, $field, $old )
    {
    	if ($old == '') $new = uniqid();
        return $new ;
    }

    $old doesn’t give me the old value of the field…

    #2085
    Anh Tran
    Keymaster

    Hi,

    This is a hard question :). I think the filter allows you to change the value of whole group, as the group is the actual field you’re filtering. So, $old and $new are arrays (of sub-field values). I guess you’re using a cloneable group? If so, then $old and $new are arrays (of clones), each clone is an array of sub-fields.

    Let’s try with the code below:

    add_filter('rwmb_GROUP_ID_value', 'prefix_change_value',10,3);
    function prefix_change_value ( $new, $field, $old )
    {
        $text_field_id = 'SUB_FIELD_ID';
        foreach ( $new as $k => $clone )
        {
            $new[$k][$text_field_id] = uniqid();
        }
        return $new;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding unique key to rows’ is closed to new replies.