In first lines of media/modules/media_wysiwyg/includes/media_wysiwyg.filter.inc file there are regex for searching media tokens:

define('MEDIA_WYSIWYG_TOKEN_REGEX', '/\[\[.*?\]\]/s');

This regex finds all [[anything]] blocks. But some other modules also an use this type of tokens, so using media will break those tokens from other modules.

Best solution is adding media prefix for this tokens, for prevent all other conflicts, so change this token:
[[{"fid":"24","view_mode":"default","fields":{"format":"default","field_file_image_alt_text[und][0][value]":"","field_file_image_title_text[und][0][value]":""},"type":"media","attributes":{"height":"745","width":"1059","class":"media-element file-default"}}]]
to this:
[[media-wysiwyg:{"fid":"24","view_mode":"default","fields":{"format":"default","field_file_image_alt_text[und][0][value]":"","field_file_image_title_text[und][0][value]":""},"type":"media","attributes":{"height":"745","width":"1059","class":"media-element file-default"}}]]

But this must be changed on all other modules, that use this token, too.

For easily fix this problem, we can extend regexp to find only file tokens, starting with {fid text - like this:

define('MEDIA_WYSIWYG_TOKEN_REGEX', '/\[\[\{"fid".*?\]\]/s');

I attach patch with this fixes.

---
P.S. Also in function _media_wysiwyg_generate_tagMap($text) there are duplicate of regexp text:

  preg_match_all("/\[\[.*?\]\]/s", $text, $matches, PREG_SET_ORDER);

This must be replaced to:

  preg_match_all(MEDIA_WYSIWYG_TOKEN_REGEX, $text, $matches, PREG_SET_ORDER);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Murz created an issue. See original summary.

Murz’s picture

Title: MEDIA_WYSIWYG_TOKEN_REGEX conflicts with other tokens, using double brackets [[token]] » MEDIA_WYSIWYG_TOKEN_REGEX in Media Wysiwyg module conflicts with other tokens, using double square brackets [[token]]
Murz’s picture

Also it conflicts not only with specific module, tokens, but with all plain texts using double square brackets, also in html tag arguments, for example:

1. <div class="tablechart" data-height="200" data-legend="top" data-tabledata="[[&quot;Search&quot;,&quot;Visits&quot;],[&quot;Yandex&quot;,47],[&quot;Google&quot;,29]]" data-title="" data-type="PieChart" data-vaxis="true">&nbsp;</div>

2. <p>Here is [[problem demonstration]] for media wysiwyg module.</p>

joseph.olstad’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, media-wisywyg-fix-token-1.patch, failed testing.

joseph.olstad’s picture

Status: Needs work » Needs review
FileSize
1.09 KB

reroll of original patch by @Murz

jmdorian’s picture

#6 works for me, thanks!

  • joseph.olstad committed db5b708 on 7.x-2.x
    Issue #2825653 by Murz, joseph.olstad: MEDIA_WYSIWYG_TOKEN_REGEX in...
joseph.olstad’s picture

Status: Needs review » Fixed

looks good, its committed to dev. If no complaints then it'll make it into 7.x-2.0-beta6 likely in a few weeks

sylus’s picture

I have traced an issue to this commit:

http://cgit.drupalcode.org/media/commit/?id=db5b708c50a43d2986d85037ebf8...

Immediately before this commit doing a fresh install of a distro all media images in wyswyg were working, then when leveraging this commit, some images but not all are no longer working. Seems like it is related to this issue:

https://www.drupal.org/node/2828665

sylus’s picture

joseph.olstad’s picture

Status: Fixed » Needs work

  • joseph.olstad committed 99e99c1 on 7.x-2.x authored by sylus
    Issue #2828665 by sylus, adriancotter: Regression introduced in 2.0-...
Murz’s picture

Seems that sometimes fid escaped with ' instead of ", so I extend regex for matching both variants.

Murz’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 14: media-wysiwyg-fix-token-7.patch, failed testing.

The last submitted patch, 14: media-wysiwyg-fix-token-7.patch, failed testing.

junaidpv’s picture

"fid" does not come always as first property in media tag. That how I see many media tags in one of our production sites. So, patch from #14 is not enough.

Murz’s picture

@junaidpv, can you provide some examples in which cases media tags not starting with "fid"?
I review php code in all Media module and can't find places in which it can add tokens, starting not from "fid" text.

IMHO, best solution will be add "media" prefix for all media tokens - what do you think about this way?

Murz’s picture

I have found another much easier way to fix this - replace token regex from:

define('MEDIA_WYSIWYG_TOKEN_REGEX', '/\[\[.*?\]\]/s');

to:

define('MEDIA_WYSIWYG_TOKEN_REGEX', '/\[\[.+"type":"media".+\]\]/s');

This is prevent media module for touching other tokens without media types. Same way works in media/modules/media_wysiwyg/js/media_wysiwyg.filter.js js file:

          if (match.indexOf('"type":"media"') == -1) {

So think this is true way.
Patch is included.

Murz’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 20: media_2825653_fix-token-filter-8.patch, failed testing.

Murz’s picture

Status: Needs work » Needs review
FileSize
557 bytes

Updated regexp for multiple Media tags

joseph.olstad’s picture

So if I'm understanding this correctly,

patch #23 is to deal with scenarios where sometimes fid escaped with ' instead of "

However, I have not yet noticed this myself.

@Murz, what is the test scenario? How can you reproduce this scenario? I am not sure how to reproduce or test your patch.

Murz’s picture

Test scenario is very simple: try to add any text with double square brackets in text, for example:

This is text with [[custom-token:test-1-2-3]] token from any other module or custom code, or this is specific text with [[double brackets]] in node body

Media module remove all items with double square brackets. My patch will fix this and Media module will touch only tokens that contains "type":"media" text.

joseph.olstad’s picture

any volunteer reviewers?

  • joseph.olstad committed 99e99c1 on 7.x-3.x authored by sylus
    Issue #2828665 by sylus, adriancotter: Regression introduced in 2.0-...
  • joseph.olstad committed db5b708 on 7.x-3.x
    Issue #2825653 by Murz, joseph.olstad: MEDIA_WYSIWYG_TOKEN_REGEX in...

  • joseph.olstad committed 51c51bc on 7.x-3.x authored by Murz
    Issue #2825653 by Murz, joseph.olstad: MEDIA_WYSIWYG_TOKEN_REGEX in...
joseph.olstad’s picture

anyone interested in trying the latest enhancement by @murz , you can download a copy of 7.x-3.x-dev , once it gets some mileage, then it'll get merged or cherry picked back into 7.x-2.x. Going to try to be extra vigilent about what makes it into the 7.x-2.x branch.

joseph.olstad’s picture

This (media_2825653_fix-token-filter-9.patch) is included with the 7.x-3.x-dev release , if you like it after review, the above fix can be merged into the 7.x-2.x branch and scheduled for the next release.

joseph.olstad’s picture

Assigned: Unassigned » joseph.olstad
Issue tags: +minor version target

Fixed in 7.x-3.x ,
minor version target for (needs merge to) 7.x-2.x

  • joseph.olstad committed 51c51bc on 7.x-2.x authored by Murz
    Issue #2825653 by Murz, joseph.olstad: MEDIA_WYSIWYG_TOKEN_REGEX in...
joseph.olstad’s picture

Status: Needs review » Fixed

This was just merged into 7.x-2.x and is included in release 7.x-2.6

joseph.olstad’s picture

Issue tags: -minor version target

Status: Fixed » Closed (fixed)

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