Download & Extend

views carousel does not 'work' when embedded

Project:Views carousel
Version:6.x-2.x-dev
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:active

Issue Summary

Hello, I created a views carousel and it worked fine as a block but when I embed it as using views_embed_view or module_invoke - the view does not show up. Instead a HTML list view shows up - I did override the defaults in the view config.

Any help would be greatly appreciated. This is a great module and I plan to use it a lot :-)

Comments

#1

I am also having this problem, using views_embed_view in my tpl file.

#2

I had this issue as well. This may not apply to your cases, but maybe it might help someone.

I was using two carousels on the page, one in the header from a block and one embedded in my node template file. Each carousel specified a custom css skin. However, only one of those css files was getting put in the styles variable, which was making my second carousel seem broken.

So, I just combined the styles into a single css and it worked ok.

So, generally, speaking I don't think it is as issue of the carousel being broken simply if you use views_embed_view.

Thanks,
-Bill

#3

It may not only be the problem of embedding, i have created two blocks with Views, added them to a panel, for the testing purposes added the second block twice. No matter in what order i put them, alway the second one doesn't work.. Will try to use your suggestion brenk28

#4

I am trying to embed a single carousel using views_get_view in my page-front.tpl.php. The carousel works fine in a block, but as an embeded view, the carousel doesn't show up. Upon loading the front page, the carousel shows up in a brief flicker (less than 1 second) and then disappears. You can still see the admin tabs (Edit Export Clone) above where the carousel would be.

Any and all help is appreciated to get this resolved.

#5

Category:support request» bug report
Priority:normal» critical

confirm problem with views_embed_view
any ideas?

#6

After a lot of Googling, I got this working by somewhat hacking it. Here is what I did.

Put a normal block with the view you want to embed on a page, this will add the needed js and css links in the page header. I did a view page source and made note of those links in the head tags. Next I created (or updated) the preprocess_page function in my theme's template.php and added the required links via drupal_add_css and drupal_add_js.

<?php
function MYTHEMENAME_preprocess_page(&$vars) {
  if (
$vars['is_front']) {
   
drupal_add_css(drupal_get_path('module', 'views') .'/css/views.css');
   
drupal_add_css(drupal_get_path('module', 'jcarousel') .'/jcarousel/lib/jquery.jcarousel.css');
   
drupal_add_css(drupal_get_path('module', 'jcarousel') .'/jcarousel/jcarousel.css');
   
drupal_add_css(drupal_get_path('module', 'jcarousel') .'/jcarousel/skins/tango/skin.css');
   
   
drupal_add_js(drupal_get_path('module', 'jcarousel') . '/jcarousel/lib/jquery.jcarousel.js');
   
drupal_add_js(drupal_get_path('module', 'jcarousel') . '/jcarousel.js');
  }
 
// Reconstruct CSS and JS variables.
 
$vars['css'] = drupal_add_css();
 
$vars['styles'] = drupal_get_css();
 
$vars['scripts'] = drupal_get_js();
}
?>

Next I snagged the jquery.extend link from the page source and tweaked the view name ("#viewscarousel-FrontCarousel-default") to match the one I was calling with my views_embed_view and hard coded this as the last line in the head tag of the tpl file.

<script type="text/javascript">
<!--//--><![CDATA[//><!--
jQuery.extend(Drupal.settings, { "basePath": "/", "jcarousel": { "#viewscarousel-FrontCarousel-default": { "animation": "slow", "auto": 7, "wrap": "last", "skin": "tango" } } });
//--><!]]>
</script>
</head>

Finally I called my embedded view from within in my page tpl (page-front.tpl.php):

<div>
  <?php print views_embed_view('FrontCarousel', $display_id ='default'); ?>
</div>

Now the carousel is working. Apparently the issue is related to the processing of $scripts and $style in the template and the embedded view not firing something off to include the necessary JS and CSS links in the header.

It's not pretty but it is valid a workaround.

#7

I would be willing to help sponsor this if we could get it fixed without hacking everything...any takers?

#8

There is a simple way to make this work. Instead of using PHP to embed the view, create a new region for the carousel and display the block for the carousel view to that region. After many failed attempts to get the carousel view to display using PHP, this worked like a charm for me.

#9

Ah, I see this is the same bug I ran into myself earlier.

Basically, when the carousel is being embedded via views_embed_view(), this causes the call to display the carousel to be delayed until the views_embed_view call, which means that it's quite possible that the code which adds the js and css files will be invoked _after_ the theme calls drupal_get_js and drupal_get_css to get the html to embed the files into the page. As such - no carousel.

I fixed it by adding code to my theme's page preprocess that adds the javascript and css (copied form the viewcarousel's template function) and then specifically called something like:

<?php
$variables
['scripts'] = drupal_get_js();
$variables['styles'] = drupal_get_css();
?>
(at the end of my preprocess_page in my theme)

It's a tricky problem. On the one hand, putting it in the block/display code would prevent this error, but would mean that if anyone turned on block caching the carousel wouldn't work, as block caching breaks included javascript. By putting this into the theme of the block, I think he's side-stepping that.

#10

I tried numerous methods for embedding views that haven't quite worked for placing multiple views carousel blocks in a page so I wanted to share my code method which has worked for me. I've stretched vars out to make things a little more clear. If you need any further insight take a look at the views_page function itself, http://drupalcontrib.org/api/function/views_page/6

In my example below my view name is "my_carousel" my block id is "block_1" and I'm passing term ids as an argument to the view.

$view_name = 'my_carousel';
$view_block_id = 'block_1';
$view_arg = '1+2+3';
$block = (object) views_page($view_name, $view_block_id, $view_arg);
print theme('block', $block);

#11

chales, this code works for you? I tried it (and the other suggestions), but I still get an HTML list.

I tried jpstuddard's approach, which makes sense in theory, but hasn't worked for me yet.

#12

The code posted is what I use on a couple of projects I'm working on. I'm calling that block of code in the appropriate TPL file where I need the blocks output.

Be sure your view is indeed setup correctly with the style of your block set to Views Carousel. Next if you are using a custom theme switch over to Garland or another base theme to try it.

#13

Hi,
I tried your method, but it still not work for front-page.tpl.php. It works for page.tpl.php.
Could you share what you have done to make it working?

How do I call block at front-page-tpl.php? is there a way to add the command directly in the .php file?

thanks

#14

@radiofranky2009 did you mean page-front.tpl.php?

I can't think of why it might not work for you. Can you show me your code?