I'm trying to limit de date range that datapicker popup allows to select on a date field. I'have a custom module where I drive all my modifications and "hook_alters". I call a javascript file on a hook_init with many modifications here and there and all perform as expected.

Drupal.behaviors.datepickerMaxDate = {
  attach: function (context, settings) {
    var form = $('form#nodetype-node-form');

    form.find('#edit-field-date-oferta-und-0-value-datepicker-popup-0')
      .datepicker({ minDate: 0, maxDate: "+7D" });
  } 
};

When I place that function on the module js (added on hook_init via drupal_add_js) it breaks many module JS as fivestars or jcarousel. But when I place exactly the same function on my theme's js file it works and the other modules javascript also works as expected.

I'm not very proficient with JS and learning very fast, may someone advise me why it works on my theme but not on my module??

Thanks :-)))

Comments

jorditr’s picture

Status: Active » Closed (fixed)

I finally found that even that solution collided on other node types with the wysiwyg editor. I realized that it was colliding on all content types forms except on the ones that had the date widget that I was trying to modify and I understood that I had to verify that the field existed. Thanks to that now it works and I've been able to return that JS function to the module js file (not in theme's template file).

For documenting that issue for others that could find the same trouble, the code is:

Drupal.behaviors.datepickerMaxDate = {
  attach: function (context, settings) {
    var dateFieldofChoice = form.find('#edit-field-date-oferta-und-0-value-datepicker-popup-0');

    if (dateFieldofChoice.length) {
      dateFieldofChoice.datepicker({ minDate: 0, maxDate: "+7D" });
    } 
  }
};

I'm not expert enough to know if it's a good solution or not. Is someone with a better understing wants to suggest something new feel free to writte it down!

Thanks to the maintainers of such an impressive module :-)

nightlife2008’s picture

I solved this problem using the HOOK_date_popup_process_alter(), which is run AFTER the element has been processed, to add the #datepicker_options array.

After adding the options, I just reprocess the element with the proper options set.

function MODULE_date_popup_process_alter(&$element, &$form_state, &$context) {
  if (isset($element['#field']['field_name'])) {
    switch ($element['#field']['field_name']) {
      case 'field_fact_periode': {
        $element['#datepicker_options'] = array(
          'minDate' => "+3D",
        );
        $element['date'] = date_popup_process_date_part($element);
      }
      break;
    }
  }
}

Works like a charm!

fox mulder’s picture

Issue summary: View changes

nightlife2008: Thanks for your help!

berramou’s picture

how can i add minDate in HOOK_date_select_process_alter()

function HOOK_date_select_process_alter(&$element, &$form_state, $context) {
  if (isset($element['#field']['field_name'])) {
    switch ($element['#field']['field_name']) {
      case 'field_date_end_registrat': {
        // ???
      }
        break;
    }
  }
}

thanks .

nishat ahmad’s picture

function MODULE_NAME_date_popup_process_alter(&$element, &$form_state, &$context) {
  if(isset($element['#field']['field_name'])) {
    switch ($element['#field']['field_name']) {
      case 'field_start_date': {
        $element['#datepicker_options'] = array(
          'minDate' => "+0D",
        );
        $element['date'] = date_popup_process_date_part($element);
      }
      break;
    }
  }
}

or

function MODULE_NAME_date_popup_process_alter(&$element, &$form_state, &$context) {

  $element['#datepicker_options'] = array('minDate' => "+0D");
  $element['date'] = date_popup_process_date_part($element);  

}


put it into the custom module and after that clear the cached