Skip to content

Commit

Permalink
Clean things up to use standard repeatable field argument instead of …
Browse files Browse the repository at this point in the history
…a field option
  • Loading branch information
jtsternberg committed May 4, 2015
2 parents 3090cf3 + cd9f25e commit 42ae877
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
25 changes: 16 additions & 9 deletions includes/CMB2.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,17 @@ public function render_group( $args ) {
$field_group = $this->get_field( $args );
$desc = $field_group->args( 'description' );
$label = $field_group->args( 'name' );
$sortable = $field_group->options( 'sortable' ) ? ' sortable' : '';
$sortable = $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable';
$repeat_class = $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable';
$group_val = (array) $field_group->value();
$nrows = count( $group_val );
$remove_disabled = $nrows <= 1 ? 'disabled="disabled" ' : '';
$field_group->index = 0;

$field_group->peform_param_callback( 'before_group' );

echo '<div class="cmb-row cmb-repeat-group-wrap"><div class="cmb-td"><div id="', $field_group->id(), '_repeat" class="cmb-nested cmb-field-list cmb-repeatable-group', $sortable, '" style="width:100%;">';
echo '<div class="cmb-row cmb-repeat-group-wrap"><div class="cmb-td"><div id="', $field_group->id(), '_repeat" class="cmb-nested cmb-field-list cmb-repeatable-group', $sortable, $repeat_class, '" style="width:100%;">';

if ( $desc || $label ) {
$class = $desc ? ' cmb-group-description' : '';
echo '<div class="cmb-row', $class, '"><div class="cmb-th">';
Expand All @@ -291,7 +293,9 @@ public function render_group( $args ) {
$this->render_group_row( $field_group, $remove_disabled );
}

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

echo '</div></div></div>';

Expand Down Expand Up @@ -335,13 +339,16 @@ public function render_group_row( $field_group, $remove_disabled ) {
$field = $this->get_field( $field_args, $field_group )->render_field();
}
}
echo '
<div class="cmb-row cmb-remove-field-row">
<div class="cmb-remove-row">
<button ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="button cmb-remove-group-row alignright">', $field_group->options( 'remove_button' ), '</button>
if ( $field_group->args( 'repeatable' ) ) {
echo '
<div class="cmb-row cmb-remove-field-row">
<div class="cmb-remove-row">
<button ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="button cmb-remove-group-row alignright">', $field_group->options( 'remove_button' ), '</button>
</div>
</div>
</div>
';
}
echo '
</div>
</div>
';
Expand Down
4 changes: 2 additions & 2 deletions includes/CMB2_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ public function replace_hash( $value ) {
public function options( $key = '' ) {
if ( ! empty( $this->field_options ) ) {
if ( $key ) {
return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : false;
return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : NULL;
}

return $this->field_options;
Expand All @@ -840,7 +840,7 @@ public function options( $key = '' ) {
}

if ( $key ) {
return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : false;
return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : NULL;
}

return $this->field_options;
Expand Down
2 changes: 1 addition & 1 deletion tests/test-cmb-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public function test_group_field_param_callbacks() {
before_group output
<div class="cmb-row cmb-repeat-group-wrap">
<div class="cmb-td">
<div id="group_field_repeat" class="cmb-nested cmb-field-list cmb-repeatable-group" style="width:100%;">
<div id="group_field_repeat" class="cmb-nested cmb-field-list cmb-repeatable-group repeatable" style="width:100%;">
<div class="cmb-row cmb-group-description">
<div class="cmb-th">
<h2 class="cmb-group-name">Group</h2>
Expand Down
13 changes: 13 additions & 0 deletions tests/test-cmb-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,19 @@ public function test_field_options() {
$this->assertHTMLstringsAreEqual( $expected_field, $this->render_field( $field ) );
}

public function test_field_options_bools() {
$cmb = new CMB2( $this->options_test );
$field = cmb2_get_field( $this->options_test['id'], 'options_test_field', $this->post_id );
$this->assertInstanceOf( 'CMB2_Field', $field );

$this->assertEquals( $field->options('one'), 'One' );
$this->assertEquals( $field->options('two'), 'Two' );
$this->assertTrue( $field->options('true') );
$this->assertFalse( $field->options('false') );
$this->assertNull( $field->options('random_string') );

}

public function test_field_attributes() {
$cmb = new CMB2( $this->attributes_test );
$field = cmb2_get_field( $this->attributes_test['id'], 'attributes_test_field', $this->post_id );
Expand Down

0 comments on commit 42ae877

Please sign in to comment.