PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: Add date pop-up calendar in custom drupal 7 form

21 Oct 2016

Add date pop-up calendar in custom drupal 7 form

     This blog describes how to add date pop-up calender to a custom form in the Drupal 7.

Use date pop-up calendar in custom form - drupal 7


     The use case is if you want to use date pop-up calendar in a custom form, then how you can do it in the drupal 7. Actually, the drupal 7 form API provides lots of form types like textfield, checkbox, checkboxes etc to create a custom form. Similarly, the date module  also provides the form type called date_popup. We can use it in the custom form in order to display the date pop-up in the custom form.

Use date pop-up calendar with the custom form in drupal 7:


   Let consider the below code snippet:

 
function phponwebsites_menu() {
  $items = array();

  $items['customform'] = array(
    'title' => t('Custom Form'),
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('phponwebsites_display_date_popup_form'),
    'access callback' => TRUE,
  );

  return $items;
}

function phponwebsites_display_date_popup_form($form, &$form_state) {
  $form['date'] = array(
    '#type' => 'date_popup',
    '#default_value' => date('Y-m-d'),
    '#date_format'   => 'Y-m-d',
    '#date_year_range' => '0:+5',
    '#datepicker_options' => array('minDate' => 0, 'maxDate' => 0),
  );

  return $form;
}

    Where,
      '#date_format'   => 'Y-m-d' if you need to display only date
      '#date_format'   => 'Y-m-d H:i:s' if you need to display date & time
      '#date_year_range' => '0:+5' if you need to display only future 5 years
      '#datepicker_options' => array('minDate' => 0, 'maxDate' => 0) if you want to display only current date. We can hide the future & past dates using this option.

   Please add the above code into your module file and look into the "customform" page. It looks like the below image:

Display only current date in date -pop-up - drupal 7


   Now I've hope you know how to add date pop-up calendar with custom form in the drupal 7.

16 comments:

  1. I enjoyed over read your blog post. Your blog have nice information,
    I got good ideas from this amazing blog.

    ReplyDelete
  2. Your article is easy to understand, the simple way of communicating is accessible to the majority of people. I will keep track of your posts regularly.

    ReplyDelete
  3. Thanks, really helpful.

    ReplyDelete
  4. I have a similar interest this is my page read everything carefully and let me know what you think. Black satta king

    ReplyDelete
  5. Add character to your kitchen design with a wide range of kitchen cabinet doors and drawer fronts.

    ReplyDelete
  6. We are renowned for providing our patients with the most reasonably priced,

    ReplyDelete
  7. Sharing your cell phones can be risky. But with I-Cell Station's trustworthy services,

    ReplyDelete
  8. A blog is a discussion or informational website published on the

    ReplyDelete
  9. Constructing a house in DHA (Defence Housing Authority) Karachi is

    ReplyDelete
  10. Digital Marketing USA, a digital marketing agency in USA,

    ReplyDelete
  11. We deliver big ideas and even bigger results with more agility and efficiency than larger digital agencies.

    ReplyDelete
  12. Hence, people miss out on the most essential part of the sales process.

    ReplyDelete
  13. E-Biz, a leading digital marketing agency, commenced its journey in Ireland as a Printing

    ReplyDelete
  14. We aim to work in partnership with our clients and build a good

    ReplyDelete
  15. Our team has experience bringing in both e-commerce sales as well as service leads.

    ReplyDelete