Skip to content

Commit

Permalink
Merge pull request #25 from jonathan-dejong/master
Browse files Browse the repository at this point in the history
Fixed format_value and minor tweaks
  • Loading branch information
lgladdy committed Apr 24, 2015
2 parents deb7971 + aff1783 commit ac0fae5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 77 deletions.
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Compatibility

This add-on will work with:

* version 4 and up
* version 4
* Version 5

Installation
============
Expand All @@ -34,28 +35,37 @@ Using the field

The field lets you pick one or many fields.

The data returned is either a Form object or an array of [Form objects](http://www.gravityhelp.com/documentation/page/Form_Object).
The data returned is either a Form object, an array of [Form objects](http://www.gravityhelp.com/documentation/page/Form_Object) or false if an error occurred.

If you have selected a single form and you want to display the form on the page, you can use:

```
<?php
$form = get_field('your_form_field');
gravity_form_enqueue_scripts($form->id, true);
gravity_form($form->id, true, true, false, '', true, 1);
$form_object = get_field('your_form_field');
gravity_form_enqueue_scripts($form_object['id'], true);
gravity_form($form_object['id'], true, true, false, '', true, 1);
?>
```

You can find out more about the gravity_form method to embed a form on a page in their [documentation](http://www.gravityhelp.com/documentation/page/Embedding_A_Form#Function_Call)
or

```
<?php
$form_object = get_field('your_form_field');
echo do_shortcode('[gravityform id="' . $form_object['id'] . '" title="true" description="true" ajax="true"]');
?>
```

You can find out more about the gravity_form method to embed a form on a page in their [documentation](http://www.gravityhelp.com/documentation/page/Embedding_A_Form)

If you are using the field to select multiple forms, you will have to iterate over the array. You can then use the form object as you like:

```
<?php
$forms = get_field('your_forms');
$form_objects = get_field('your_forms');
foreach($forms as $form){
echo $form->title;
foreach($form_objects as $form){
echo $form['title'];
}
?>
```
Expand All @@ -66,7 +76,7 @@ If you are using the field to select multiple forms, you will have to iterate ov
About
=====

Version: 1.1
Version: 1.2

Written by Adam Pope and Liam Gladdy of [Storm Consultancy](http://www.stormconsultancy.co.uk) and the amazing contributors on [Github](https://github.com/stormuk/Gravity-Forms-ACF-Field/graphs/contributors)

Expand Down
53 changes: 29 additions & 24 deletions gravity_forms-v4.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,36 +161,41 @@ function create_field( $field )
function format_value_for_api( $value, $field )
{

//format_value
if( !$value )
{
return $value;
}

if( $value == 'null' )
{
return false;
//Return false if value is false, null or empty
if( !$value || empty($value) ){
return false;
}

//If there are multiple forms, construct and return an array of form markup
if(is_array($value)){
//If there are multiple forms, construct and return an array of form objects
if( is_array($value) && !empty($value) ){

$form_objects = array();
foreach($value as $k => $v){
$form = GFAPI::get_form( $v );
$f = do_shortcode('[gravityform id="'.$form['id'].'" title="'.$form['title'].'"]');
$value[$k] = array();
$value[$k] = $f;
//Add it if it's not an error object
if( !is_wp_error($form) ){
$form_objects[$k] = $form;
}
}
//Return false if the array is empty
if( !empty($form_objects) ){
return $form_objects;
}else{
return false;
}


//Else return single form object
}else{

$form = GFAPI::get_form(intval($value));
//Return the form object if it's not an error object. Otherwise return false.
if( !is_wp_error($form) ){
return $form;
}else{
return false;
}

return $value;

//Else return single form markup
} else{
//$value can be mixed, make it an int
$value = intval($value);
//get the form object
$form = GFAPI::get_form( $value );
//we can directly echo the shortcode here
echo do_shortcode('[gravityform id="'.$form['id'].'" title="'.$form['title'].'"]');
}

}
Expand Down
88 changes: 45 additions & 43 deletions gravity_forms-v5.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ function render_field( $field ) {
}

//Prevent undefined variable notice
if(isset($forms))
{
foreach( $forms as $form )
{
$choices[ $form->id ] = ucfirst($form->title);
if(isset($forms)){
foreach( $forms as $form ){
$choices[ intval($form->id) ] = ucfirst($form->title);
}
}
// override field settings and render
Expand All @@ -128,20 +126,19 @@ function render_field( $field ) {
}
else $multiple = '';
?>

<select id="<?php echo str_replace(array('[',']'), array('-',''), $field['name']);?>" name="<?php echo $field['name']; if( $field['allow_multiple'] ) echo "[]"; ?>"<?php echo $multiple; ?>>
<select id="<?php echo str_replace(array('[',']'), array('-',''), $field['name']);?>" name="<?php echo $field['name']; if( $field['allow_multiple'] ) echo "[]"; ?>"<?php echo $multiple; ?>>
<?php
if ( $field['allow_null'] ) echo '<option value="">- Select -</option>';
foreach ($field['choices'] as $key => $value) :
$selected = '';
if ( (is_array($field['value']) && in_array($key, $field['value'])) || $field['value'] == $key )
$selected = ' selected="selected"';
?>
<option value="<?php echo $key; ?>"<?php echo $selected;?>><?php echo $value; ?></option>
<?php endforeach;
?>
</select>
if ( $field['allow_null'] )
echo '<option value="">- Select -</option>';
foreach ($field['choices'] as $key => $value){
$selected = '';
if ( (is_array($field['value']) && in_array($key, $field['value'])) || $field['value'] == $key )
$selected = ' selected="selected"';
?>
<option value="<?php echo $key; ?>"<?php echo $selected;?>><?php echo $value; ?></option>
<?php } ?>
</select>
<?php
}

Expand All @@ -164,38 +161,43 @@ function render_field( $field ) {

function format_value( $value, $post_id, $field ) {

//format_value
if( !$value )
{
return $value;
}

if( $value == 'null' )
{
return false;
//Return false if value is false, null or empty
if( !$value || empty($value) ){
return false;
}

//If there are multiple forms, construct and return an array of form markup
if(is_array($value)){
//If there are multiple forms, construct and return an array of form objects
if( is_array($value) && !empty($value) ){

$form_objects = array();
foreach($value as $k => $v){
$form = GFAPI::get_form( $v );
$f = do_shortcode('[gravityform id="'.$form['id'].'" title="'.$form['title'].'"]');
$value[$k] = array();
$value[$k] = $f;
//Add it if it's not an error object
if( !is_wp_error($form) ){
$form_objects[$k] = $form;
}
}
//Return false if the array is empty
if( !empty($form_objects) ){
return $form_objects;
}else{
return false;
}

return $value;

//Else return single form markup
} else{
//$value can be mixed, make it an int
$value = intval($value);
//get the form object
$form = GFAPI::get_form( $value );
//we can directly echo the shortcode here
echo do_shortcode('[gravityform id="'.$form['id'].'" title="'.$form['title'].'"]');
}

//Else return single form object
}else{

$form = GFAPI::get_form(intval($value));
//Return the form object if it's not an error object. Otherwise return false.
if( !is_wp_error($form) ){
return $form;
}else{
return false;
}

}

}

}
Expand Down

0 comments on commit ac0fae5

Please sign in to comment.