Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Allow for groups to not be repeatable. #589

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion helpers/cmb_Meta_Box_field.php
Expand Up @@ -449,7 +449,6 @@ public function _set_field_defaults( $args ) {
$args['save_id'] = 'file' == $args['type'] && ! ( isset( $args['save_id'] ) && ! $args['save_id'] );
// $args['multiple'] = isset( $args['multiple'] ) ? $args['multiple'] : ( 'multicheck' == $args['type'] ? true : false );
$args['multiple'] = isset( $args['multiple'] ) ? $args['multiple'] : false;
$args['repeatable'] = isset( $args['repeatable'] ) && $args['repeatable'] && ! $this->repeatable_exception( $args['type'] );
$args['inline'] = isset( $args['inline'] ) && $args['inline'] || false !== stripos( $args['type'], '_inline' );
$args['on_front'] = ! ( isset( $args['on_front'] ) && ! $args['on_front'] );
$args['attributes'] = isset( $args['attributes'] ) && is_array( $args['attributes'] ) ? $args['attributes'] : array();
Expand All @@ -473,6 +472,12 @@ public function _set_field_defaults( $args ) {
$args['options']['textarea_name'] = $args['_name'];
}

if ( 'group' == $args['type'] ) {
$args['repeatable'] = isset( $args['repeatable'] ) ? $args['repeatable'] : true;
} else {
$args['repeatable'] = isset( $args['repeatable'] ) && $args['repeatable'] && ! $this->repeatable_exception( $args['type'] );
}

$option_types = apply_filters( 'cmb_all_or_nothing_types', array( 'taxonomy_select', 'taxonomy_radio', 'taxonomy_radio_inline' ) );
if ( in_array( $args['type'], $option_types, true ) ) {

Expand Down
18 changes: 14 additions & 4 deletions init.php
Expand Up @@ -462,8 +462,9 @@ public static function render_group( $args ) {
$group_val = (array) $field_group->value();
$nrows = count( $group_val );
$remove_disabled = $nrows <= 1 ? 'disabled="disabled" ' : '';
$repeatable_class = $field_group->args( 'repeatable' ) ? 'repeatable-group' : 'group';

echo '<tr><td colspan="2"><table id="', $field_group->id(), '_repeat" class="repeatable-group'. $sortable .'" style="width:100%;">';
echo '<tr><td colspan="2"><table id="', $field_group->id(), '_repeat" class="' . $repeatable_class . ' '. $sortable .'" style="width:100%;">';
if ( $desc || $label ) {
echo '<tr><th>';
if ( $label )
Expand All @@ -473,7 +474,7 @@ public static function render_group( $args ) {
echo '</th></tr>';
}

if ( ! empty( $group_val ) ) {
if ( ! empty( $group_val ) && $field_group->args( 'repeatable' ) ) {

foreach ( $group_val as $iterator => $field_id ) {
self::render_group_row( $field_group, $remove_disabled );
Expand All @@ -482,7 +483,9 @@ public static function render_group( $args ) {
self::render_group_row( $field_group, $remove_disabled );
}

echo '<tr><td><p class="add-row"><button data-selector="', $field_group->id() ,'_repeat" data-grouptitle="', $field_group->options( 'group_title' ) ,'" class="add-group-row button">'. $field_group->options( 'add_button' ) .'</button></p></td></tr>';
if( $field_group->args( 'repeatable' ) ) {
echo '<tr><td><p class="add-row"><button data-selector="', $field_group->id() ,'_repeat" data-grouptitle="', $field_group->options( 'group_title' ) ,'" class="add-group-row button">'. $field_group->options( 'add_button' ) .'</button></p></td></tr>';
}

echo '</table></td></tr>';

Expand Down Expand Up @@ -510,12 +513,19 @@ public static function render_group_row( $field_group, $remove_disabled ) {
$field = new cmb_Meta_Box_field( $field_args, $field_group );
$field->render_field();
}
echo '

// Only show the remove group button if the group is repeatable
if( $field_group->args( 'repeatable' ) ) {
echo '
<tr>
<td class="remove-row" colspan="2">
<button '. $remove_disabled .'data-selector="'. $field_group->id() .'_repeat" class="button remove-group-row alignright">'. $field_group->options( 'remove_button' ) .'</button>
</td>
</tr>
';
}

echo '
</table>
</td>
</tr>
Expand Down