Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce three new filters to filter field arguments. #588

Closed
wants to merge 2 commits into from

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Feb 11, 2016

The cmb2_field_defaults filter to filter the generic field defaults. Especially useful if you have a lot of fields which use nearly the same arguments. This way you can just set the defaults for those fields in one go.

The cmb2_field_arguments_raw filter to filter the arguments used to set up the field before processing.

The cmb2_field_arguments filter to filter the arguments used to set up the field after processing.

All filters also pass the field id, so the filter can (and should) selectively be applied by testing the id against a specific id or testing it against the prefix substring.
They also pass the field type and the object type so the filter can selectively be applied against one specific field type or object type only.

Example use case for the defaults filter:

  • Change the show_names variable default to false for all CMB2 fields.
  • If you have a default set of radio options for all radio fields in a specific metabox, you could set this in one go for all your radio fields.
add_filter( `cmb2_field_defaults`, 'my_field_defaults_filter', 10, 3 );
function my_field_defaults_filter( $args, $id, $type ) {
    if ( strpos( $id, 'myprefix' ) === false || $type !== 'radio' ) {
        return $args;
    }

    $args['options'] = 'my_radio_options_callback';
    return $args;
}

Example use case for the field arguments raw filter:

  • Changing all textarea field to textarea_small:
add_filter( `cmb2_field_arguments_raw`, 'my_field_arg_raw_filter', 10, 3 );
function my_field_arg_raw_filter( $args, $id, $type ) {
    if ( strpos( $id, 'myprefix' ) === false || $type !== 'textarea' ) {
        return $args;
    }

    $args['type'] = 'textarea_small';
    return $args;
}

Example use case for the field arguments filter:

  • Say you've added another field type which needs supporting data, this filter would allow you to set $args['has_supporting_data'] to true.

@jrfnl jrfnl force-pushed the feature/new-field-arg-filters branch from f0acbde to bd1ef49 Compare February 11, 2016 07:09
jrfnl added a commit to jrfnl/cmb2-conditionals that referenced this pull request Feb 11, 2016
The js file was now only loaded on post (type) related pages, which effectively made it impossible to have conditionals on term, comment or user objects.

Also:
* minify the js for faster loading
* decouple the script registration and enqueueing
* use the proper hook for the registration

This PR depends on PR [#588 on CMB2](CMB2/CMB2#588) itself being accepted.
@jtsternberg
Copy link
Member

IMO, this processing should happen during the field registration process. For your radio example:

    $fields[] = array(
        'name'             => __( 'Test Radio inline', 'cmb2' ),
        'desc'             => __( 'field description (optional)', 'cmb2' ),
        'id'               => $prefix . 'radio_inline',
        'type'             => 'radio_inline',
        'show_option_none' => 'No Selection',
        'options'          => array(
            'standard' => __( 'Option One', 'cmb2' ),
            'custom'   => __( 'Option Two', 'cmb2' ),
            'none'     => __( 'Option Three', 'cmb2' ),
        ),
    ) );

    $fields[] = array(
        'name'    => __( 'Test Radio', 'cmb2' ),
        'desc'    => __( 'field description (optional)', 'cmb2' ),
        'id'      => $prefix . 'radio',
        'type'    => 'radio',
        'options' => array(
            'option1' => __( 'Option One', 'cmb2' ),
            'option2' => __( 'Option Two', 'cmb2' ),
            'option3' => __( 'Option Three', 'cmb2' ),
        ),
    ) );

    foreach ( $fields as $field ) {
        switch ( $field['type'] ) {
            case 'radio':
                $field['options'] = 'my_radio_options_callback';
                break;
        }

        $cmb_demo->add_field( $field );
    }

The only filter that makes sense to me is the last filter, cmb2_field_arguments, but I think it should pass $this as the 2nd argument to allow full access to the field object.

@jrfnl
Copy link
Contributor Author

jrfnl commented Feb 16, 2016

IMO, this processing should happen during the field registration process.

@jtsternberg I know how it can be done during registration, that's not the point.

These filters are useful for CMB2-add-on plugins as well as making large changes quickly.

Say, you've got ten metaboxes set up with some 150 fields. Instead of having to edit all the fields to make a change to a particular field type, you could now just filter them.

I've currently got my setup organized so it will filter all arguments from all metaboxes before passing them to CMB2, but it makes a lot more sense to have such a filter in CMB2 instead of in each implementation.

@jrfnl jrfnl force-pushed the feature/new-field-arg-filters branch from bd1ef49 to af247c4 Compare March 1, 2016 00:58
@jrfnl
Copy link
Contributor Author

jrfnl commented Mar 1, 2016

Rebased, added filter documentation and changed the last argument as per suggestion.

@jtsternberg
Copy link
Member

Let's yank the cmb2_field_arguments_raw filter, and we'll call it good. I can't imagine a scenario where it would be needed over the cmb2_field_arguments filter.

@jrfnl
Copy link
Contributor Author

jrfnl commented Mar 2, 2016

If you insist, I'll take it out, but the reason I put it in was to be able to globally change some field arguments without having to duplicate the logic contained in the rest of the function.
If one changes it using the cmb_field_arguments filter at the end, the logic within the function would often still need to be duplicated.

@jrfnl jrfnl force-pushed the feature/new-field-arg-filters branch from af247c4 to e56adb9 Compare April 22, 2016 11:48
jrfnl added 2 commits August 27, 2016 11:40
The `cmb2_field_defaults` filter to filter the generic field defaults. Especially useful if you have a lot of fields which use nearly the same arguments. This way you can just set the defaults for those fields in one go.

The `cmb2_field_arguments_raw` filter to filter the arguments used to set up the field *before* processing.

The `cmb2_field_arguments` filter to filter the arguments used to set up the field *after* processing.

All filters also pass the field id, so the filter can (and should) selectively be applied by testing the id against a specific id or testing it against the prefix substring.
They also both pass the field type and the object type so the filter can selectively be applied against one specific field type or object type only.

Example use case for the defaults filter:
* Change the `show_names` variable default to `false` for all CMB2 fields.
* If you have a default set of radio options for all radio fields in a specific metabox, you could set this in one go for all your radio fields.
```php
add_filter( `cmb2_field_defaults`, 'my_field_defaults_filter', 10, 3 );
function my_field_defaults_filter( $args, $id, $type ) {
	if ( strpos( $id, 'myprefix' ) === false || $type !== 'radio' ) {
		return $args;
	}

	$args['options'] = 'my_radio_options_callback';
	return $args;
}
```

Example use case for the field arguments raw filter:
* Changing all textarea field to textarea_small:
```php
add_filter( `cmb2_field_arguments_raw`, 'my_field_arg_raw_filter', 10, 3 );
function my_field_arg_raw_filter( $args, $id, $type ) {
	if ( strpos( $id, 'myprefix' ) === false || $type !== 'textarea' ) {
		return $args;
	}

	$args['type'] = 'textarea_small';
	return $args;
}
```

Example use case for the field arguments filter:
* Say you've added another field type which needs supporting data, this filter would allow you to set `$args['has_supporting_data']` to true.
…s and and change the last parameter to `$this`.
@jrfnl jrfnl force-pushed the feature/new-field-arg-filters branch from e56adb9 to 8bd6a8d Compare August 27, 2016 09:48
lipemat added a commit to lipemat/CMB2 that referenced this pull request Apr 9, 2019
* upstream/develop:
  Corrected link to facetwp-cmb2 in README.md (CMB2#1248)
  Implement attribute escaping per VIP standards/@GaryJones
  Update readme with more available 3rd-party resources
  Ensure date picker fields can have a default value. Fixes CMB2#1245
  Ignore some more directories in the distribution package.
  Do exclude composer.lock file from archive
  Don't exclude composer.json from the distribution.
  Add example for using the query_args field arg to override the get_terms args
  assignment alignment and space replacement with tabs
  phpdoc and comment touchups
  Try to make travis happy
  Update changelog
  Rename row variables in cmb.addAjaxRow as technically the emtpyRow is the one being passed around. Also pass the new row to the cmb2_add_row trigger
  use phpunit 5.7 in PHP 5.6
  Add develop suffix to the loader
  Add apigen to version control
  Prep 2.6.0 for release
  Oops, fix the fix (from bcec44e)
  Add props for CMB2#588. Closes CMB2#588
  Add props for CMB2#463. Closes CMB2#463
  Add props for CMB2#953. Closes CMB2#953
  Ditch the field args, and only pass in the field object to the new field before/after hooks, and clean up docs
  Remove unused variables/method attributes
  Ensure value passed to CMB2_Utils::filter_empty is always an array. Fixes CMB2#1026
  Add section for Creating/Running Tests
  Add magic getter for CMB2_Option class. Fixes CMB2#1052
  Add props for CMB2#1108. Closes CMB2#1108
  Update test to reflect postbox groups getting ids
  Update cmb-group-id functionality a bit
  Add props for CMB2#1216
  update github issue/PR templates
  Do not trigger tinyMCE editor save for the activeEditor. Prevents cursor jump in Gutenberg. Fixes CMB2#1202
  see CMB2#1214 - fix tests.
  see CMB2#1214 - better placement of the lib dep registration.
  fix CMB2#1214 - Making a field repeatable generates JS error because of missing sortable lib.
  Add props for CMB2#1212
  Added 'id' attribute on group field ref class 'postbox cmb-row cmb-repeatable-grouping'
  Before/After row hooks
  Document the new field_argument filters, remove superfluous parameters and and change the last parameter to `$this`.
  Introduce three new filters to filter field arguments.
  WP_DEVELOP_DIR != $test_root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants