Skip to content

Instantly share code, notes, and snippets.

Created December 27, 2012 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4390824 to your computer and use it in GitHub Desktop.
Save anonymous/4390824 to your computer and use it in GitHub Desktop.
Pods 2.0 Gravity Forms input helper. Use a text or code field.
<?php
wp_enqueue_style( 'pods-select2' );
wp_enqueue_script( 'pods-select2' );
$attributes = array();
$attributes[ 'tabindex' ] = 2;
$pick_limit = (int) pods_var( 'pick_limit', $options, 0 );
$name .= '[]';
$attributes[ 'multiple' ] = 'multiple';
if ( !is_array( $options[ 'data' ] ) && false !== $options[ 'data' ] && 0 < strlen( $options[ 'data' ] ) )
$options[ 'data' ] = implode( ',', $options[ 'data' ] );
else
$options[ 'data' ] = (array) pods_var_raw( 'data', $options, array(), null, true );
$attributes = PodsForm::merge_attributes( $attributes, $name, PodsForm::$field_type, $options );
if( class_exists( 'RGFormsModel' ) )
{
$forms = RGFormsModel::get_forms(1, "title");
if( count( $forms ) > 0 )
{ ?>
<div class="pods-select2">
<select<?php PodsForm::attributes( $attributes, $name, PodsForm::$field_type, $options ); ?> style="width:95%;">
<?php
foreach( $forms as $form ) :
$selected = '';
if ( $form->id == $value || ( is_array( $value ) && in_array( $form->id, $value ) ) )
$selected = ' SELECTED';
if ( is_array( $option_value ) ) {
?>
<option<?php PodsForm::attributes( $form->id, $name, PodsForm::$field_type . '_option', $options ); ?>><?php echo $form->title; ?></option>
<?php
}
else {
?>
<option value="<?php echo $form->id; ?>"<?php echo $selected; ?>><?php echo $form->title; ?></option>
<?php
}
endforeach ?>
</select>
</div>
<script type="text/javascript">
jQuery( function ( $ ) {
if ( typeof pods_ajaxurl === "undefined" ) {
var pods_ajaxurl = "<?php echo admin_url( 'admin-ajax.php?pods_ajax=1' ); ?>";
}
function <?php echo pods_clean_name( $attributes[ 'id' ] ); ?>_podsFormatResult ( item ) {
return item.text;
}
function <?php echo pods_clean_name( $attributes[ 'id' ] ); ?>_podsFormatSelection ( item ) {
return item.text;
}
var <?php echo pods_clean_name( $attributes[ 'id' ] ); ?>_data = {<?php
if ( !is_object( $pod ) || !empty( $options[ 'data' ] ) ) {
$data = array();
foreach ( $options[ 'data' ] as $item_id => $item ) {
$data[] = '\'' . esc_js( $item_id ) . '\' : {id : \'' . esc_js( $item_id ) . '\', text: \'' . esc_js( $item ) . '\'}';
}
echo implode( ",\n", $data );
}
?>};
var $element = $('#<?php echo $attributes[ 'id' ] ?>' );
$element.select2({
placeholder: "Choose one or several forms"
});
});
</script>
<?php }
else
{ ?>
<input type="hidden" id="<?php echo $css_id; ?>" name="<?php echo $css_id; ?>" value="0" />
<p>There are no forms available at this time. You may <a href="admin.php?page=gf_new_form">create one</a> now (<strong>warning</strong> entered data <em>will be lost</em> unless you save first)</p>
<?php }
}
else
{ ?>
<p>Gravity Forms is currently not available.</p>
<?php }
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment