diff --git a/includes/extensions/edit-entry/class-edit-entry-render.php b/includes/extensions/edit-entry/class-edit-entry-render.php index 74d2c9dc7..450e849d0 100644 --- a/includes/extensions/edit-entry/class-edit-entry-render.php +++ b/includes/extensions/edit-entry/class-edit-entry-render.php @@ -1556,7 +1556,7 @@ private function get_field_value( $field ) { $override_saved_value = apply_filters( 'gravityview/edit_entry/pre_populate/override', false, $field ); // We're dealing with multiple inputs (e.g. checkbox) but not time or date (as it doesn't store data in input IDs) - if ( isset( $field->inputs ) && is_array( $field->inputs ) && ! in_array( $field->type, array( 'time', 'date' ) ) ) { + if ( isset( $field->inputs ) && is_array( $field->inputs ) && ! in_array( $field->type, array( 'time', 'date' ) ) && ! ( $field instanceof GF_Field_Radio && in_array($field->type, array('image_choice','multi_choice')) ) ) { $field_value = array(); diff --git a/includes/fields/class-gravityview-field-image-choice.php b/includes/fields/class-gravityview-field-image-choice.php new file mode 100644 index 000000000..ac3017995 --- /dev/null +++ b/includes/fields/class-gravityview-field-image-choice.php @@ -0,0 +1,119 @@ +label = esc_html__( 'Image Choice', 'gk-gravityview' ); + parent::__construct(); + } + + /** + * Adds `choice_display` setting to the field + * + * @since TBD + * + * @param array $field_options + * @param string $template_id + * @param string $field_id + * @param string $context + * @param string $input_type + * + * @return array + */ + public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) { + + $field_options = parent::field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ); + + $choices = array( + 'label' => __( 'Label of the input', 'gk-gravityview' ), + 'image' => __( 'Image of the input', 'gk-gravityview' ), + ); + + if ( $this->is_choice_value_enabled() ) { + $choices['value'] = __( 'Value of the input', 'gk-gravityview' ); + } + + $field_options['choice_display'] = array( + 'type' => 'radio', + 'value' => 'image', + 'label' => __( 'What should be displayed:', 'gk-gravityview' ), + // translators: %s is replaced by the components that the field has (label, value, and image or label, value) + 'desc' => sprintf( __( 'This input has a %s. What should be displayed?', 'gk-gravityview' ), $this->is_choice_value_enabled() ? _x( 'label, value, and image', 'These are a list of choices for what to to display for the current input.', 'gk-gravityview' ) : _x( 'label and value', 'These are a list of choices for what to to display for the current input.', 'gk-gravityview' ) ), + 'choices' => $choices, + 'group' => 'display', + ); + + return $field_options; + } + + /** + * Outputs the image choice markup. + * + * @since TBD + * + * @param mixed $value The field value + * @param GF_Field_Select $field Gravity Forms Select field + * @param array $form The current form array + * @param array $entry GF Entry + * @param \GV\Template_Context $gravityview The context + * + * @return string The image markup + */ + public function output_image_choice( $value, $field, $form ) { + $choices = $field->choices; + $output = ''; + + $values = is_array( $value ) ? $value : array( $value ); + + foreach ( $values as $val ) { + foreach ( $choices as $choice ) { + if ( $choice['value'] != $val ) { + continue; + } + $decorator = new ChoiceDecorator( $field ); + /** + * Override the image markup for the image choice field. + * + * @since TBD + * + * @param string $image_markup The image markup + * @param array $choice The choice array + * @param array $form The current form array + * @param GF_Field_Select $field Gravity Forms Select field + */ + $image_markup = apply_filters( + 'gravityview/fields/image_choice/image_markup', + $decorator->get_image_markup( $choice, $form ), + $choice, + $form, + $field, + ); + $output .= $image_markup; + break; + } + } + + return $output; + } + + +} + +new GravityView_Field_Image_Choice(); diff --git a/includes/fields/class-gravityview-field-multiple-choice.php b/includes/fields/class-gravityview-field-multiple-choice.php new file mode 100644 index 000000000..421e4e30f --- /dev/null +++ b/includes/fields/class-gravityview-field-multiple-choice.php @@ -0,0 +1,65 @@ +label = esc_html__( 'Multiple Choice', 'gk-gravityview' ); + parent::__construct(); + } + + + /** + * Adds `choice_display` setting to the field + * + * @since TBD + * + * @param array $field_options + * @param string $template_id + * @param string $field_id + * @param string $context + * @param string $input_type + * + * @return array + */ + public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) { + + // Set the $_field_id var + $field_options = parent::field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ); + + if ( $this->is_choice_value_enabled() ) { + $field_options['choice_display'] = array( + 'type' => 'radio', + 'value' => 'value', + 'label' => __( 'What should be displayed:', 'gk-gravityview' ), + 'desc' => __( 'This input has a label and a value. What should be displayed?', 'gk-gravityview' ), + 'choices' => array( + 'value' => __( 'Value of the input', 'gk-gravityview' ), + 'label' => __( 'Label of the input', 'gk-gravityview' ), + ), + 'group' => 'display', + ); + } + + return $field_options; + } + +} + +new GravityView_Field_Multiple_Choice(); \ No newline at end of file diff --git a/includes/widgets/search-widget/class-search-widget.php b/includes/widgets/search-widget/class-search-widget.php index 95a4e12cb..d21c7c081 100644 --- a/includes/widgets/search-widget/class-search-widget.php +++ b/includes/widgets/search-widget/class-search-widget.php @@ -216,19 +216,19 @@ public static function get_input_types_by_field_type() { * @see admin-search-widget.js (getSelectInput) */ $input_types = array( - 'text' => array( 'input_text' ), - 'address' => array( 'input_text' ), - 'number' => array( 'input_text', 'number_range' ), - 'date' => array( 'date', 'date_range' ), - 'entry_date' => array( 'date_range' ), - 'boolean' => array( 'single_checkbox' ), - 'select' => array( 'select', 'radio', 'link' ), - 'multi' => array( 'select', 'multiselect', 'radio', 'checkbox', 'link' ), + 'text' => array( 'input_text' ), + 'address' => array( 'input_text' ), + 'number' => array( 'input_text', 'number_range' ), + 'date' => array( 'date', 'date_range' ), + 'entry_date' => array( 'date_range' ), + 'boolean' => array( 'single_checkbox' ), + 'select' => array( 'select', 'radio', 'link' ), + 'multi' => array( 'select', 'multiselect', 'radio', 'checkbox', 'link' ), // hybrids - 'created_by' => array( 'select', 'radio', 'checkbox', 'multiselect', 'link', 'input_text' ), - 'multi_text' => array( 'select', 'radio', 'checkbox', 'multiselect', 'link', 'input_text' ), - 'product' => array( 'select', 'radio', 'link', 'input_text', 'number_range' ), + 'created_by' => array( 'select', 'radio', 'checkbox', 'multiselect', 'link', 'input_text' ), + 'multi_text' => array( 'select', 'radio', 'checkbox', 'multiselect', 'link', 'input_text' ), + 'product' => array( 'select', 'radio', 'link', 'input_text', 'number_range' ), ); /** @@ -448,12 +448,18 @@ public static function render_searchable_fields( $form_id = null, $current = '' $blocklist_field_types = apply_filters( 'gravityview_blocklist_field_types', array( 'fileupload', 'post_image', 'post_id', 'section' ), null ); + $blocklist_sub_fields = apply_filters( 'gravityview_blocklist_sub_fields', array( 'image_choice', 'multi_choice' ), null ); + foreach ( $fields as $id => $field ) { if ( in_array( $field['type'], $blocklist_field_types ) ) { continue; } + if ( in_array( $field['type'], $blocklist_sub_fields ) && NULL !== $field['parent'] ) { + continue; + } + $types = self::get_search_input_types( $id, $field['type'] ); $output .= ''; @@ -480,7 +486,7 @@ public static function get_search_input_types( $field_id = '', $field_type = nul // @todo - This needs to be improved - many fields have . including products and addresses if ( false !== strpos( (string) $field_id, '.' ) && in_array( $field_type, array( 'checkbox' ) ) || in_array( $field_id, array( 'is_fulfilled' ) ) ) { $input_type = 'boolean'; // on/off checkbox - } elseif ( in_array( $field_type, array( 'checkbox', 'post_category', 'multiselect' ) ) ) { + } elseif ( in_array( $field_type, array( 'checkbox', 'post_category', 'multiselect', 'image_choice','multi_choice' ) ) ) { $input_type = 'multi'; // multiselect } elseif ( in_array( $field_id, array( 'payment_status' ) ) ) { $input_type = 'multi_text'; diff --git a/templates/fields/field-image_choice-html.php b/templates/fields/field-image_choice-html.php new file mode 100644 index 000000000..8ba1c4c0c --- /dev/null +++ b/templates/fields/field-image_choice-html.php @@ -0,0 +1,37 @@ +template ) ) { + gravityview()->log->error( '{file} template loaded without context', array( 'file' => __FILE__ ) ); + return; +} +$field = $gravityview->field->field; +$entry = $gravityview->entry->as_entry(); +$field_settings = $gravityview->field->as_configuration(); + +if ( 'image' === \GV\Utils::get( $field_settings, 'choice_display' ) ) { + $gravityview_view = GravityView_View::getInstance(); + $form = $gravityview_view->getForm(); + $image_choice = new GravityView_Field_Image_Choice( $field ); + + echo $image_choice->output_image_choice( $gravityview->value, $field, $form ); +} else { + /** + * Overrides whether to show the value or the label of a Image Choice field. + * + * @since TBD + * + * @param bool $show_label True: Show the label of the Choice; False: show the value of the Choice. Default: `false` + * @param array $entry GF Entry + * @param GF_Field_Select $field Gravity Forms Select field + * @param \GV\Template_Context $gravityview The context + */ + $show_label = apply_filters( 'gravityview/fields/image_choice/output_label', ( 'label' === \GV\Utils::get( $field_settings, 'choice_display' ) ), $entry, $field, $gravityview ); + + echo $field->get_value_entry_detail( $gravityview->value, '', $show_label ); +}