-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle RadioField submission values when using a custom "other" …
…choice (#421)
- Loading branch information
1 parent
e68b7c0
commit b70ef41
Showing
11 changed files
with
389 additions
and
5 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
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,85 @@ | ||
<?php | ||
/** | ||
* Manipulates input data for Radio field values. | ||
* | ||
* @package WPGraphQL\GF\Data\FieldValueInput | ||
* @since @todo | ||
*/ | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace WPGraphQL\GF\Data\FieldValueInput; | ||
|
||
use GFCommon; | ||
|
||
/** | ||
* Class - RadioValueInput | ||
*/ | ||
class RadioValueInput extends AbstractFieldValueInput { | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @var string | ||
*/ | ||
protected $args; | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @var array<int|string,?string> | ||
*/ | ||
public $value; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function get_field_name(): string { | ||
return 'value'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return array<int|string,?string> | ||
*/ | ||
protected function prepare_value() { | ||
$value = $this->args; | ||
|
||
// Handle values with price. | ||
if ( ! empty( $this->field->enablePrice ) && false === strpos( $value, '|' ) ) { | ||
$value_key = ! empty( $this->field->enablePrice ) || ! empty( $this->field->enableChoiceValue ) ? 'value' : 'text'; | ||
$choice_key = array_search( $value, array_column( $this->field->choices, $value_key ), true ); | ||
$choice = $this->field->choices[ $choice_key ]; | ||
$price = rgempty( 'price', $choice ) ? 0 : GFCommon::to_number( rgar( $choice, 'price' ) ); | ||
$value = $value . '|' . $price; | ||
} | ||
|
||
if ( empty( $this->field->enableOtherChoice ) ) { | ||
return [ | ||
$this->field->id => $value, | ||
]; | ||
} | ||
|
||
$allowed_values = wp_list_pluck( $this->field->choices, 'value' ); | ||
|
||
if ( ! in_array( $value, $allowed_values, true ) ) { | ||
$_POST[ $this->field->id . '_other' ] = $value; | ||
$_POST[ $this->field->id ] = 'gf_other_choice'; | ||
return [ | ||
$this->field->id => 'gf_other_choice', | ||
$this->field->id . '_other' => $value, | ||
]; | ||
} | ||
|
||
return [ | ||
$this->field->id => $value, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function add_value_to_submission( array &$field_values ): void { | ||
$field_values += $this->value; | ||
} | ||
} |
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
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
Oops, something went wrong.