Problems

  • Loops over settings.dateTime but sets settings in the loop, so the the loop can run at most one time. (This is not a core bug, though, because there is no page where multiple dateTime widgets are used, but this would make it reusable in contrib.)
  • Has no hasOwnProperty() check.
  • The key-up callback uses settings. This should be in a closure, so that one of multiple widgets would use its own settings.

The referenced code

(Yeah, it's not PHP, but the syntax highlighting isn't too bad.)

/**
 * Show/hide custom format sections on the regional settings page.
 */
Drupal.behaviors.dateTime = {
  attach: function (context, settings) {
    for (var value in settings.dateTime) {
      var settings = settings.dateTime[value];
      var source = '#edit-' + value;
      var suffix = source + '-suffix';

      // Attach keyup handler to custom format inputs.
      $('input' + source, context).once('date-time').keyup(function () {
        var input = $(this);
        var url = settings.lookup + (settings.lookup.match(/\?q=/) ? '&format=' : '?format=') + encodeURIComponent(input.val());
        $.getJSON(url, function (data) {
          $(suffix).empty().append(' ' + settings.text + ': <em>' + data + '</em>');
        });
      });
    }
  }
};

Comments

Niklas Fiekas’s picture

Status: Active » Needs review
StatusFileSize
new1.7 KB

First try.

nod_’s picture

Category: task » bug

I'd say a loop not looping is a bug.
This looks good, I'll try it when I have some time later today.

nod_’s picture

Status: Needs review » Reviewed & tested by the community

Works, to test duplicate the date field in the admin/config/regional/date-time/formats/add page. Does not work before the patch, works after the patch.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x and 8.x. Thanks!

xjm’s picture

Status: Fixed » Reviewed & tested by the community

This commit appears in 7.x, but not in 8.x.

xjm’s picture

Status: Reviewed & tested by the community » Fixed

Alright, it's fixed now. :)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Explain syntax highlighting.