Skip to content

Commit

Permalink
Split show_form into render_form_open, render_form_close, and render_…
Browse files Browse the repository at this point in the history
…field, and return field objects whenever possible. Closes #506
  • Loading branch information
jtsternberg committed Oct 27, 2015
1 parent 0f81f26 commit ddef6f7
Showing 1 changed file with 73 additions and 29 deletions.
102 changes: 73 additions & 29 deletions includes/CMB2.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ public function __construct( $meta_box, $object_id = 0 ) {
* @param string $object_type Type of object being saved. (e.g., post, user, or comment)
*/
public function show_form( $object_id = 0, $object_type = '' ) {
$this->render_form_open( $object_id, $object_type );

foreach ( $this->prop( 'fields' ) as $field_args ) {
$this->render_field( $field_args );
}

$this->render_form_close( $object_id, $object_type );
}

/**
* Outputs the opening form markup and runs corresponding hooks:
* 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}"
* @since 2.1.3
* @param integer $object_id Object ID
* @param string $object_type Object type
* @return void
*/
public function render_form_open( $object_id = 0, $object_type = '' ) {
$object_type = $this->object_type( $object_type );
$object_id = $this->object_id( $object_id );

Expand Down Expand Up @@ -188,33 +206,19 @@ public function show_form( $object_id = 0, $object_type = '' ) {

echo '<div class="cmb2-wrap form-table"><div id="cmb2-metabox-', sanitize_html_class( $this->cmb_id ), '" class="cmb2-metabox cmb-field-list">';

foreach ( $this->prop( 'fields' ) as $field_args ) {

$field_args['context'] = $this->prop( 'context' );

if ( 'group' == $field_args['type'] ) {

if ( ! isset( $field_args['show_names'] ) ) {
$field_args['show_names'] = $this->prop( 'show_names' );
}
$this->render_group( $field_args );

} elseif ( 'hidden' == $field_args['type'] && $this->get_field( $field_args )->should_show() ) {
// Save rendering for after the metabox
$this->add_hidden_field( array(
'field_args' => $field_args,
'object_type' => $this->object_type(),
'object_id' => $this->object_id(),
) );

} else {

$field_args['show_names'] = $this->prop( 'show_names' );
}

// Render default fields
$field = $this->get_field( $field_args )->render_field();
}
}
/**
* Outputs the closing form markup and runs corresponding hooks:
* 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}"
* @since 2.1.3
* @param integer $object_id Object ID
* @param string $object_type Object type
* @return void
*/
public function render_form_close( $object_id = 0, $object_type = '' ) {
$object_type = $this->object_type( $object_type );
$object_id = $this->object_id( $object_id );

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

Expand Down Expand Up @@ -251,8 +255,44 @@ public function show_form( $object_id = 0, $object_type = '' ) {
}

/**
* Render a repeatable group
* @param array $args Array of field arguments for a group field parent
* Renders a field based on the field type
* @since 2.1.3
* @param array $field_args A field configuration array.
* @return mixed CMB2_Field object if successful.
*/
public function render_field( $field_args ) {
$field_args['context'] = $this->prop( 'context' );

if ( 'group' == $field_args['type'] ) {

if ( ! isset( $field_args['show_names'] ) ) {
$field_args['show_names'] = $this->prop( 'show_names' );
}
$field = $this->render_group( $field_args );

} elseif ( 'hidden' == $field_args['type'] && $this->get_field( $field_args )->should_show() ) {
// Save rendering for after the metabox
$field = $this->add_hidden_field( array(
'field_args' => $field_args,
'object_type' => $this->object_type(),
'object_id' => $this->object_id(),
) );

} else {

$field_args['show_names'] = $this->prop( 'show_names' );

// Render default fields
$field = $this->get_field( $field_args )->render_field();
}

return $field;
}

/**
* Render a repeatable group.
* @param array $args Array of field arguments for a group field parent.
* @return CMB2_Field|null Group field object.
*/
public function render_group( $args ) {

Expand Down Expand Up @@ -310,6 +350,7 @@ public function render_group( $args ) {

$field_group->peform_param_callback( 'after_group' );

return $field_group;
}

/**
Expand Down Expand Up @@ -376,7 +417,10 @@ public function render_group_row( $field_group, $remove_disabled ) {
* @param array $args Array of arguments to be passed to CMB2_Field
*/
public function add_hidden_field( $args ) {
$this->hidden_fields[] = new CMB2_Types( new CMB2_Field( $args ) );
$field = new CMB2_Field( $args );
$this->hidden_fields[] = new CMB2_Types( $field );

return $field;
}

/**
Expand Down

0 comments on commit ddef6f7

Please sign in to comment.