Hi Guys!

I was having a really bad experience with Media module, specifically with media.filter.js. I have multiple WYSIWYG editors (CKEditor in my case) on the form with ability to add more. I don't think this is a case, but WYSIWYGs were inside nested Field Collections. Anyway, when I add new WYSIWYG item to the form with the help of AJAX, it results with this nice JS error (in chrome): Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist.

I'm using:
Media 7.x-2.x
WYSYWYG 7.x-2.x
CKEditor 4.1.0

Because Drupal extends Drupal.settings object on every AJAX response media tags inside Drupal.settings.tagmap become arrays instead of string. This was resulting error inside Drupal.media.filter.create_element. I've made a small workaround for Drupal.media.filter.ensure_tagmap to ensure that media tags inside will always be string (no array).

Also, I had another issue, when after some AJAX requests tagmap was empty. I had not time to debug this issue and made a workaround with a local tagmap (tagmap backup), which I assign to Drupal.settings.tagmap in case it is empty. Probably this issue happens because Field Collection, very hard to say. Anyway, workaround works.

I'm posting a patch here (in comments). I do not think this patch will get into the media dev version. I hope guys that develop media module will get around with this issue in more elegant way.
For those, who do not like to hack module, you may insert this code inside your custom JS:

(function($) {
    $(document).ready(function() {
        if(Drupal.media && Drupal.media.filter) {
            tagmap_local = {};
            $.extend(Drupal.media.filter, {
                ensure_tagmap: function() {
                    Drupal.settings.tagmap = Drupal.settings.tagmap || {};

                    if ($.isEmptyObject(Drupal.settings.tagmap) && !$.isEmptyObject(tagmap_local)) {
                        Drupal.settings.tagmap = tagmap_local;
                    }

                    tagmap_local = Drupal.settings.tagmap;

                    for (var i in Drupal.settings.tagmap) {
                        if (!Drupal.settings.tagmap[i]) {
                            continue;
                        }
                        Drupal.settings.tagmap[i] = Object.prototype.toString.call(Drupal.settings.tagmap[i]) === "[object Array]" ? Drupal.settings.tagmap[i].shift() : Drupal.settings.tagmap[i];
                    }

                    return Drupal.settings.tagmap;
                }
            });            
        }
    });
})(jQuery);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nostop8’s picture

Issue summary: View changes

spelling

nostop8’s picture

Issue summary: View changes

code extend for dummys

nostop8’s picture

nostop8’s picture

Issue summary: View changes

formatting fix

bneil’s picture

DamienMcKenna’s picture

Status: Active » Needs work

At the very least the patch needs to be updated to match the Drupal coding standards.

DamienMcKenna’s picture

FYI this problem no longer exists with CKEditor 4.2.2 and the combination of patches described in https://drupal.org/node/2067063#comment-8009717.

nostop8’s picture

I'm afraid the problem I've described above is a little bit different from the issues described here: https://drupal.org/node/2067063#comment-8009717.

I'm talking about the situation, when you have unlimited field of Long Text type on the form with WYSIWYG turned on. So, when you click on 'Add more' button, the images (media tags) inside previous fields disappears and we see JavaScript error: "Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist."
Media's tagmap variable has nothing to CKEditor. CKEditor is triggered a bit later.

nostop8’s picture

Issue summary: View changes

code improvement

Vultures’s picture

Component: WYSIWYG integration » Code
FileSize
1.2 KB

I had a similar issue with a multivalue field.
Using Wysiwyg and CKeditor in a multi-value field, clicking on "Add new value" results with a JS error caused by the issue described above.
In tagmap got arrays instead of strings as value, and this breaks the media logic.

This patch fixed the issue for me.

Chris Matthews’s picture

Status: Needs work » Closed (outdated)

Closing this issue as outdated. However, if you think this issue is still important, please let us know and we will gladly re-open it for review.
sincerely,
- the Drupal Media Team