I have some code that I can’t quite get to work the way I need it to and I’m hoping someone else can look at it and see where I’m going wrong. I’ve searched but can’t seem to find a post that’s quite the same.
I have 2 forms: Form A and Form B. Form A is the main form that gets completed. Once completed, the workflow sends out Form B to multiple people to complete. Once completed, I need to display certain field entries from Form B in a table inside Form A. I have set up a snippet to do this and am including it below.
As of right now, the code below DOES create a table and it has all the correct cells showing. The issue is that the last 2 cells are not being populated. One of those is supposed to display a score (based on a calculation field) and the other is supposed to display the status of the form as either having been submitted or not submitted (that field is a text field set to populate with the status of the relevant workflow step). Here’s the code:
//{rda_scoreform_feedback:review_fields='502,503,506,508,510,512,514,516,518,520,522,524,526,528,530,532,534,536,538,540, 594, 596'}
add_filter( 'gform_replace_merge_tags', 'rda_mergetag_scoreform_feedback', 10, 7 );
function rda_mergetag_scoreform_feedback( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
$matches = array();
preg_match_all( '/{rda_scoreform_feedback(:(.*?))?}/', $text, $matches, PREG_SET_ORDER );
if ( ! empty( $matches ) ) {
foreach ( $matches as $match ) {
//Get the parts of the merge tag into separate values we can evaluate against
$full_tag = $match[0];
$options_string = isset( $match[2] ) ? $match[2] : false;
$attributes = shortcode_parse_atts( $options_string );
if ( ! isset( $attributes['review_fields'] ) ) {
$text = str_replace( $full_tag, '', $text );
} else {
//Begin to craft the markup as it is on the specific merge tag of interest
$markup = '<div class="rda-scoreform-feedback">';
$markup .= '<table id="scoreform-feedback">
<thead><tr>
<th>Entry ID</th>
<th>Reviewer</th>
<th>Role</th>
<th>Feedback</th>
<th>Score</th>
<th>Status</th>
</tr></thead><tbody>';
//Get the field IDs into an array
$feedback_ids = explode( ',', $attributes['review_fields'] );
//Loop each ID and ensure that it is a valid entry with a completed workflow
foreach( $feedback_ids as $position => $feedback_id ) {
$feedback = GFAPI::get_entry( $entry[ $feedback_id ] );
if ( is_wp_error( $feedback ) ) {
continue;
}
//Add the feedback to the markup
$markup .= '<tr><td>' . $feedback['id'] . '<td>' . $feedback['114.3'] . ' ' . $feedback['114.6'] . '</td><td>' . $feedback['1'] . '</td><td>' . $feedback['10'] . ' ' .$feedback['81'] . ' ' .$feedback['23'] . ' ' .$feedback['115'] . ' ' .$feedback['111'] . ' ' .$feedback['147'] . ' ' .$feedback['91'] . ' ' .$feedback['132'] . ' ' .$feedback['33'] . '</td><td>' . $feedback['43'] . '</td><td>' . $feedback['174'] . '</td></tr>';
}
$markup .= '</tbody></table>';
//Put the markup back to the $text which the merge tag function returns.
$text = str_replace( $full_tag, $markup, $text );
}
}
}
return $text;
}