2015-03-16 21:18:03 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
									
										
										
										
											2015-09-10 02:45:52 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								nav: blog
							 
						 
					
						
							
								
									
										
										
										
											2015-03-16 21:18:03 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								title: How to add a date popup calendar onto a custom form
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								slug: add-date-popup-calendar-custom-form
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								tags:
							 
						 
					
						
							
								
									
										
										
										
											2015-06-14 03:27:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  forms
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  form-api
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  date
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  calendar
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  drupal-7
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  drupal-planet
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  drupal
							 
						 
					
						
							
								
									
										
										
										
											2015-03-16 21:18:03 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
									
										
										
										
											2015-06-18 08:58:56 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{% block excerpt %}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								How to use a date popup calendar within your custom module.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{% endblock %}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{% block content %}
							 
						 
					
						
							
								
									
										
										
										
											2015-03-18 21:16:06 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								First, I need to download the [Date ](http://drupal.org/project/date "Date module on Drupal.org" ) module, and make my module dependent on date_popup by adding the following line into my module's .info file.
							 
						 
					
						
							
								
									
										
										
										
											2015-03-16 21:18:03 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-14 03:27:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    dependencies[] = date_popup
							 
						 
					
						
							
								
									
										
										
										
											2015-03-16 21:18:03 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Within my form builder function:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								~~~php
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$form['date'] = array(
							 
						 
					
						
							
								
									
										
										
										
											2015-06-14 03:27:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  '#title ' => t('Arrival date'),
							 
						 
					
						
							
								
									
										
										
										
											2015-03-16 21:18:03 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-14 03:27:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  // Provided by the date_popup module
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  '#type ' => 'date_popup',
							 
						 
					
						
							
								
									
										
										
										
											2015-03-16 21:18:03 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-14 03:27:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  // Uses the PHP date() format - http://php.net/manual/en/function.date.php
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  '#date_format ' => 'j F Y',
							 
						 
					
						
							
								
									
										
										
										
											2015-03-16 21:18:03 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-14 03:27:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  // Limits the year range to the next two upcoming years
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  '#date_year_range ' => '0:+2',
							 
						 
					
						
							
								
									
										
										
										
											2015-03-16 21:18:03 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-14 03:27:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  // Default value must be in 'Y-m-d' format.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  '#default_value ' => date('Y-m-d', time()),
							 
						 
					
						
							
								
									
										
										
										
											2015-03-16 21:18:03 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2015-06-14 03:27:41 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								~~~
							 
						 
					
						
							
								
									
										
										
										
											2015-06-18 08:58:56 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{% endblock %}