carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

Pass $parameters to add_options callback

Open boquiabierto opened this issue 9 years ago • 5 comments

f.e.: ->add_options( array( $this, 'get_custom_taxonomy_for_select' ), 'custom_tax' ) or even ->add_options( array( $this, 'get_custom_taxonomy_for_select' ), array( 'key' => 'value' ) ) DRY salt on this would be delicious!

Thanks for the great work!

boquiabierto avatar Jul 08 '17 13:07 boquiabierto

I think this goes beyond the scope of what an ->add_options() method should do. I'd suggest you use an anonymous function instead:

->add_options( function() use ( $your_object ) {
	return $your_object->get_custom_taxonomy_for_select( array( 'key' => 'value' ) );
} )

atanas-dev avatar Jul 10 '17 06:07 atanas-dev

Tried that, the ! is_callable ( $options ) here blocks an anonymous call.

boquiabierto avatar Jul 10 '17 15:07 boquiabierto

I tested on PHP 5.3 and PHP 7 just to make sure and is_callable() returns true for anonymous functions as expected:

var_dump( is_callable( function() {} ) );

I also tested this and it works correctly as well:

Field::make( 'select', 'crb_closure_select' )
	->add_options( function() {
		return array( 'foo'=>'Foo' );
	} ),

atanas-dev avatar Jul 11 '17 06:07 atanas-dev

Your right, my bad, had v1.5 on this install, sorry for inconvenience, thank you.

boquiabierto avatar Jul 11 '17 11:07 boquiabierto

No worries : )

atanas-dev avatar Jul 11 '17 11:07 atanas-dev