1

I have just updated to drupal 7.21, and I have massive errors with the new itok suff: Right now I use json_encode(views_get_view_result('viewsitem')), and decode the result with JSON and JavaScript. My slideshow wants to have the normal path but returns things similar to "...sites/default/files/styles/large/public/field/image?itok=M-zTIVI1/mypic.JPG."

Normally, I get the images using the following code.

if (value.field_field_image) {
  var images = value.field_field_image
  // iterating over the images per nid
  for (var i = 0; i < images.length; i++) {
    slideshow_settings.this_slides += '<a class="rsImg" href="'
      + slideshow_settings.basepath + '/' + images[i].raw.filename
      + '"></a>';
  }

  pics = slideshow_settings.this_slides;
} 

How can I fix this?

  • Where is slideshow_settings.basepath coming from? Ideally your view should be building up the URL to the image with API calls. That should take care of the whole itok thing. – mpdonadio Mar 28 '13 at 19:17
  • @MPD hmm not sure how this works...from my module which pushes the url into json the code looks like this: $upload_url=image_style_url('large', 'public://field/image'); – Jurudocs Mar 28 '13 at 21:47
  • So you aren't generating the actual URL of the image style, just using image_style_url() to get the dirname? – mpdonadio Mar 28 '13 at 22:23
  • @MPD sounds weired ;-P...but yes...how would you do it? – Jurudocs Mar 28 '13 at 22:24
4

Your root problem seems to be that you are using a side effect of image_style_url().

When you don't pass in a valid image path, you used to get back the basepath of the image style. Now, with the token patch, it will include the token, and therefore not really be the basepath. From what I can gather, this is why a lot of modules broke.

The proper thing to do is to use the API to create the URL for you on the server side, instead of gluing together parts in JS.

This is something that I used on a recent project:

$images = array();
$styles = image_styles();

if (! empty($node->field_my_image[$node->language])) {
  foreach ($node->field_my_image[$node->language] as $image) {
    $fid = (int) $image['fid'];
    $uri = $image['uri'];
    $url = file_create_url($uri);

    $metadata = array(
      'alt' => empty($image['alt']) ? '' : $image['alt'],
      'width' => (int) $image['width'],
      'height' => (int) $image['height'],
    );

    $metadata['styles'] = array(
      'original' => $url,
    );

    foreach ($styles as $style => $unused) {
      $metadata['styles'][$style] = file_create_url(image_style_url($style, $uri));
    }
  }

  $images[$fid] = $metadata;
}

drupal_add_js(array('mymodule' => array('images' => $images)), 'setting');

This dumps all of my image fields into a JS array with all of their metadata, including all of the URLs for the image styles that I may need. My JS code can then use what it needs from this array without worrying about building paths up.

  • this looks like a good approach! Thanks very much for the inspiration! – Jurudocs Mar 28 '13 at 22:51
1

There is a recent (today) patch in this issue and if the thread is to be believed, itok's days may be numbered as 'too intrusive'. Personally, I have frozen any image-intensive sites at 7.19 until this issue is resolved. You might consider a rollback.

  • hmm if there is no easy solution for this...i must do that grrr – Jurudocs Mar 28 '13 at 18:59
1

As a quick hack you could do something like...

for (var i = 0; i < images.length; i++) {
  var parts = slideshow_settings.basepath.split('?');
  var href = parts[0] + '/' + images[i].raw.filename + '?' + parts[1];

  slideshow_settings.this_slides += '<a class="rsImg" href="' + href + '"></a>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.