Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populating Time Field #18

Open
dennisheiden opened this issue Jan 19, 2018 · 0 comments
Open

Populating Time Field #18

dennisheiden opened this issue Jan 19, 2018 · 0 comments

Comments

@dennisheiden
Copy link

dennisheiden commented Jan 19, 2018

Hi,

I had some trouble to populate a time field which needs 3 default values for HH, MM and AM/PM.

I had to extend the populate_element(...) function with an entry for field_type = time:

public static function populate_element( $field, $field_type, $value )
	{

		$value = maybe_unserialize($value);
		
		switch ( $field_type )
		{
			case 'time':
				if(!empty($value)){
					$string = $value;
					$pm_am = '';
					if(strpos('am',$string) !== false){
						$pm_am = 'am';
						$string = replace('am','',$string);
					}
					if(strpos('pm',$string) !== false){
						$pm_am = 'pm';
						$string = replace('pm','',$string);
					}
					$time	= explode(':',$string);
					if(!is_array($time)) break;
					$inputs = array(
						$field['inputs'][0],
						$field['inputs'][1],
						$field['inputs'][2]
					);
					$inputs[0]['defaultValue'] = $time[0];
					$inputs[1]['defaultValue'] = $time[1];
					$inputs[2]['defaultValue'] = $pm_am;
				}
				$field['inputs'] = $inputs;
				break;
			case 'post_category':

				$field['allowsPrepopulate'] = true;
				$field['inputName'] = $field_type;

				self::$settings['cat_value'] = $value;
				add_filter( 'gform_field_value_' . $field['inputName'], array(__CLASS__, 'return_category_field_value'), 10, 2 );
				#add_filter( 'gform_field_value_' . $field['inputName'], function($value) use($value) { return $value; } );
				break;

			case 'populateTaxonomy':

				$field['allowsPrepopulate'] = true;
				$field['inputName'] = $field['populateTaxonomy'];

				self::$settings['tax_value'][$field['inputName']] = $value;
				add_filter( 'gform_field_value_' . $field['inputName'], array(__CLASS__, 'return_taxonomy_field_value') , 10, 2 );

				$value = (! is_array($value) ) ? array($value) : $value;

				if ( version_compare(GFCommon::$version, '1.9') >= 0 )
				{
					if ( isset($field->choices) )
					{
						foreach ( $field->choices as &$choice ) {
							$choice['isSelected'] = ( in_array($choice['value'], $value) ) ? true : '';
						}
					}
				}
				else
				{
					if ( isset($field['choices']) )
					{
						foreach ( $field['choices'] as &$choice ) {
							$choice['isSelected'] = ( in_array($choice['value'], $value) ) ? true : '';
						}
					}
				}

				#add_filter( 'gform_field_value_' . $field['inputName'], function($value) use($value) { return $value; } );
				break;

			case 'list':

				if ( is_array($value) )
				{
					$new_value = array();
					foreach ( $value as $row )
					{
						$row_array = explode('|', $row);
						$new_value = array_merge($new_value, $row_array);
					}
					$field['allowsPrepopulate'] = true;
					$field['inputName'] = $field['postCustomFieldName'];

					$value = $new_value;
					add_filter( 'gform_field_value_' . $field['inputName'], function($value) use($value) { return $value; } );
				}
				break;

			#case 'select':
			case 'multiselect':
			case 'checkbox':
			#case 'radio':

				$value = (! is_array($value) ) ? array($value) : $value;

				if ( version_compare(GFCommon::$version, '1.9') >= 0 )
				{
					if ( isset($field->choices) )
					{
						foreach ( $field->choices as &$choice ) {
							$choice['isSelected'] = ( in_array($choice['value'], $value) ) ? true : '';
						}
					}
				}
				else
				{
					if ( isset($field['choices']) )
					{
						foreach ( $field['choices'] as &$choice ) {
							$choice['isSelected'] = ( in_array($choice['value'], $value) ) ? true : '';
						}
					}
				}

				break;

			default:

				if ( is_array($value) )
				{
					$value = implode(', ', $value);
				}

				$field['defaultValue'] = $value;
				break;
		}
	
			
		return $field;
	}

Not an elegant solution and without the use for filters, but it works for the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant