Skip to content

Commit

Permalink
Add add_group_field method and modify return of add_field to return t…
Browse files Browse the repository at this point in the history
…he field id
  • Loading branch information
jtsternberg committed Feb 13, 2015
1 parent faa2425 commit 5a451b0
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions includes/CMB2.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,16 +650,46 @@ public function prop( $property, $fallback = null ) {
/**
* Add a field to the metabox
* @since 2.0.0
* @param array $field Metabox field config array
* @return bool True if field was added
* @param array $field Metabox field config array
* @return string The field id
*/
public function add_field( array $field ) {
if ( ! is_array( $field ) || ! array_key_exists( 'id', $field ) ) {
return false;
}

$this->meta_box['fields'][ $field['id'] ] = $field;
return true;
return $field['id'];
}

/**
* Add a field to a group field array
* @since 2.0.2
* @param string $field_id The Field ID of the group field to append the fields
* @param array $field Metabox field config array
* @return string The group field id
*/
public function add_group_field( $field_id, array $field ) {
if ( ! array_key_exists( $field_id, $this->meta_box['fields'] ) ) {
return false;
}

$parent_field = $this->meta_box['fields'][ $field_id ];

if ( 'group' !== $parent_field['type'] ) {
return false;
}

if ( ! is_array( $field ) || ! array_key_exists( 'id', $field ) ) {
return false;
}

if ( ! isset( $parent_field['fields'] ) ) {
$this->meta_box['fields'][ $field_id ]['fields'] = array();
}

$this->meta_box['fields'][ $field_id ]['fields'][ $field['id'] ] = $field;
return $field_id;
}

/**
Expand Down

0 comments on commit 5a451b0

Please sign in to comment.