acf_field_repeater::render_field()publicACF 3.6

Create the HTML interface for your field

{} Это метод класса: acf_field_repeater{}

Хуков нет.

Возвращает

null. Ничего.

Использование

$acf_field_repeater = new acf_field_repeater();
$acf_field_repeater->render_field( $field );
$field (обязательный)
-

Список изменений

С версии 3.6 Введена.

Код acf_field_repeater::render_field() ACF 5.10.2

<?php
function render_field( $field ) {

	// vars
	$sub_fields  = $field['sub_fields'];
	$show_order  = true;
	$show_add    = true;
	$show_remove = true;

	// bail early if no sub fields
	if ( empty( $sub_fields ) ) {
		return;
	}

	// value
	$value = is_array( $field['value'] ) ? $field['value'] : array();

	// 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'];

	// collapsed
	if ( $field['collapsed'] ) {

		// loop
		foreach ( $sub_fields as &$sub_field ) {

			// add target class
			if ( $sub_field['key'] == $field['collapsed'] ) {
				$sub_field['wrapper']['class'] .= ' -collapsed-target';
			}
		}
		unset( $sub_field );
	}

	?>
<div <?php acf_esc_attr_e( $div ); ?>>
	<?php
	acf_hidden_input(
		array(
			'name'  => $field['name'],
			'value' => '',
		)
	);
	?>
<table class="acf-table">
	
	<?php if ( $field['layout'] == 'table' ) : ?>
<thead>
	<tr>
		<?php if ( $show_order ) : ?>
			<th class="acf-row-handle"></th>
		<?php endif; ?>
		
		<?php
		foreach ( $sub_fields as $sub_field ) :

			// Prepare field (allow sub fields to be removed).
			$sub_field = acf_prepare_field( $sub_field );
			if ( ! $sub_field ) {
				continue;
			}

			// Define attrs.
			$attrs              = array();
			$attrs['class']     = 'acf-th';
			$attrs['data-name'] = $sub_field['_name'];
			$attrs['data-type'] = $sub_field['type'];
			$attrs['data-key']  = $sub_field['key'];

			if ( $sub_field['wrapper']['width'] ) {
				$attrs['data-width'] = $sub_field['wrapper']['width'];
				$attrs['style']      = 'width: ' . $sub_field['wrapper']['width'] . '%;';
			}

			// Remove "id" to avoid "for" attribute on <label>.
			$sub_field['id'] = '';

			?>
			<th <?php acf_esc_attr_e( $attrs ); ?>>
				<?php acf_render_field_label( $sub_field ); ?>
				<?php acf_render_field_instructions( $sub_field ); ?>
			</th>
		<?php endforeach; ?>

		<?php if ( $show_remove ) : ?>
			<th class="acf-row-handle"></th>
		<?php endif; ?>
	</tr>
</thead>
	<?php endif; ?>
	
	<tbody>
	<?php
	foreach ( $value as $i => $row ) :

		// Generate row id.
		$id = ( $i === 'acfcloneindex' ) ? 'acfcloneindex' : "row-$i";

		?>
	<tr class="acf-row
		<?php
		if ( $i === 'acfcloneindex' ) {
			echo ' acf-clone'; }
		?>
	" data-id="<?php echo esc_attr( $id ); ?>">
		
		<?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 endif; ?>
				<span><?php echo intval( $i ) + 1; ?></span>
			</td>
		<?php endif; ?>
		
		<?php echo $before_fields; ?>
		
		<?php
		foreach ( $sub_fields as $sub_field ) :

			// 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'] . '[' . $id . ']';

			// render input
			acf_render_field_wrap( $sub_field, $el );
			?>
			
		<?php endforeach; ?>
		
			<?php echo $after_fields; ?>
		
			<?php if ( $show_remove ) : ?>
			<td class="acf-row-handle remove">
				<a class="acf-icon -plus small acf-js-tooltip hide-on-shift" href="#" data-event="add-row" title="<?php _e( 'Add row', 'acf' ); ?>"></a>
				<a class="acf-icon -duplicate small acf-js-tooltip show-on-shift" href="#" data-event="duplicate-row" title="<?php _e( 'Duplicate row', 'acf' ); ?>"></a>
				<a class="acf-icon -minus small acf-js-tooltip" href="#" data-event="remove-row" title="<?php _e( 'Remove row', 'acf' ); ?>"></a>
			</td>
		<?php endif; ?>
		
	</tr>
	<?php endforeach; ?>
	</tbody>
</table>
	<?php if ( $show_add ) : ?>
	
	<div class="acf-actions">
<a class="acf-button button button-primary" href="#" data-event="add-row"><?php echo acf_esc_html( $field['button_label'] ); ?></a>
	</div>
	
<?php endif; ?>
</div>
	<?php

}