$attributes Block attributes. * @since 0.0.2 */ public function __construct( $attributes ) { $this->slug = 'dropdown'; $this->set_properties( $attributes ); $this->set_input_label( __( 'Dropdown', 'sureforms' ) ); $this->set_error_msg( $attributes, 'srfm_dropdown_block_required_text' ); $this->multi_select_attr = ! empty( $attributes['multiSelect'] ) ? 'true' : 'false'; $this->search_attr = ! empty( $attributes['searchable'] ) ? 'true' : 'false'; $this->preselected_options = $attributes['preselectedOptions'] ?? []; $this->resolve_dynamic_default( $attributes ); $this->set_markup_properties(); $this->set_aria_described_by(); // Store the original placeholder before set_label_as_placeholder modifies placeholder_attr. $original_placeholder = $this->placeholder; $this->set_label_as_placeholder( $this->input_label ); // For dropdowns, use the appropriate placeholder text: // 1. If label is being used as placeholder (label_markup is empty), use the label. // 2. Otherwise, use the block's placeholder value. if ( empty( $this->label_markup ) ) { $this->placeholder = $this->label; } elseif ( ! empty( $original_placeholder ) ) { $this->placeholder = $original_placeholder; } // Translate the default placeholder text for frontend display. if ( 'Select an option' === $this->placeholder ) { $this->placeholder = __( 'Select an option', 'sureforms' ); } $this->show_values = apply_filters( 'srfm_show_options_values', false, $attributes['showValues'] ?? false ); // Generate unique instance identifier. self::$instance_counter++; $this->unique_slug = $this->slug . '-' . self::$instance_counter; } /** * Render the sureforms dropdown classic styling * * @since 0.0.2 * @return string|bool */ public function markup() { $this->class_name = $this->get_field_classes(); ob_start(); ?>
data_attribute_markup() ); ?> name="srfm-unique_slug ); ?>-block_id ); ?>field_name ); ?>" type="hidden" value=""/> label_markup ); ?> help_markup ); ?>
options ) ) { ?>
error_msg_markup ); ?>
$this->slug, 'field_name' => $this->field_name, 'is_editing' => $this->is_editing, 'attributes' => $this->attributes, ] ); } /** * Resolve dynamic default value from smart tags and override preselected options. * * @param array $attributes Block attributes. * @since 2.8.1 * @return void */ protected function resolve_dynamic_default( $attributes ) { $dynamic_default = is_string( $attributes['dynamicDefaultValue'] ?? '' ) ? $attributes['dynamicDefaultValue'] : ''; if ( empty( $dynamic_default ) || ! empty( $attributes['multiSelect'] ) ) { return; } $smart_tags = new Smart_Tags(); $resolved = $smart_tags->process_smart_tags( $dynamic_default ); if ( empty( $resolved ) || ! is_string( $resolved ) ) { return; } $resolved = trim( $resolved ); if ( is_array( $this->options ) ) { foreach ( $this->options as $i => $option ) { $option_label = is_array( $option ) ? ( $option['label'] ?? '' ) : ''; if ( strcasecmp( $option_label, $resolved ) === 0 ) { $this->preselected_options = [ $i ]; return; } } } } /** * Data attribute markup for min and max value * * @since 0.0.13 * @return string */ protected function data_attribute_markup() { $data_attr = ''; if ( 'false' === $this->multi_select_attr ) { return ''; } if ( $this->min_selection ) { $data_attr .= 'data-min-selection="' . esc_attr( $this->min_selection ) . '"'; } if ( $this->max_selection ) { $data_attr .= 'data-max-selection="' . esc_attr( $this->max_selection ) . '"'; } return $data_attr; } }