choice-control-render.php
2 years ago
choice-render.php
2 years ago
choices-field-render.php
2 years ago
choices-field-render.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Advanced_Choices\Block_Renders; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 8 | use Jet_Form_Builder\Blocks\Render\Base; |
| 9 | use Jet_Form_Builder\Classes\Builder_Helper; |
| 10 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 11 | use JFB_Modules\Advanced_Choices\Block_Types\Choices_Field; |
| 12 | use JFB_Modules\Wp_Experiments\Module; |
| 13 | |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * @property Choices_Field $block_type |
| 20 | * |
| 21 | * Class Choices_Field_Render |
| 22 | * @package Jet_Form_Builder\Blocks\Render |
| 23 | */ |
| 24 | class Choices_Field_Render extends Base { |
| 25 | |
| 26 | public function get_name() { |
| 27 | return 'choices-field'; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param null $wp_block |
| 32 | * @param null $template |
| 33 | * |
| 34 | * @return false|string |
| 35 | * @throws Repository_Exception |
| 36 | */ |
| 37 | public function render( $wp_block = null, $template = null ) { |
| 38 | /** |
| 39 | * For radio options, you must specify these attributes on the wrapper |
| 40 | * |
| 41 | * @link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/radiogroup_role |
| 42 | */ |
| 43 | $accessibility = Builder_Helper::attrs( |
| 44 | $this->block_type->is_allowed_multiple() |
| 45 | ? array() |
| 46 | : array( |
| 47 | array( 'role', 'radiogroup' ), |
| 48 | array( 'aria-required', $this->block_type->get_required_val() ), |
| 49 | ) |
| 50 | ); |
| 51 | |
| 52 | $attrs = get_block_wrapper_attributes( |
| 53 | array( |
| 54 | 'class' => 'jet-form-builder-choice', |
| 55 | 'data-jfb-sync' => true, |
| 56 | ) |
| 57 | ); |
| 58 | |
| 59 | $content = ''; |
| 60 | $full_name = $this->block_type->get_field_name() . ( $this->block_type->is_allowed_multiple() ? '[]' : '' ); |
| 61 | |
| 62 | foreach ( $wp_block['innerBlocks'] as $inner_block ) { |
| 63 | $content .= Block_Helper::render_with_context( |
| 64 | $inner_block, |
| 65 | $this->block_type->block_context + array( |
| 66 | Choices_Field::CONTEXT_RAW_NAME => $this->block_type->block_attrs['name'] ?? '', |
| 67 | Choices_Field::CONTEXT_NAME => $full_name, |
| 68 | Choices_Field::CONTEXT_DEFAULT => $this->block_type->block_attrs['default'], |
| 69 | Choices_Field::CONTEXT_MULTIPLE => $this->block_type->is_allowed_multiple(), |
| 70 | Choices_Field::CONTEXT_REQUIRED => $this->block_type->block_attrs['required'] ?? false, |
| 71 | ) |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | $html = sprintf( |
| 76 | '<ul %1$s %3$s>%2$s</ul>', |
| 77 | $attrs, |
| 78 | $content, |
| 79 | $accessibility |
| 80 | ); |
| 81 | |
| 82 | /** @var Module $module */ |
| 83 | $module = jet_form_builder()->module( 'wp-experiments' ); |
| 84 | $module->enable_native_layout(); |
| 85 | |
| 86 | $html = wp_render_layout_support_flag( $html, Block_Helper::current_block() ); |
| 87 | |
| 88 | $module->remove_native_layout(); |
| 89 | |
| 90 | return parent::render( null, $html ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @see \Jet_Form_Builder\Blocks\Render\Calculated_Field_Render::get_fields_label_tag |
| 95 | * |
| 96 | * @return string |
| 97 | */ |
| 98 | protected function get_fields_label_tag(): string { |
| 99 | return 'div'; |
| 100 | } |
| 101 | |
| 102 | } |
| 103 |