htmlburger/carbon-fields

Feature request: possibility of per-field rendering callback functions

Opened this issue · 0 comments

Version

  • Carbon Fields: 3.6.3
  • WordPress: 6.5.4
  • PHP: 8.3

Feature Request/Suggestion

The actual request is to make it possible to use Gutenberg blocks in complex fields. If I understand correctly, this would require the functionality of set_render_callback to be applied on the field level, right? Something like code below:

Block::make('sample_grid', 'Grid Layout')
	->add_tab('Tiles', [
		Field::make('complex', 'grid_tiles', 'Grid Tiles')
			->add_fields([])
			->set_min(2)
			->set_collapsed(true)
			->set_inner_blocks(true)
			->set_render_callback(function ($value, $attributes, $inner_blocks) {
				print '<div class="tile">' . $inner_blocks . '</div>';
			})
		])
	->add_tab('Options', [
		Field::make('text', 'columns_count', 'Number of columns per row')
			->set_default_value(2)
			->set_attribute('min', 2)
			->set_attribute('max', 6),
		Field::make('text', 'grid_gap', 'Gap (in pixels) between columns and rows')
			->set_attribute('min', 0)
			->set_default_value(24),
		])
	->set_description("Grid block")
	->set_render_callback(function ($fields, $attributes) {
		$classes = ['grid','cols-'.$fields['columns_count']];			
		print '<div class="'.implode(' ',$classes).'" style="--gap: '.$fields['grid_gap'].'px">';
		foreach($fields['grid_tiles'] as $item {
			print crb_render_field($item); // new function to present rendered field
		}
		print '</div>';
	});

I really hope it makes sense. Thank you for your awesome plugin anyways!