Skip to content

gw-display-html-field-on-entry-detail.php: Added a snippet to display HTML field content on Entry Details page. #1074

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

saifsultanc
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/2901917754/81943/

Summary

Allow HTML field content to displayed on Entry Details page.

https://www.loom.com/share/fa4b49240b6447c7839392af69476bf2

…ay HTML field content on Entry Details page.
…ay HTML field content on Entry Details page.
Copy link

coderabbitai bot commented Apr 14, 2025

Walkthrough

A new PHP class, GW_Display_HTML_Field_Entry_Detail, has been introduced in the Gravity Forms plugin. This class manages the saving of HTML field content during form entry submission and later displays it on entry detail pages. It accepts configuration parameters for form and field IDs, hooks into Gravity Forms events, saves HTML content to entry metadata via the save_html_field_content method, and processes the content for display (applying live merge tags and shortcodes) using the display_html_field_content method.

Changes

File(s) Change Summary
gravity-forms/gw-display-html-field-on-entry-detail.php Added new class GW_Display_HTML_Field_Entry_Detail that hooks into Gravity Forms to save HTML field content on entry save and to display it on entry details.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant GF as Gravity Forms Engine
    participant GW as GW_Display_HTML_Field_Entry_Detail
    User->>GF: Submit form entry
    GF->>GW: Trigger save_html_field_content hook
    GW->>GW: Validate form ID and process HTML fields
    GW->>GF: Save HTML field content to entry metadata
Loading
sequenceDiagram
    participant User
    participant GF as Gravity Forms Engine
    participant GW as GW_Display_HTML_Field_Entry_Detail
    User->>GF: Request entry detail view
    GF->>GW: Trigger display_html_field_content hook
    GW->>GW: Retrieve and process saved HTML content<br/>(apply merge tags & shortcodes)
    GW->>GF: Return formatted HTML content
    GF->>User: Render entry detail page with HTML content
Loading

Suggested reviewers

  • veryspry

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Apr 14, 2025

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.
Messages
📖 A new snippet with a Loom video? Magical! 📹
📖 Merlin would give this scroll the highest of praises. Cheers for adding this new snippet to the library! 🪄

Generated by 🚫 dangerJS against 50ff4df

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

🧹 Nitpick comments (1)
gravity-forms/gw-display-html-field-on-entry-detail.php (1)

80-118: Secure processing and display of HTML content.

The method properly:

  1. Retrieves saved content
  2. Conditionally processes Live Merge Tags
  3. Processes shortcodes
  4. Securely outputs content with proper escaping via esc_html() and wp_kses_post()

Consider making the HTML output structure more customizable for different display requirements.

- printf(
-   '<h4>%s</h4><div>%s</div><hr>',
-   esc_html( $field->label ),
-   wp_kses_post( $content )
- );
+ // Add a filter to allow customization of the HTML output structure
+ $output_template = apply_filters(
+   'gw_display_html_field_template', 
+   '<h4>%1$s</h4><div class="gw-html-field-content">%2$s</div><hr>',
+   $field,
+   $entry
+ );
+ printf(
+   $output_template,
+   esc_html( $field->label ),
+   wp_kses_post( $content )
+ );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0390f25 and 24a9e26.

📒 Files selected for processing (1)
  • gravity-forms/gw-display-html-field-on-entry-detail.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
gravity-forms/gw-display-html-field-on-entry-detail.php (1)
gravity-forms/gw-calculated-shipping.php (1)
  • field (91-117)
🔇 Additional comments (6)
gravity-forms/gw-display-html-field-on-entry-detail.php (6)

1-16: Well-structured plugin header with clear documentation.

The header includes a descriptive title, purpose explanation, and instruction video link, which are excellent for helping users understand the snippet's functionality.


19-31: Clean implementation of constructor with proper default values.

The constructor properly uses wp_parse_args() to merge user arguments with defaults, providing a flexible API for customization.


33-37: Good use of WordPress hooks for integration with Gravity Forms.

The initialization method properly hooks into the Gravity Forms lifecycle at appropriate points - after entry save and during entry detail display.


39-43: Appropriate form validation logic.

The helper method handles both array and integer input formats, with proper type casting for comparison.


51-72: Safe implementation of HTML field content saving.

The method correctly validates the form, iterates through fields, and saves HTML content to entry metadata. It properly uses Gravity Forms API functions and applies appropriate conditions.


121-125: Update hardcoded IDs in the configuration.

The snippet includes specific form and field IDs in the instantiation that need to be updated for your specific use case.

Make sure to replace the hardcoded IDs (form_id: 846, field_id: 4) with your actual form and field IDs before using this snippet in production. Alternatively, you could set these to false to process HTML fields across all forms:

new GW_Display_HTML_Field_Entry_Detail( array(
-	'form_id'  => 846, // Replace with your form ID or leave false for all.
-	'field_id' => 4,   // Replace with your HTML field ID or leave false to process all HTML fields.
+	'form_id'  => false, // Set your specific form ID or leave as false for all forms
+	'field_id' => false, // Set your specific HTML field ID or leave as false for all HTML fields
) );

@spivurno
Copy link
Contributor

@saifsultanc Just a note this should support the Entry List as well. 🙏

…ay HTML field content on Entry Details page.
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: 1

🧹 Nitpick comments (3)
gravity-forms/gw-display-html-field-on-entry-detail.php (3)

35-36: Consider extending support to the Entry List page.
At present, the snippet only hooks into gform_entry_detail. Per the PR discussion, you may also wish to display the HTML field on the Entry List. Gravity Forms offers filters (e.g., gform_entries_list_columns or gform_entries_list_column_value) for customizing column output in listing views.

Here's a starting point if you decide to pursue this:

 add_action( 'gform_entry_detail', array( $this, 'display_html_field_content' ), 10, 2 );
+// Consider hooking into the entries list to display HTML field content in the listing view as well.
+add_filter( 'gform_entries_list_column_value', array( $this, 'display_html_field_in_list' ), 10, 4 );

100-105: Check for the class existence before invoking methods.
method_exists( 'GP_Populate_Anything_Live_Merge_Tags', 'has_live_merge_tag' ) will return false if the class does not exist, but referencing GP_Populate_Anything_Live_Merge_Tags::get_instance() could still cause an error if the class is not loaded. Consider adding a class_exists() check for complete safety.


113-118: Use a different heading level or remove the <hr> for a cleaner layout.
Using <h4> and <hr> for each field might be visually cluttered on certain admin pages. If multiple HTML fields exist, consider a simpler heading or a refined stylistic approach.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 24a9e26 and 576d849.

📒 Files selected for processing (1)
  • gravity-forms/gw-display-html-field-on-entry-detail.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
gravity-forms/gw-display-html-field-on-entry-detail.php (1)
gravity-forms/gw-calculated-shipping.php (1)
  • field (91-117)
🔇 Additional comments (1)
gravity-forms/gw-display-html-field-on-entry-detail.php (1)

124-129: Configuration appears correct.
Current instantiation targets form ID 846 and field ID 4. This is fine for a single form and field setup. If you want to process multiple fields or multiple forms, consider leaving ‘field_id’ or ‘form_id’ as false.

…ay HTML field content on Entry Details page.
@saifsultanc
Copy link
Contributor Author

@saifsultanc Just a note this should support the Entry List as well. 🙏

Yup. Done!

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

♻️ Duplicate comments (1)
gravity-forms/gw-display-html-field-on-entry-detail.php (1)

66-72: ⚠️ Potential issue

Validate $form before referencing $form['fields'].
If GFAPI::get_form() returns false, $form will not be an array and $form['fields'] will trigger an error. This repeats a concern raised in previous reviews. Consider adding a guard clause to prevent potential PHP notices or fatal errors.

 $form = GFAPI::get_form( rgar( $entry, 'form_id' ) );

+if ( ! is_array( $form ) ) {
+    return $entry;
+}

 if ( ! $this->is_applicable_form( $form ) ) {
     return $entry;
 }
🧹 Nitpick comments (1)
gravity-forms/gw-display-html-field-on-entry-detail.php (1)

102-121: Consider unifying or documenting your display logic for Entry Detail and Entry List.
Both methods share similar steps (fetch and process content). Creating a reusable helper or adding docblocks could improve maintainability and clarity.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 576d849 and 50ff4df.

📒 Files selected for processing (1)
  • gravity-forms/gw-display-html-field-on-entry-detail.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
gravity-forms/gw-display-html-field-on-entry-detail.php (1)
gravity-forms/gw-calculated-shipping.php (1)
  • field (91-117)
🔇 Additional comments (2)
gravity-forms/gw-display-html-field-on-entry-detail.php (2)

81-100: Entry Detail Rendering Logic Looks Good.
This section correctly retrieves the saved HTML content for each applicable field and processes it for display. Great job ensuring that only valid HTML fields are processed.


124-129: Configuration Instantiation is Straightforward!
Everything looks good here. This is a clear and minimal setup for specifying the form and field IDs.

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