Skip to content

Conversation

@saifsultanc
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/3069272683/89066

Summary

Updated gpnf_hec_column_has_value() to correctly detect values stored on sub-inputs (e.g. 8.3, 8.6) in addition to the actual field ID (e.g. 8).

Fields like Name or Address, which store values across multiple sub-input IDs, were not being detected.

… for fields having data stored on sub field IDs like Name, Address, etc.
@coderabbitai
Copy link

coderabbitai bot commented Sep 17, 2025

Walkthrough

Revised gpnf_hec_column_has_value() to scan each entry’s key/value pairs with a regex matching the base field ID and optional subfield suffix (.\d+), returning true on first non-blank match. This enables detecting values in multi-input subfields. Function signature and JS behavior remain unchanged.

Changes

Cohort / File(s) Summary
PHP: Nested subfield detection for Hide Empty Columns
gp-nested-forms/gpnf-hide-empty-columns.php
Updated gpnf_hec_column_has_value() internals to iterate entry keys and regex-match field_id(.subfield)?, returning true on any non-blank value; comments adjusted. No signature or JS changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant UI as Table Renderer
  participant PHP as GPNF PHP
  participant Func as gpnf_hec_column_has_value()
  participant Store as Entries

  UI->>PHP: Determine if column is empty
  PHP->>Func: gpnf_hec_column_has_value(field_id, entries)
  Func->>Store: Iterate entries
  loop For each entry
    Func->>Func: Regex match keys: ^field_id(\.\d+)?
    alt Any matched key has non-blank value
      Func-->>PHP: return true
      PHP-->>UI: Column has value
    end
  end
  Func-->>PHP: return false
  PHP-->>UI: Column empty
  Note over Func: Changed: key-level scan with regex to include subfields
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • veryspry
  • spivurno

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title accurately captures the main change—fixing gpnf-hide-empty-columns.php so the hide-empty-columns snippet detects values stored on sub-input IDs (e.g., Name, Address)—and therefore reflects the primary intent of the changeset. It is slightly verbose and contains a minor typo ("withsnippet" missing a space).
Description Check ✅ Passed The PR description follows the repository template by including a Context section with the ticket link and a Summary that succinctly explains the change to gpnf_hec_column_has_value() and the bug it fixes (detecting values on sub-input IDs like 8.3/8.6 for Name/Address fields). The description is clear, maps to the raw summary of code changes, and provides enough context for reviewers.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch saif/fix/89066-fix-subfield-ids

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against 23b7472

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
gp-nested-forms/gpnf-hide-empty-columns.php (1)

151-159: Fix client-side parity: scan sub‑input IDs in gpnfHecColumnHasValue.

Client JS gpnfHecColumnHasValue only checks entry[fieldId] and entry[f${fieldId}], ignoring sub‑input keys like "8.3", while the PHP/simple‑detail path uses gpnf_hec_column_has_value — this creates a parity gap that can leave columns hidden. (gravitywiz.com)

Apply this diff (mirrors PHP logic):

  <script>
    function gpnfHecColumnHasValue( fieldId, entries ) {
-      var hasValue = false;
-      jQuery.each( entries, function( i, entry ) {
-        if( ( entry[ fieldId ] && entry[ fieldId ].label ) || ( entry[ `f${fieldId}` ] && entry[ `f${fieldId}` ].label ) ) {
-          hasValue = true;
-        }
-      } );
-      return hasValue;
+      var id = String(fieldId);
+      var esc = id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+      var re = new RegExp('^' + esc + '(\\.\\d+)?$');
+      return entries.some(function(entry) {
+        // Aggregated label helpers first.
+        var f = entry['f' + id];
+        if (f && f.label && String(f.label).trim() !== '') return true;
+        var base = entry[id];
+        if (base && base.label && String(base.label).trim() !== '') return true;
+        // Scan sub‑inputs.
+        for (var k in entry) {
+          if (!Object.prototype.hasOwnProperty.call(entry, k)) continue;
+          if (!re.test(String(k))) continue;
+          var v = entry[k];
+          var label = (v && typeof v === 'object' && 'label' in v) ? v.label : v;
+          if (label != null && String(label).trim() !== '') return true;
+        }
+        return false;
+      });
    }
  </script>

Quick tests to run:

  • Add a Name field; submit only First Name — confirm column visible in default template.
  • Add an Address field; submit only Address Line 1 — confirm column visible in default template.
  • Repeat both checks in the simple detail template and confirm parity.
🧹 Nitpick comments (2)
gp-nested-forms/gpnf-hide-empty-columns.php (2)

166-172: Nit: precompute the regex once and tighten the comment.

Avoid rebuilding the pattern per key and fix the stray parenthesis in the comment.

Apply this diff:

 function gpnf_hec_column_has_value( $field_id, $entries ) {
   foreach ( $entries as $entry ) {
-    foreach ( $entry as $key => $value ) {
-      // Match $field_id or subfield IDs (For fields like Name, Address, etc).)
-      if ( preg_match( '/^' . preg_quote( $field_id, '/' ) . '(\.\d+)?$/', $key ) ) {
+    // Build once per entry (field_id is constant for this call).
+    $pattern = '/^' . preg_quote( (string) $field_id, '/' ) . '(?:\.\d+)?$/';
+    foreach ( $entry as $key => $value ) {
+      // Match $field_id or sub‑input IDs (fields like Name, Address, etc.).
+      if ( preg_match( $pattern, (string) $key ) ) {
         if ( ! rgblank( $value ) ) {
           return true;
         }
       }
     }
   }
   return false;
 }

104-116: Avoid recomputing visibility per row (minor perf/clarity).

gpnf_hec_column_has_value() is called repeatedly for each row. Compute once per field and reuse.

Example minimal change:

 <?php
 // before rendering the table header
+ $visible = array();
+ foreach ( $nested_fields as $nf ) {
+   $visible[ $nf->id ] = gpnf_hec_column_has_value( $nf->id, $entries );
+ }
 ?>
 ...
- foreach ( $nested_fields as $nested_field ) :
-   if ( ! gpnf_hec_column_has_value( $nested_field->id, $entries ) ) :
+ foreach ( $nested_fields as $nested_field ) :
+   if ( empty( $visible[ $nested_field->id ] ) ) :
      continue;
   endif;
 ...
- foreach ( $nested_fields as $nested_field ) :
-   if ( ! gpnf_hec_column_has_value( $nested_field->id, $entries ) ) :
+ foreach ( $nested_fields as $nested_field ) :
+   if ( empty( $visible[ $nested_field->id ] ) ) :
      continue;
   endif;

Also applies to: 124-136

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ad94f1d and 23b7472.

📒 Files selected for processing (1)
  • gp-nested-forms/gpnf-hide-empty-columns.php (1 hunks)
🔇 Additional comments (2)
gp-nested-forms/gpnf-hide-empty-columns.php (2)

166-172: Good fix: now correctly detects values on sub‑input IDs.

Anchored regex + preg_quote is safe and solves the Name/Address gaps server‑side. Looks good.

Please sanity‑check with entries where only a single sub‑input is filled (e.g., First Name only or Address Line 1 only) to ensure the column shows for:

  • nested-entries-detail-simple (PHP check in this file)
  • the default nested-entries template (client JS; see separate comment)

169-170: Whitespace‑only and array values (double‑check desired behavior).

rgblank() may treat "0" and whitespace differently than expected. If a sub‑input value is " " or an array of mostly blanks (edge cases), columns may show/hide unexpectedly.

If you want to ignore whitespace‑only strings and require at least one non‑blank element in arrays, consider:

$val = $value;
if ( is_string( $val ) ) {
    $val = trim( $val );
}
if ( is_array( $val ) ) {
    $val = array_filter( $val, function( $v ) { return ! rgblank( is_string( $v ) ? trim( $v ) : $v ); } );
    if ( ! empty( $val ) ) { return true; }
} elseif ( ! rgblank( $val ) ) {
    return true;
}

Please confirm whether this stricter behavior aligns with product expectations before changing.

@saifsultanc saifsultanc merged commit c75fe1d into master Sep 17, 2025
4 checks passed
@saifsultanc saifsultanc deleted the saif/fix/89066-fix-subfield-ids branch September 17, 2025 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants