Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty value filter for checkbox field with no selected option #208

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ You can hide a row by adding a hook. Checkout this example:

== Changelog ==

= develop =

* Fixed: Type errors on fields no longer cause exports to fail. Values become empty, and errors are logged.
* Enhancement: Added a `gfexcel_field_checkbox_empty` filter to control checkbox output, when no option was selected.

= 2.3.5 on November 25, 2024 =

* Fixed: PHP notice in WordPress 6.7 caused by initializing product translations too early.
Expand Down
9 changes: 8 additions & 1 deletion src/Field/AbstractField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GFExcel\Addon\GravityExportAddon;
use GFExcel\Values\BaseValue;
use Throwable;

/**
* @since 1.0.0
Expand Down Expand Up @@ -137,7 +138,13 @@ protected function getGFieldValue( $entry, $input_id ) {
return date_i18n( 'Y-m-d H:i:s', $lead_local_time, true );
}

$value = $this->field->get_value_export( $entry, $input_id, $use_text = false, $is_csv = false );
// Prevent Type Errors from fields.
try {
$value = $this->field->get_value_export( $entry, $input_id, $use_text = false, $is_csv = false );
} catch ( Throwable $error ) {
GravityExportAddon::get_instance()->log_error( $error->getMessage() );
$value = null;
}

if ( is_string( $value ) ) {
$value = html_entity_decode( $value );
Expand Down
22 changes: 20 additions & 2 deletions src/Field/CheckboxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,33 @@ public function getRows( ?array $entry = null ): iterable {
);
yield $this->wrap( [ $value ] );
} else {
$has_values = false;
foreach ( $inputs as $input ) {
$index = (string) $input['id'];

if ( ! rgempty( $index, $entry ) ) {
$value = $this->filter_value( $this->getFieldValue( $entry, $index ), $entry );

$has_values = true;
$value = $this->filter_value( $this->getFieldValue( $entry, $index ), $entry );
yield $this->wrap( [ $value ] );
}
}

if ( ! $has_values ) {
$empty_value = gf_apply_filters(
[
'gfexcel_field_checkbox_empty',
$this->field->formId,
$this->field->id,
],
'',
$entry,
$this->field
);

if ( '' !== $empty_value ) {
yield $this->wrap( [ $empty_value ] );
}
}
}
}
}
Loading