name = 'group'; $this->label = __( 'Group', 'acf' ); $this->category = 'layout'; $this->description = __( 'Provides a way to structure fields into groups to better organize the data and the edit screen.', 'acf' ); $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-group.png'; $this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/group/', 'docs', 'field-type-selection' ); $this->defaults = array( 'sub_fields' => array(), 'layout' => 'block', ); $this->have_rows = 'single'; // field filters $this->add_field_filter( 'acf/prepare_field_for_export', array( $this, 'prepare_field_for_export' ) ); $this->add_field_filter( 'acf/prepare_field_for_import', array( $this, 'prepare_field_for_import' ) ); } /* * load_field() * * This filter is appied to the $field after it is loaded from the database * * @type filter * @since 3.6 * @date 23/01/13 * * @param $field - the field array holding all the field options * * @return $field - the field array holding all the field options */ function load_field( $field ) { // vars $sub_fields = acf_get_fields( $field ); // append if ( $sub_fields ) { $field['sub_fields'] = $sub_fields; } // return return $field; } /* * load_value() * * This filter is applied to the $value after it is loaded from the db * * @type filter * @since 3.6 * @date 23/01/13 * * @param $value (mixed) the value found in the database * @param $post_id (mixed) the $post_id from which the value was loaded * @param $field (array) the field array holding all the field options * @return $value */ function load_value( $value, $post_id, $field ) { // bail early if no sub fields if ( empty( $field['sub_fields'] ) ) { return $value; } // modify names $field = $this->prepare_field_for_db( $field ); // load sub fields $value = array(); // loop foreach ( $field['sub_fields'] as $sub_field ) { // load $value[ $sub_field['key'] ] = acf_get_value( $post_id, $sub_field ); } // return return $value; } /* * format_value() * * This filter is appied to the $value after it is loaded from the db and before it is returned to the template * * @type filter * @since 3.6 * @date 23/01/13 * * @param $value (mixed) the value which was loaded from the database * @param $post_id (mixed) the $post_id from which the value was loaded * @param $field (array) the field array holding all the field options * * @return $value (mixed) the modified value */ function format_value( $value, $post_id, $field ) { // bail early if no value if ( empty( $value ) ) { return false; } // modify names $field = $this->prepare_field_for_db( $field ); // loop foreach ( $field['sub_fields'] as $sub_field ) { // extract value $sub_value = acf_extract_var( $value, $sub_field['key'] ); // format value $sub_value = acf_format_value( $sub_value, $post_id, $sub_field ); // append to $row $value[ $sub_field['_name'] ] = $sub_value; } // return return $value; } /* * update_value() * * This filter is appied to the $value before it is updated in the db * * @type filter * @since 3.6 * @date 23/01/13 * * @param $value - the value which will be saved in the database * @param $field - the field array holding all the field options * @param $post_id - the $post_id of which the value will be saved * * @return $value - the modified value */ function update_value( $value, $post_id, $field ) { // bail early if no value if ( ! acf_is_array( $value ) ) { return null; } // bail early if no sub fields if ( empty( $field['sub_fields'] ) ) { return null; } // modify names $field = $this->prepare_field_for_db( $field ); // loop foreach ( $field['sub_fields'] as $sub_field ) { // vars $v = false; // key (backend) if ( isset( $value[ $sub_field['key'] ] ) ) { $v = $value[ $sub_field['key'] ]; // name (frontend) } elseif ( isset( $value[ $sub_field['_name'] ] ) ) { $v = $value[ $sub_field['_name'] ]; // empty } else { // input is not set (hidden by conditioanl logic) continue; } // update value acf_update_value( $v, $post_id, $sub_field ); } // return return ''; } /* * prepare_field_for_db * * This function will modify sub fields ready for update / load * * @type function * @date 4/11/16 * @since 5.5.0 * * @param $field (array) * @return $field */ function prepare_field_for_db( $field ) { // bail early if no sub fields if ( empty( $field['sub_fields'] ) ) { return $field; } // loop foreach ( $field['sub_fields'] as &$sub_field ) { // prefix name $sub_field['name'] = $field['name'] . '_' . $sub_field['_name']; } // return return $field; } /* * render_field() * * Create the HTML interface for your field * * @param $field - an array holding all the field's data * * @type action * @since 3.6 * @date 23/01/13 */ function render_field( $field ) { // bail early if no sub fields if ( empty( $field['sub_fields'] ) ) { return; } // load values foreach ( $field['sub_fields'] as &$sub_field ) { // add value if ( isset( $field['value'][ $sub_field['key'] ] ) ) { // this is a normal value $sub_field['value'] = $field['value'][ $sub_field['key'] ]; } elseif ( isset( $sub_field['default_value'] ) ) { // no value, but this sub field has a default value $sub_field['value'] = $sub_field['default_value']; } // update prefix to allow for nested values $sub_field['prefix'] = $field['name']; // restore required if ( $field['required'] ) { $sub_field['required'] = 0; } } // render if ( $field['layout'] == 'table' ) { $this->render_field_table( $field ); } else { $this->render_field_block( $field ); } } /* * render_field_block * * description * * @type function * @date 12/07/2016 * @since 5.4.0 * * @param $post_id (int) * @return $post_id (int) */ function render_field_block( $field ) { // vars $label_placement = ( $field['layout'] == 'block' ) ? 'top' : 'left'; // html echo '
> |
---|