I am unsure whether this is a deeper issue with the AJAX system or if it is only specific to machine_name elements. When a machine_name form element is used in a form that makes AJAX requests, the following JavaScript error occurs:

Uncaught TypeError: Cannot read property 'length' of undefined
    at Function.each (jquery.js?v=1.4.4:33)
    at Object.attach (machine-name.js?oko14n:29)
    at Object.<anonymous> (drupal.js?oko14n:76)
    at Function.each (jquery.js?v=1.4.4:33)
    at Object.Drupal.attachBehaviors (drupal.js?oko14n:74)
    at Object.insert (ajax.js?v=7.53:570)
    at Drupal.ajax.success (ajax.js?v=7.53:428)
    at Object.success (ajax.js?v=7.53:189)
    at Object.a.success (jquery.form.js?v=2.52:12)
    at s (jquery.form.js?v=2.52:12)

This error can be seen by placing a machine_name element and a file_managed element in the same form (see attached "mn_example" module which provides such a form). When either the "Upload" or "Remove" button of the file_managed element is clicked, an AJAX request is triggered. The error occurs when the AJAX response is processed by ajax.js.

Specifically, line 29 of machine-name.js is problematic:

 attach: function (context, settings) {
    var self = this;
    $.each(settings.machineName, function (source_id, options) {
      var $source = $(source_id, context).addClass('machine-name-source');
      var $target = $(options.target, context).addClass('machine-name-target');

The problem is that `settings.machineName` is undefined. The `machineName` setting should be added by the form_process_machine_name() function. However, it appears that during AJAX processing of the form, the `#attached` JS setting is not processed correctly.

CommentFileSizeAuthor
mn_example.tar_.gz756 bytesjtsnow
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jtsnow created an issue. See original summary.

djdevin’s picture

I confirmed this behavior, I also think this is the same as #2868929: machine name javascript is not robust in AJAX scenarios

djdevin’s picture

Status: Active » Closed (duplicate)
djdevin’s picture