function render_field($field) { // vars $div = array('class' => 'acf-repeater', 'data-min' => $field['min'], 'data-max' => $field['max']); // ensure value is an array if (empty($field['value'])) { $field['value'] = array(); $div['class'] .= ' -empty'; } // rows $field['min'] = empty($field['min']) ? 0 : $field['min']; $field['max'] = empty($field['max']) ? 0 : $field['max']; // populate the empty row data (used for acfcloneindex and min setting) $empty_row = array(); // If there are less values than min, populate the extra values if ($field['min']) { for ($i = 0; $i < $field['min']; $i++) { // continue if already have a value if (array_key_exists($i, $field['value'])) { continue; } // populate values $field['value'][$i] = $empty_row; } } // If there are more values than man, remove some values if ($field['max']) { for ($i = 0; $i < count($field['value']); $i++) { if ($i >= $field['max']) { unset($field['value'][$i]); } } } // setup values for row clone $field['value']['acfcloneindex'] = $empty_row; // show columns $show_order = true; $show_add = true; $show_remove = true; if ($field['max']) { if ($field['max'] == 1) { $show_order = false; } if ($field['max'] <= $field['min']) { $show_remove = false; $show_add = false; } } // field wrap $el = 'td'; $before_fields = ''; $after_fields = ''; if ($field['layout'] == 'row') { $el = 'div'; $before_fields = '<td class="acf-fields -left">'; $after_fields = '</td>'; } elseif ($field['layout'] == 'block') { $el = 'div'; $before_fields = '<td class="acf-fields">'; $after_fields = '</td>'; } // layout $div['class'] .= ' -' . $field['layout']; // hidden input acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); // collapsed if ($field['collapsed']) { // add target class foreach (array_keys($field['sub_fields']) as $i) { if ($field['sub_fields'][$i]['key'] === $field['collapsed']) { $field['sub_fields'][$i]['wrapper']['class'] .= ' -collapsed-target'; } } } ?> <div <?php acf_esc_attr_e($div); ?> > <table class="acf-table"> <?php if ($field['layout'] == 'table') { ?> <thead> <tr> <?php if ($show_order) { ?> <th class="acf-row-handle"></th> <?php } ?> <?php foreach ($field['sub_fields'] as $sub_field) { $atts = array('class' => 'acf-th', 'data-key' => $sub_field['key']); // add type $atts['class'] .= ' acf-th-' . $sub_field['type']; // Add custom width if ($sub_field['wrapper']['width']) { $atts['data-width'] = $sub_field['wrapper']['width']; } ?> <th <?php acf_esc_attr_e($atts); ?> > <?php echo acf_get_field_label($sub_field); ?> <?php if ($sub_field['instructions']) { ?> <p class="description"><?php echo $sub_field['instructions']; ?> </p> <?php } ?> </th> <?php } ?> <?php if ($show_remove) { ?> <th class="acf-row-handle"></th> <?php } ?> </tr> </thead> <?php } ?> <tbody> <?php foreach ($field['value'] as $i => $row) { $row_class = 'acf-row'; if ($i === 'acfcloneindex') { $row_class .= ' acf-clone'; } elseif (acf_is_row_collapsed($field['key'], $i)) { $row_class .= ' -collapsed'; } ?> <tr class="<?php echo $row_class; ?> " data-id="<?php echo $i; ?> "> <?php if ($show_order) { ?> <td class="acf-row-handle order" title="<?php _e('Drag to reorder', 'acf'); ?> "> <?php if ($field['collapsed']) { ?> <a class="acf-icon -collapse small" href="#" data-event="collapse-row" title="<?php _e('Click to toggle', 'acf'); ?> "></a> <?php } ?> <span><?php echo intval($i) + 1; ?> </span> </td> <?php } ?> <?php echo $before_fields; ?> <?php foreach ($field['sub_fields'] as $sub_field) { // prevent repeater field from creating multiple conditional logic items for each row if ($i !== 'acfcloneindex') { $sub_field['conditional_logic'] = 0; } // add value if (isset($row[$sub_field['key']])) { // this is a normal value $sub_field['value'] = $row[$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']}[{$i}]"; // render input acf_render_field_wrap($sub_field, $el); ?> <?php } ?> <?php echo $after_fields; ?> <?php if ($show_remove) { ?> <td class="acf-row-handle remove"> <a class="acf-icon -plus small" href="#" data-event="add-row" title="<?php _e('Add row', 'acf'); ?> "></a> <a class="acf-icon -minus small" href="#" data-event="remove-row" title="<?php _e('Remove row', 'acf'); ?> "></a> </td> <?php } ?> </tr> <?php } ?> </tbody> </table> <?php if ($show_add) { ?> <ul class="acf-actions acf-hl"> <li> <a class="acf-button button button-primary" data-event="add-row"><?php echo $field['button_label']; ?> </a> </li> </ul> <?php } ?> </div> <?php }
function render_layout($field, $layout, $i, $value) { // vars $order = 0; $el = 'div'; $div = array('class' => 'layout', 'data-id' => $i, 'data-layout' => $layout['name']); // collapsed class if (acf_is_row_collapsed($field['key'], $i)) { $div['class'] .= ' -collapsed'; } // clone if (is_numeric($i)) { $order = $i + 1; } else { $div['class'] .= ' acf-clone'; } // title $title = $this->get_layout_title($field, $layout, $i, $value); // remove row reset_rows(); ?> <div <?php acf_esc_attr_e($div); ?> > <div class="acf-hidden"> <?php acf_hidden_input(array('name' => "{$field['name']}[{$i}][acf_fc_layout]", 'value' => $layout['name'])); ?> </div> <div class="acf-fc-layout-handle" title="<?php _e('Drag to reorder', 'acf'); ?> "><?php echo $title; ?> </div> <ul class="acf-fc-layout-controlls acf-hl"> <li class="acf-fc-show-on-hover"> <a class="acf-icon -plus small" href="#" data-event="add-layout" title="<?php _e('Add layout', 'acf'); ?> "></a> </li> <li class="acf-fc-show-on-hover"> <a class="acf-icon -minus small" href="#" data-event="remove-layout" title="<?php _e('Remove layout', 'acf'); ?> "></a> </li> <li> <a class="acf-icon -collapse small" href="#" data-event="collapse-layout" title="<?php _e('Click to toggle', 'acf'); ?> "></a> </li> </ul> <?php if (!empty($layout['sub_fields'])) { ?> <?php if ($layout['display'] == 'table') { // update vars $el = 'td'; ?> <table class="acf-table"> <thead> <tr> <?php foreach ($layout['sub_fields'] as $sub_field) { $atts = array('class' => "acf-th acf-th-{$sub_field['name']}", 'data-key' => $sub_field['key']); // Add custom width if ($sub_field['wrapper']['width']) { $atts['data-width'] = $sub_field['wrapper']['width']; } ?> <th <?php acf_esc_attr_e($atts); ?> > <?php echo acf_get_field_label($sub_field); ?> <?php if ($sub_field['instructions']) { ?> <p class="description"><?php echo $sub_field['instructions']; ?> </p> <?php } ?> </th> <?php } ?> </tr> </thead> <tbody> <?php } else { ?> <div class="acf-fields <?php if ($layout['display'] == 'row') { ?> -left<?php } ?> "> <?php } ?> <?php // loop though sub fields foreach ($layout['sub_fields'] as $sub_field) { // prevent repeater field from creating multiple conditional logic items for each row if ($i !== 'acfcloneindex') { $sub_field['conditional_logic'] = 0; } // add value if (isset($value[$sub_field['key']])) { // this is a normal value $sub_field['value'] = $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']}[{$i}]"; // render input acf_render_field_wrap($sub_field, $el); } ?> <?php if ($layout['display'] == 'table') { ?> </tbody> </table> <?php } else { ?> </div> <?php } ?> <?php } ?> </div> <?php }
function render_field($field) { // vars $sub_fields = $field['sub_fields']; $value = acf_get_array($field['value']); $show_order = true; $show_add = true; $show_remove = true; // bail early if no sub fields if (empty($sub_fields)) { return; } // div $div = array('class' => 'acf-repeater', 'data-min' => $field['min'], 'data-max' => $field['max']); // empty if (empty($value)) { $div['class'] .= ' -empty'; } // If there are less values than min, populate the extra values if ($field['min']) { $value = array_pad($value, $field['min'], array()); } // If there are more values than man, remove some values if ($field['max']) { $value = array_slice($value, 0, $field['max']); // if max 1 row, don't show order if ($field['max'] == 1) { $show_order = false; } // if max == min, don't show add or remove buttons if ($field['max'] <= $field['min']) { $show_remove = false; $show_add = false; } } // setup values for row clone $value['acfcloneindex'] = array(); // button label if ($field['button_label'] === '') { $field['button_label'] = __('Add Row', 'acf'); } // field wrap $el = 'td'; $before_fields = ''; $after_fields = ''; if ($field['layout'] == 'row') { $el = 'div'; $before_fields = '<td class="acf-fields -left">'; $after_fields = '</td>'; } elseif ($field['layout'] == 'block') { $el = 'div'; $before_fields = '<td class="acf-fields">'; $after_fields = '</td>'; } // layout $div['class'] .= ' -' . $field['layout']; // hidden input acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); // collapsed if ($field['collapsed']) { // add target class foreach ($sub_fields as $i => $sub_field) { // bail early if no match if ($sub_field['key'] !== $field['collapsed']) { continue; } // class $sub_field['wrapper']['class'] .= ' -collapsed-target'; // update $sub_fields[$i] = $sub_field; } } ?> <div <?php acf_esc_attr_e($div); ?> > <table class="acf-table"> <?php if ($field['layout'] == 'table') { ?> <thead> <tr> <?php if ($show_order) { ?> <th class="acf-row-handle"></th> <?php } ?> <?php foreach ($sub_fields as $sub_field) { // prepare field (allow sub fields to be removed) $sub_field = acf_prepare_field($sub_field); // bail ealry if no field if (!$sub_field) { continue; } // vars $atts = array(); $atts['class'] = 'acf-th'; $atts['data-name'] = $sub_field['_name']; $atts['data-type'] = $sub_field['type']; $atts['data-key'] = $sub_field['key']; // Add custom width if ($sub_field['wrapper']['width']) { $atts['data-width'] = $sub_field['wrapper']['width']; $atts['style'] = 'width: ' . $sub_field['wrapper']['width'] . '%;'; } ?> <th <?php echo acf_esc_attr($atts); ?> > <?php echo acf_get_field_label($sub_field); ?> <?php if ($sub_field['instructions']) { ?> <p class="description"><?php echo $sub_field['instructions']; ?> </p> <?php } ?> </th> <?php } ?> <?php if ($show_remove) { ?> <th class="acf-row-handle"></th> <?php } ?> </tr> </thead> <?php } ?> <tbody> <?php foreach ($value as $i => $row) { $row_class = 'acf-row'; if ($i === 'acfcloneindex') { $row_class .= ' acf-clone'; } elseif (acf_is_row_collapsed($field['key'], $i)) { $row_class .= ' -collapsed'; } ?> <tr class="<?php echo $row_class; ?> " data-id="<?php echo $i; ?> "> <?php if ($show_order) { ?> <td class="acf-row-handle order" title="<?php _e('Drag to reorder', 'acf'); ?> "> <?php if ($field['collapsed']) { ?> <a class="acf-icon -collapse small" href="#" data-event="collapse-row" title="<?php _e('Click to toggle', 'acf'); ?> "></a> <?php } ?> <span><?php echo intval($i) + 1; ?> </span> </td> <?php } ?> <?php echo $before_fields; ?> <?php foreach ($sub_fields as $sub_field) { // prevent repeater field from creating multiple conditional logic items for each row if ($i !== 'acfcloneindex') { $sub_field['conditional_logic'] = 0; } // add value if (isset($row[$sub_field['key']])) { // this is a normal value $sub_field['value'] = $row[$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'] . '[' . $i . ']'; // render input acf_render_field_wrap($sub_field, $el); ?> <?php } ?> <?php echo $after_fields; ?> <?php if ($show_remove) { ?> <td class="acf-row-handle remove"> <a class="acf-icon -plus small" href="#" data-event="add-row" title="<?php _e('Add row', 'acf'); ?> "></a> <a class="acf-icon -minus small" href="#" data-event="remove-row" title="<?php _e('Remove row', 'acf'); ?> "></a> </td> <?php } ?> </tr> <?php } ?> </tbody> </table> <?php if ($show_add) { ?> <ul class="acf-actions acf-hl"> <li> <a class="acf-button button button-primary" data-event="add-row"><?php echo $field['button_label']; ?> </a> </li> </ul> <?php } ?> </div> <?php }