name = 'flexible_content'; $this->title = __("Flexible Content",'acf'); // filters add_filter('acf_save_field-' . $this->name, array($this, 'acf_save_field')); } /*-------------------------------------------------------------------------------------- * * create_field * - called in lots of places to create the html version of the field * * @author Elliot Condon * @since 2.2.0 * *-------------------------------------------------------------------------------------*/ function create_field($field) { $button_label = ( isset($field['button_label']) && $field['button_label'] != "" ) ? $field['button_label'] : __("+ Add Row",'acf'); $layouts = array(); foreach($field['layouts'] as $l) { $layouts[$l['name']] = $l; } ?>
>
$sub_field): // add width attr $attr = ""; if( count($layout['sub_fields']) > 1 && isset($sub_field['column_width']) && $sub_field['column_width'] ) { $attr = 'width="' . $sub_field['column_width'] . '%"'; } ?>
>
$sub_field ): ?>
$value): // validate layout if( !isset($layouts[$value['acf_fc_layout']]) ) { continue; } // vars $layout = $layouts[$value['acf_fc_layout']]; ?>
$sub_field): // add width attr $attr = ""; if( count($layout['sub_fields']) > 1 && isset($sub_field['column_width']) && $sub_field['column_width'] ) { $attr = 'width="' . $sub_field['column_width'] . '%"'; } ?>
>
$sub_field ): ?>
$value) endif; // if($field['value']): ?>
array(), 'button_label' => __("Add Row",'acf'), ); $field = array_merge($defaults, $field); // load default layout if(empty($field['layouts'])) { $field['layouts'][] = array( 'name' => '', 'label' => '', 'display' => 'row', 'sub_fields' => array(), ); } // get name of all fields for use in field type foreach($this->parent->fields as $f) { if( $f->name ) { $fields_names[$f->name] = $f->title; } } unset( $fields_names['flexible_content'], $fields_names['tab'] ); // loop through layouts and create the options for them if($field['layouts']): foreach($field['layouts'] as $layout_key => $layout): $layout['sub_fields'][] = array( 'key' => 'field_clone', 'label' => __("New Field",'acf'), 'name' => __("new_field",'acf'), 'type' => 'text', 'order_no' => 1, 'instructions' => '', ); ?>

" href="javascript:;"> | " href="javascript:;">
" href="javascript:;"> | " href="javascript:;">

'text', 'name' => 'fields['.$key.'][layouts][' . $layout_key . '][label]', 'value' => $layout['label'], )); ?> 'text', 'name' => 'fields['.$key.'][layouts][' . $layout_key . '][name]', 'value' => $layout['name'], )); ?> 'select', 'name' => 'fields['.$key.'][layouts][' . $layout_key . '][display]', 'value' => $layout['display'], 'choices' => array( 'row' => __("Row",'acf'), 'table' => __("Table",'acf'), ) )); ?>
1){ echo 'style="display:none;"'; } ?>>
parent->fields[ $sub_field['type'] ]) ) { $this->parent->fields[$sub_field['type']]->create_options($key.'][layouts][' . $layout_key . '][sub_fields]['.$sub_field['key'], $sub_field); } ?>

'text', 'name' => 'fields['.$key.'][layouts][' . $layout_key . '][sub_fields]['.$sub_field['key'].'][label]', 'value' => $sub_field['label'], 'class' => 'label', )); ?>

'text', 'name' => 'fields['.$key.'][layouts][' . $layout_key . '][sub_fields]['.$sub_field['key'].'][name]', 'value' => $sub_field['name'], 'class' => 'name', )); ?>
'select', 'name' => 'fields['.$key.'][layouts][' . $layout_key . '][sub_fields]['.$sub_field['key'].'][type]', 'value' => $sub_field['type'], 'class' => 'type', 'choices' => $fields_names )); ?>
'text', 'name' => 'fields['.$key.'][layouts][' . $layout_key . '][sub_fields]['.$sub_field['key'].'][instructions]', 'value' => $sub_field['instructions'], 'class' => 'instructions', )); ?>

'number', 'name' => 'fields['.$key.'][layouts][' . $layout_key . '][sub_fields]['.$sub_field['key'].'][column_width]', 'value' => $sub_field['column_width'], 'class' => 'column_width', )); ?> %
'text', 'name' => 'fields['.$key.'][button_label]', 'value' => $field['button_label'], )); ?> $v) { $sub_field = $sub_fields[$field_key]; // update full name $sub_field['name'] = $field['name'] . '_' . $i . '_' . $sub_field['name']; // save sub field value $this->parent->update_value($post_id, $sub_field, $v); } } } /* * Remove Old Data * * @credit: http://support.advancedcustomfields.com/discussion/1994/deleting-single-repeater-fields-does-not-remove-entry-from-database */ $old_total = parent::get_value($post_id, $field); $old_total = count( $old_total ); $new_total = count( $total ); if( $old_total > $new_total ) { foreach( $sub_fields as $sub_field ) { for ( $j = $new_total; $j < $old_total; $j++ ) { parent::delete_value( $post_id, $field['name'] . '_' . $j . '_' . $sub_field['name'] ); } } } parent::update_value($post_id, $field, $total); } /*-------------------------------------------------------------------------------------- * * pre_save_field * - called just before saving the field to the database. * * @author Elliot Condon * @since 2.2.0 * *-------------------------------------------------------------------------------------*/ function acf_save_field( $field ) { // format sub_fields if($field['layouts']) { // loop through and save fields foreach($field['layouts'] as $layout_key => $layout) { if( $layout['sub_fields'] ) { // remove dummy field unset( $layout['sub_fields']['field_clone'] ); // loop through and save fields $i = -1; $sub_fields = array(); foreach( $layout['sub_fields'] as $key => $f ) { $i++; // order $f['order_no'] = $i; $f['key'] = $key; // apply filters $f = apply_filters('acf_save_field', $f ); $f = apply_filters('acf_save_field-' . $f['type'], $f ); $sub_fields[ $f['key'] ] = $f; } $layout['sub_fields'] = $sub_fields; } // update $layout $field['layouts'][ $layout_key ] = $layout; } } // return updated repeater field return $field; } /*-------------------------------------------------------------------------------------- * * get_value * - called from the input edit page to get the value. * * @author Elliot Condon * @since 2.2.0 * *-------------------------------------------------------------------------------------*/ function get_value($post_id, $field) { $layouts = array(); foreach($field['layouts'] as $l) { $layouts[$l['name']] = $l; } // vars $values = array(); $layout_order = false; // get total rows $layout_order = parent::get_value($post_id, $field); if( !empty( $layout_order) ) { $i = -1; // loop through rows foreach($layout_order as $layout) { $i++; $values[$i]['acf_fc_layout'] = $layout; // check if layout still exists if(isset($layouts[$layout])) { // loop through sub fields foreach($layouts[$layout]['sub_fields'] as $sub_field) { // update full name $sub_field['name'] = $field['name'] . '_' . $i . '_' . $sub_field['name']; $values[$i][ $sub_field['key'] ] = $this->parent->get_value($post_id, $sub_field); } } } } else { $values = false; } return $values; } /*-------------------------------------------------------------------------------------- * * get_value_for_api * * @author Elliot Condon * @since 3.0.0 * *-------------------------------------------------------------------------------------*/ function get_value_for_api($post_id, $field) { $layouts = array(); foreach($field['layouts'] as $l) { $layouts[$l['name']] = $l; } // vars $values = array(); $layout_order = false; // get total rows $layout_order = parent::get_value($post_id, $field); if($layout_order) { $i = -1; // loop through rows foreach($layout_order as $layout) { $i++; $values[$i]['acf_fc_layout'] = $layout; // loop through sub fields foreach($layouts[$layout]['sub_fields'] as $sub_field) { // store name $field_name = $sub_field['name']; // update full name $sub_field['name'] = $field['name'] . '_' . $i . '_' . $field_name; $values[$i][$field_name] = $this->parent->get_value_for_api($post_id, $sub_field); } } return $values; } return array(); } } ?>