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);
Comment | File | Size | Author |
---|---|---|---|
#6 | media-tagmap_variable_fix-2116639-6.patch | 1.2 KB | Vultures |
Comments
Comment #0.0
nostop8 CreditAttribution: nostop8 commentedspelling
Comment #0.1
nostop8 CreditAttribution: nostop8 commentedcode extend for dummys
Comment #1
nostop8 CreditAttribution: nostop8 commentedComment #1.0
nostop8 CreditAttribution: nostop8 commentedformatting fix
Comment #2
bneil CreditAttribution: bneil commentedSounds like this is related to #2102623: Multiple WYSIWYG editors on one form breaks Media integration.
Comment #3
DamienMcKennaAt the very least the patch needs to be updated to match the Drupal coding standards.
Comment #4
DamienMcKennaFYI this problem no longer exists with CKEditor 4.2.2 and the combination of patches described in https://drupal.org/node/2067063#comment-8009717.
Comment #5
nostop8 CreditAttribution: nostop8 commentedI'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.
Comment #5.0
nostop8 CreditAttribution: nostop8 commentedcode improvement
Comment #6
Vultures CreditAttribution: Vultures commentedI 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.
Comment #7
Chris Matthews CreditAttribution: Chris Matthews as a volunteer commentedClosing 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