-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue/2120 support the new image choice and multiple choice fields in…
… 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
Showing
5 changed files
with
240 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
includes/fields/class-gravityview-field-image-choice.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
65
includes/fields/class-gravityview-field-multiple-choice.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |