web-dev-qa-db-fra.com

Format de champ de date du formulaire Web

Comment changer le format de date d'un champ de date dans WEBFORM?

J'ai activé le calendrier contextuel pour ce champ, et lorsque je sélectionne une date dans le calendrier, le format par défaut est en Y-m-d que je veux changer en d-m-Y.

Je ne vois aucune configuration disponible dans la page d'édition du champ de date.

Sous configuration -> Date et heure -> Le type de date courte a le format "d M Y", j'ai essayé de le mettre à jour en d-m-Y mais sans verrou!

Aidez-moi! Captures d'écran jointes:

enter image description hereenter image description hereenter image description hereenter image description here

1
Maddy

J'ai pu trouver le fichier webform-calendar.tpl.php que vous avez utilisé Drupal 7 - Changer la date du formulaire web en textfield avec calendrier popup .

Fondamentalement, il ajoute un champ de texte et masque le champ de date déroulante.

J'ai compris que la sortie du champ de date est contrôlée dans webform.js

Vous devrez changer de

  // Set up the jQuery datepicker element.
  $calendar.datepicker({
    dateFormat: 'yy-mm-dd',
    yearRange: startYear + ':' + endYear,
    firstDay: parseInt(firstDay),
    minDate: startDate,
    maxDate: endDate,
    onSelect: function (dateText, inst) {
      var date = dateText.split('-');
      $webformDatepicker.find('select.year, input.year').val(+date[0]).trigger('change');
      $webformDatepicker.find('select.month').val(+date[1]).trigger('change');
      $webformDatepicker.find('select.day').val(+date[2]).trigger('change');
    },
    beforeShow: function (input, inst) {
      // Get the select list values.
      var year = $webformDatepicker.find('select.year, input.year').val();
      var month = $webformDatepicker.find('select.month').val();
      var day = $webformDatepicker.find('select.day').val();

      // If empty, default to the current year/month/day in the popup.
      var today = new Date();
      year = year ? year : today.getFullYear();
      month = month ? month : today.getMonth() + 1;
      day = day ? day : today.getDate();

      // Make sure that the default year fits in the available options.
      year = (year < startYear || year > endYear) ? startYear : year;

      // jQuery UI Datepicker will read the input field and base its date
      // off of that, even though in our case the input field is a button.
      $(input).val(year + '-' + month + '-' + day);
    }
  });

à

  // Set up the jQuery datepicker element.
  $calendar.datepicker({
    dateFormat: 'dd-mm-yy', // changed
    yearRange: startYear + ':' + endYear,
    firstDay: parseInt(firstDay),
    minDate: startDate,
    maxDate: endDate,
    onSelect: function (dateText, inst) {
      var date = dateText.split('-');
      $webformDatepicker.find('select.year, input.year').val(+date[2]).trigger('change'); // changed
      $webformDatepicker.find('select.month').val(+date[1]).trigger('change');
      $webformDatepicker.find('select.day').val(+date[0]).trigger('change'); // changed
    },
    beforeShow: function (input, inst) {
          // Get the select list values.
          var year = $webformDatepicker.find('select.year, input.year').val();
          var month = $webformDatepicker.find('select.month').val();
          var day = $webformDatepicker.find('select.day').val();

          // If empty, default to the current year/month/day in the popup.
          var today = new Date();
          year = year ? year : today.getFullYear();
          month = month ? month : today.getMonth() + 1;
          day = day ? day : today.getDate();

          // Make sure that the default year fits in the available options.
          year = (year < startYear || year > endYear) ? startYear : year;

          // jQuery UI Datepicker will read the input field and base its date
          // off of that, even though in our case the input field is a button.
          $(input).val(day + '-' + month + '-' + year); // changed
        }
      });

4 lignes modifiées, je les ai marquées avec // changed

Enregistrez le nouveau fichier dans /sites/themes/YOURTHEME/js/webform.js

Vous pouvez utiliser hook_js_alter pour remplacer le fichier JS.

/*
 * Implements hook_js_alter
 */
function YOURTHEME_js_alter(&$javascript) {
  if (!empty($javascript['sites/all/modules/webform/js/webform.js'])) {
    $javascript['sites/all/modules/webform/js/webform.js']['data'] = drupal_get_path('theme', 'YOURTHEME') . '/js/webform.js';
  }
}
3
No Sssweat

Nous pouvons changer en utilisant le crochet MYTHEME_webform_date ($ variables) sous l'URL de référence est là. Vous devez écrire ce code dans template.php

https://jamesdavidson.io/blog/how-reorder-date-format-webform-date-picker

0
sivani