Skip to content

Commit

Permalink
Add 'row_classes' field parameter for adding additional row classes. …
Browse files Browse the repository at this point in the history
…Mentioned in CMB2#68.
  • Loading branch information
jtsternberg committed Feb 9, 2015
1 parent f4737a3 commit 0860f44
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 22 deletions.
66 changes: 52 additions & 14 deletions includes/CMB2_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,22 +667,55 @@ public function render_field() {
* @return string Space concatenated list of classes
*/
public function row_classes() {
$classes = 'cmb-type-'. str_replace( '_', '-', sanitize_html_class( $this->type() ) );
$classes .= ' cmb2-id-'. str_replace( '_', '-', sanitize_html_class( $this->id() ) );
$classes .= $this->args( 'repeatable' ) ? ' cmb-repeat' : '';
$classes .= $this->group ? ' cmb-repeat-group-field' : '';
$classes = array();
$classes[] = 'cmb-type-'. str_replace( '_', '-', sanitize_html_class( $this->type() ) );
$classes[] = 'cmb2-id-'. str_replace( '_', '-', sanitize_html_class( $this->id() ) );

if ( $this->args( 'repeatable' ) ) {
$classes[] = 'cmb-repeat';
}

if ( $this->group ) {
$classes[] = 'cmb-repeat-group-field';
}

// 'inline' flag, or _inline in the field type, set to true
$classes .= $this->args( 'inline' ) ? ' cmb-inline' : '';
if ( $this->args( 'inline' ) ) {
$classes[] = 'cmb-inline';
}

/**
* By default, 'text_url' and 'text' fields get table-like styling
*
* @since 2.0.0
*
* @param array $field_types The types of fields which should get the 'table-layout' class
*/
$repeat_table_rows_types = apply_filters( 'cmb2_repeat_table_row_types', array(
'text_url', 'text',
) );

if ( in_array( $this->type(), $repeat_table_rows_types ) ) {
$classes .= ' table-layout';
$classes[] = 'table-layout';
}

return apply_filters( 'cmb2_row_classes', $classes, $this );
if ( $added_classes = $this->get_param_callback_result( 'row_classes', false ) ) {
if ( is_array( $added_classes ) ) {
$classes[] = esc_attr( implode( ' ', $added_classes ) );
} elseif ( is_string( $added_classes ) ) {
$classes[] = esc_attr( $added_classes );
}
}

/**
* Globally filter row classes
*
* @since 2.0.0
*
* @param string $classes Space-separated list of row classes
* @param CMB2_Field object $field This field object
*/
return apply_filters( 'cmb2_row_classes', implode( ' ', $classes ), $this );
}

/**
Expand All @@ -699,9 +732,10 @@ public function peform_param_callback( $param ) {
* Store results of the param callbacks for continual access
* @since 2.0.0
* @param string $param Field parameter
* @param bool $echo Whether field should be 'echoed'
* @return mixed Results of param/param callback
*/
public function get_param_callback_result( $param ) {
public function get_param_callback_result( $param, $echo = true ) {

// If we've already retrieved this param's value,
if ( array_key_exists( $param, $this->callback_results ) ) {
Expand All @@ -710,12 +744,16 @@ public function get_param_callback_result( $param ) {
}

if ( $cb = $this->maybe_callback( $param ) ) {
// Ok, callback is good, let's run it and store the result
ob_start();
echo call_user_func( $cb, $this->args(), $this );
// grab the result from the output buffer and store it
$this->callback_results[ $param ] = ob_get_contents();
ob_end_clean();
if ( $echo ) {
// Ok, callback is good, let's run it and store the result
ob_start();
echo call_user_func( $cb, $this->args(), $this );
// grab the result from the output buffer and store it
$this->callback_results[ $param ] = ob_get_contents();
ob_end_clean();
} else {
$this->callback_results[ $param ] = call_user_func( $cb, $this->args(), $this );
}

return $this->callback_results[ $param ];
}
Expand Down
14 changes: 7 additions & 7 deletions languages/cmb2.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# This file is distributed under the same license as the CMB2 package.
msgid ""
msgstr ""
"Project-Id-Version: CMB2 2.0.1\n"
"Project-Id-Version: CMB2 2.0.2\n"
"Report-Msgid-Bugs-To: http://wordpress.org/support/plugin/cmb2\n"
"POT-Creation-Date: 2015-01-30 15:31:40+00:00\n"
"POT-Creation-Date: 2015-02-09 17:05:17+00:00\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"PO-Revision-Date: 2015-1-30 15:31+300\n"
"PO-Revision-Date: 2015-2-9 17:5+300\n"
"Last-Translator: WebDevStudios [email protected]\n"
"Language-Team: WebDevStudios [email protected]\n"
"X-Generator: grunt-wp-i18n 0.4.9\n"
Expand Down Expand Up @@ -289,19 +289,19 @@ msgstr ""
msgid "No oEmbed Results Found for %s. View more info at"
msgstr ""

#: includes/CMB2_Field.php:816
#: includes/CMB2_Field.php:854
msgid "Add Group"
msgstr ""

#: includes/CMB2_Field.php:817
#: includes/CMB2_Field.php:855
msgid "Remove Group"
msgstr ""

#: includes/CMB2_Field.php:838
#: includes/CMB2_Field.php:876
msgid "None"
msgstr ""

#: includes/CMB2_Field.php:839
#: includes/CMB2_Field.php:877
msgid "All"
msgstr ""

Expand Down
60 changes: 59 additions & 1 deletion tests/test-cmb-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function setUp() {
) ),
),
'before_field' => array( $this, 'before_field_cb' ),
'after_field' => 'after_field_static',
'after_field' => 'after_field_static',
'row_classes' => array( $this, 'row_classes_array_cb' ),
);

$this->object_id = $this->post_id;
Expand Down Expand Up @@ -62,8 +63,65 @@ public function test_cmb2_before_and_after_field_callbacks() {
$this->assertEquals( 'before_field_cb_test_testafter_field_static', $content );
}

public function test_cmb2_row_classes_field_callback_with_array() {
// Add row classes dynamically with a callback that returns an array
$classes = $this->field->row_classes();
$this->assertEquals( 'cmb-type-text cmb2-id-test-test table-layout type name desc before after options_cb options attributes protocols default select_all_button multiple repeatable inline on_front show_names date_format time_format description preview_size id before_field after_field row_classes _id _name', $classes );
}

public function test_cmb2_row_classes_field_callback_with_string() {

// Test with string
$args = $this->field_args;

// Add row classes dynamically with a callback that returns a string
$args['row_classes'] = array( $this, 'row_classes_string_cb' );

$field = new CMB2_Field( array(
'object_id' => $this->object_id,
'object_type' => $this->object_type,
'group' => $this->group,
'field_args' => $args,
) );

$classes = $field->row_classes();

$this->assertEquals( 'cmb-type-text cmb2-id-test-test table-layout callback with string', $classes );
}

public function test_cmb2_row_classes_string() {

// Test with string
$args = $this->field_args;

// Add row classes statically as a string
$args['row_classes'] = 'these are some classes';

$field = new CMB2_Field( array(
'object_id' => $this->object_id,
'object_type' => $this->object_type,
'group' => $this->group,
'field_args' => $args,
) );

$classes = $field->row_classes();

$this->assertEquals( 'cmb-type-text cmb2-id-test-test table-layout these are some classes', $classes );
}

public function before_field_cb( $args ) {
echo 'before_field_cb_'. $args['id'];
}

public function row_classes_array_cb( $args ) {
/**
* Side benefit: this will call out when default args change
*/
return array_keys( $args );
}

public function row_classes_string_cb( $args ) {
return 'callback with string';
}

}

0 comments on commit 0860f44

Please sign in to comment.