Skip to content

Commit

Permalink
Issue/2120 support the new image choice and multiple choice fields in…
Browse files Browse the repository at this point in the history
… gravity forms 29 (#2174)

- Implements #2120
- Support the option to choose what to display, choice, value, or image
(for image choices) on the field options modal for both fields.
- Fixed both fields doesn't show the selected choice when editing (but
allows it to be edited)
- Added both fields to the Search Bar

💾 [Build
file](https://www.dropbox.com/scl/fi/obizyjguoqrfmwfn4k4fa/gravityview-2.29-9f627988e.zip?rlkey=78djrf2kuxbvmo38pfdszhazl&dl=1)
(9f62798).
  • Loading branch information
zackkatz authored Nov 4, 2024
2 parents ebb4ba9 + c1fb510 commit e94ea52
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 13 deletions.
2 changes: 1 addition & 1 deletion includes/extensions/edit-entry/class-edit-entry-render.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
119 changes: 119 additions & 0 deletions includes/fields/class-gravityview-field-image-choice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* @file class-gravityview-field-image-choice.php
* @package GravityView
* @subpackage includes\fields
*/

class GravityView_Field_Image_Choice extends GravityView_Field {

var $name = 'image_choice';

var $search_operators = array( 'is', 'in', 'not in', 'isnot', 'contains' );

var $is_searchable = true;

var $_gf_field_class_name = 'GF_Field_Image_Choice';

var $group = 'standard';

var $icon = 'dashicons-images-alt';

public function __construct() {
$this->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();
65 changes: 65 additions & 0 deletions includes/fields/class-gravityview-field-multiple-choice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* @file class-gravityview-field-multiple-choice.php
* @package GravityView
* @subpackage includes\fields
*/

class GravityView_Field_Multiple_Choice extends GravityView_Field {

var $name = 'multi_choice';

var $search_operators = array( 'is', 'in', 'not in', 'isnot', 'contains' );

var $is_searchable = true;

var $_gf_field_class_name = 'GF_Field_Multiple_Choice';

var $group = 'standard';

var $icon = 'dashicons-list-view';

public function __construct() {
$this->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();
30 changes: 18 additions & 12 deletions includes/widgets/search-widget/class-search-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
);

/**
Expand Down Expand Up @@ -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 .= '<option value="' . $id . '" ' . selected( $id, $current, false ) . 'data-inputtypes="' . esc_attr( $types ) . '" data-placeholder="'.esc_html( $field['label'] ).'">' . esc_html( $field['text'] ?? $field['label'] ) . '</option>';
Expand All @@ -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';
Expand Down
37 changes: 37 additions & 0 deletions templates/fields/field-image_choice-html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* The default image choice field output template.
*
* @global \GV\Template_Context $gravityview
* @since 2.0
*/

if ( ! isset( $gravityview ) || empty( $gravityview->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 );
}

0 comments on commit e94ea52

Please sign in to comment.