Media Filter doesn't correctly handle multiple images.

Comments

sylus’s picture

sylus’s picture

Title: When two images are saved in a node edit screen using UID images, the second image is replace by first. » Media Filter doesn't correctly handle multiple images.
Issue summary: View changes
Status: Active » Needs work

First Commit will need to bring Media to latest dev and Update a patch we are using.

http://drupalcode.org/project/wetkit_widgets.git/commit/21957bd

sylus’s picture

I can get this fixed in PHP 5.3 using the following syntax. Problem is they won't take this patch as only works in PHP 5.3 using anonymous closures.

function media_wysiwyg_filter($text, $filter, $format, $langcode, $cache, $cache_id) {
  $matches = array();
  $replacements = array();
  $patterns = array();
  preg_match_all(MEDIA_WYSIWYG_TOKEN_REGEX, $text, $matches);

  if (!empty($matches[0])) {
    foreach ($matches[0] as $match) {
      $replacements[] = media_wysiwyg_token_to_markup(array($match), FALSE, $langcode);
    }
  }

  $rendered_text = preg_replace_callback(MEDIA_WYSIWYG_TOKEN_REGEX, function($matches) use (&$replacements) {
      return array_shift($replacements);
  }, $text);

  return $rendered_text;
}

This needs to be rewritten:

  $rendered_text = preg_replace_callback(MEDIA_WYSIWYG_TOKEN_REGEX, function($matches) use (&$replacements) {
      return array_shift($replacements);
  }, $text);

  return $rendered_text;
}
spotzero’s picture

Since it looks like you want to replace each media token, one at a time, could you do it directly each the replacement string is calculated?

I think that code would look something like this:

<?php
function media_wysiwyg_filter($text, $filter, $format, $langcode, $cache, $cache_id) {
  $matches = array();
  $replacements = array();
  $patterns = array();
  $rendered_text = $text;
  preg_match_all(MEDIA_WYSIWYG_TOKEN_REGEX, $text, $matches);
  if (!empty($matches[0])) {
    foreach ($matches[0] as $match) {
      $replacement = media_wysiwyg_token_to_markup(array($match), FALSE, $langcode);
      $rendered_text = str_replace($match, $replacement, $rendered_text, 1);
    }
  }
  return $rendered_text;
}
sylus’s picture

I don't think str_replace will work because $match is a regular expression:

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

We would have to swap it with preg_replace but then would need the /e modifier as well.

spotzero’s picture

Isn't $match the matched media string? I.E.: "[[{some:json}]]"

I though MEDIA_WYSIWYG_TOKEN_REGEX was the regex.

sylus’s picture

Your right sorry brain dead today :)

sylus’s picture

@spotzero++ seems to work for all my testing.

Code is added to issue here: https://drupal.org/node/2129273

sylus’s picture

Status: Needs work » Fixed

Am happy to say committed:

http://drupalcode.org/project/wetkit_widgets.git/commit/05f03ff

This will solve the duplicated images fiasco once and for all.

Thanks Gordon for catching this one!

Status: Fixed » Closed (fixed)

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