Skip to content

Commit

Permalink
Empty value filter for checkbox field with no selected option (#208)
Browse files Browse the repository at this point in the history
* Catch type errors
* Add filter for checkbox field with no values
  • Loading branch information
doekenorg authored Dec 18, 2024
1 parent b88ae42 commit 8e5aef3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
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 ] );
}
}
}
}
}

0 comments on commit 8e5aef3

Please sign in to comment.