Skip to content

Instantly share code, notes, and snippets.

@eugenoprea
Created March 13, 2013 14:46
Show Gist options
  • Save eugenoprea/5152806 to your computer and use it in GitHub Desktop.
Save eugenoprea/5152806 to your computer and use it in GitHub Desktop.
Gravity Forms - Populate Name Field - Setting Field Default Value Directly - Part 1.
/**
* Populate a Gravity Forms Name field.
*
* @param array $field Gravity Forms field object
* @param string|array $name Name components, pass either a string or array when the
* nameFormat is 'simple' or an array in all other cases
*/
function eo_populate_name_field(&$field, $name = array())
{
if ( !(is_array($field) && ('name' == rgar($field, 'type')) && !empty($name)) )
return;
$field_id = $field['id'];
$format = rgar($field, 'nameFormat');
if ( is_array($name) )
{
$first = sanitize_text_field(rgar($name, 'first'));
$last = sanitize_text_field(rgar($name, 'last'));
}
switch ($format)
{
case 'simple':
$default_value = isset($first, $last) ? ($first . ' ' . $last) : $name;
$default_value = sanitize_text_field($default_value);
break;
case 'normal':
$default_value = array_filter(array
(
(strval($field_id . '.3')) => $first,
(strval($field_id . '.6')) => $last,
));
break;
case 'extended':
$prefix = sanitize_text_field(rgar($name, 'prefix'));
$suffix = sanitize_text_field(rgar($name, 'suffix'));
$default_value = array_filter(array
(
(strval($field_id . '.2')) => $prefix,
(strval($field_id . '.3')) => $first,
(strval($field_id . '.6')) => $last,
(strval($field_id . '.8')) => $suffix,
));
break;
}
if ( !empty($default_value) )
$field['defaultValue'] = $default_value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment