Last active
August 29, 2015 14:00
-
-
Save aliciaduffy/11371805 to your computer and use it in GitHub Desktop.
Quiz Field Change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( "gform_field_choices", "custom_gravity_add_media_credit", 10, 2 ); | |
function custom_gravity_add_media_credit( $choices, $field ) { | |
// If field isn't quiz or radio, and isn't admin | |
if ( ( $field["type"] != "radio" && $field["type"] != "quiz" ) || is_admin() ) { | |
return $choices; | |
} | |
// If isn't an image layout | |
if ( $field["answerLayoutField"] == "list" || !$field["answerLayoutField"] ) { | |
return $choices; | |
} | |
// get current form ID # | |
$current_form = $field["formId"]; | |
// get current page | |
$current_page = GFFormDisplay::get_current_page($current_form); | |
/* if this field is not on current page do not reformat | |
prevents us from losing correct/incorrect quiz choices on answer calculation */ | |
if ( $field["pageNumber"] != $current_page ) { | |
return $choices; | |
} | |
$choices = ""; | |
if ( is_array( $field["choices"] ) ) { | |
$choice_id = 0; | |
$count = 1; | |
$logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $field["formId"] . "," . GFCommon::json_encode($field["conditionalLogicFields"]) . ");'" : ""; | |
foreach( $field["choices"] as $choice ) { | |
// Grab image ID from img tag | |
$image_id = explode('data-id="',$choice["text"]); | |
$image_id = explode('">', $image_id[1]); | |
// Get media credit | |
$media_credit = the_media_credit_html( $image_id[0] ); | |
$updated_choice = $choice["text"]; | |
if ( $field["answerLayoutField"] == "grid-2" ) { | |
$new_image = wp_get_attachment_image_src( $image_id[0], '637-320' ); | |
} else if ( $field["answerLayoutField"] == "grid-4" ) { | |
$new_image = wp_get_attachment_image_src( $image_id[0], 'thumbnail' ); | |
} else { | |
$new_image = wp_get_attachment_image_src( $image_id[0], '637-320' ); | |
} | |
// Replace img tag w/ resized image | |
$updated_choice = preg_replace("/src=\"(\S)*\"/", "src=\"$new_image[0]\"/", $updated_choice); | |
// Wrap image in <div> and add media credit | |
$updated_choice = preg_replace( '/(<img([^>]*)>)/i', '<div class="thumb">$1 ' . $media_credit . '</div>', $updated_choice ); | |
$id = $field["id"] . '_' . $choice_id++; | |
$field_value = !empty($choice["value"]) || rgar($field, "enableChoiceValue") ? $choice["value"] : $choice["text"]; | |
$checked = rgar($choice,"isSelected") ? "checked='checked'" : ""; | |
$tabindex = GFCommon::get_tabindex(); | |
$input = sprintf("<input name='input_%d' type='radio' value='%s' %s id='choice_%s' $tabindex $logic_event />", $field["id"], esc_attr($field_value), $checked, $id); | |
$choices .= sprintf("<li class='gchoice_$id gchoice_img'>%s <div><label for='choice_%s' id='label_%s'>%s </label></div></li>", $input, $id, $id, $updated_choice); | |
$count++; | |
} | |
} | |
return $choices; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment