PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: October 2016

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.

7 Oct 2016

Disable future dates in date popup - Drupal 7

     This blog describes how to disable future dates in the Drupal 7. One of the features in the date  module is displayed the date in the pop-up.

Disable future dates in the date pop up - drupal 7


The use case is if you want to display only past & current date rather than all the dates in the pop-up, then how to do it in Drupal 7. Actually, the date module provides API called hook_date_popup_process_alter to alter the date_popup widget elements.

Example for disabling future dates in Drupal 7:


   For instance, I am going to disable future dates in the article content type. Please consider the following code snippet.

/**
 * Implement hook_date_popup_process_alter().
 */
function phponwebsites_date_popup_process_alter(&$element, &$form_state, &$context) {

  if ($form_state['complete form']['#form_id'] == 'article_node_form' && $element['#field']['field_name'] == 'field_date') {
    $max = 0;
  }

  if (isset($element['#datepicker_options']['maxDate'])) {
    $max = $element['#datepicker_options']['maxDate'];
  }

  if (isset($max)) {
    $element['#datepicker_options'] = array(
      'maxDate' => "+$max D",
    );
  }
  $element['date'] = date_popup_process_date_part($element);
}

   I've disabled the dates only if the form is article & the field name is field_date. After added the above code to your module, you could see disabled future dates in the date pop up. It looks like the below image:


Disable future dates in the date pop-up in drupal 7


   Now I've hope you know how to disable the future dates at the date module in Drupal 7.