Multiple instances of the same rotator view on one page ... does not work.

manimejia - December 13, 2008 - 04:43
Project:Views Rotator
Version:6.x-1.0-alpha2
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs review
Issue tags:multiple instances
Description

Hello.
I was about to TRY to write my own view style to impliment Jquery Cycle, just a syou did. I'm sure glad you did. Looks great, but took me a bit to get it working....

YOU SEE...
Using Views 2, and D6, I have a view set up to display on node teasers (via views_embed_view($view_id, $display_id, $args) in my node.tpl.php) and it takes in one argument for the node title. Well, it ends up, the same view with different arguments is displayed on one page. Your function template_preprocess_views_view_rotator(&$vars) is not set up to differentiate between these view instances, and does not give a unique ID attribute to each one. Hence the javascript is broken.

SO ...
I took the liberty to add another variable to the $vars['views_rotator_id'] string. I added the view's first argument to the end of this string. I dunno if this is a good permanent solution ... but that's what I did to make this work for me.

I'm not going to submit a patch yet, but here's what I ended up changing (two lines with "MANI") :

<?php
function template_preprocess_views_view_rotator(&$vars) {
 
drupal_add_css(drupal_get_path('module', 'views_rotator') .'/views-rotator.css');

 
$view = $vars['view'];
 
$options = $view->style_plugin->options;
  
// MANI -- clean up the view's first argument for use in an ID attribute
  
$arg_id = strtolower(str_replace(array('][', '_', ' '), '-', $view->args[0]));
  
// MANI -- add the new variable to the end of our ID attribute, to make it more unique
  
$vars['views_rotator_id'] = 'views-rotator-'. $view->name .'-'. $view->current_display . '-' . $arg_id;

 
drupal_add_js(drupal_get_path('module', 'views_rotator') .'/views-rotator.js');

 
$view_settings['fx'] = 'fade';
 
$view_settings['timeout'] = check_plain($options['timeout']) * 1000;
 
$view_settings['speed'] = check_plain($options['speed']) * 1000;
 
$view_settings['pause'] = check_plain($options['pause']);
 
$view_settings['cleartype'] = 1;
 
  if (!empty(
$options['back_next_buttons'])) {
   
$view_settings['next'] = '#'. $vars['views_rotator_id'] .'-views-rotator-next';
   
$view_settings['prev'] = '#'. $vars['views_rotator_id'] .'-views-rotator-prev';
  }

  if (empty(
$options['height'])) {
   
$view_settings['height'] = 'auto';
   
$view_settings['auto_height'] = 1;
  }
 
 
drupal_add_js(array('views_rotator' => array($vars['views_rotator_id'] => $view_settings)), 'setting');
}
?>

Thanks for making this module. It's JUST what the doctor ordered.

M.M.

#1

mfer - December 16, 2008 - 02:24

Interesting use case. You're solution it's a good fix because most people won't use an argument for this.

Any other ideas on solving this well? In a way that provides a consistent id for themers to use?

#2

manimejia - January 11, 2009 - 19:44

I actually think THIS would be a better implementation of my fix ...
(adds ALL arguments to the ID, but if none adds nothing)
add to views_rotator.module at line 29.

<?php
 
// clean up all the view's arguments for use in an ID attribute
   
$arg_id = '';
  if (
$view->args[0]) {
    foreach (
$view->args as $arg){
   
$arg_id .= strtolower(str_replace(array('][', '_', ' '), '-', $view->args[0]));
    }
  }
   
$arg_id = $arg_id ? '-'.$arg_id : '';
 
// add the new variable to the end of our ID attribute, to make it more unique
 
$vars['views_rotator_id'] = 'views-rotator-'. $view->name .'-'. $view->current_display . $arg_id;
?>

#3

mfer - January 14, 2009 - 19:52

A more appropriate solution would be to add a counter. It shouldn't be based on arguments. If you have the exact same view on a page more than once the problem would still creep in. Yet, when it comes to theming there would still be a problem because the counter would change.

Also, the code above won't work.

Do you know how other situations like this operate?

#4

q0rban - February 20, 2009 - 19:49

Why not just use classes instead of using an ID, as views does everywhere else?

#5

q0rban - February 20, 2009 - 19:59
Status:active» needs review

patch implements #4

AttachmentSize
views_rotator_class.patch 887 bytes

#6

q0rban - February 20, 2009 - 20:15

whoops, js needs to be patched as well... This patch is only for the js file, you'll still need the above patch.

AttachmentSize
views_rotator_class_js.patch 480 bytes
 
 

Drupal is a registered trademark of Dries Buytaert.