Node aware start index
lemartialou - November 10, 2009 - 22:02
| Project: | Views carousel |
| Version: | 6.x-2.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | lemartialou |
| Status: | needs review |
Jump to:
Description
Hi there!
This is my first code contrib to the free software world and my first patch ever (: proud and shy :)
When this option is activated, if one of the items corresponds to the currently viewed node, this item will be used as the start position of the carousel and defined as the jcarousel-item-0.
Don't hesitate to correct my code or my english if necessary, i'm learning here, and there are others little options i would like to add...
Cheers!
| Attachment | Size |
|---|---|
| viewscarousel.patch | 3.23 KB |
| viewscarousel_nodeaware.zip | 4.13 KB |

#1
Forgot to give the 'need review' status :p
#2
Here is an example of how i use this functionality:
I have a node type: Video
All the video nodes are classified with terms in a specific vocabulary.
What I want is a viewscarousel under my video node showing all the other videos of the same term.
No problem, i create my views block, filter it by 'Node:Published Yes' and 'Node:Type Video'.
Then i tell the view to show my thumbnail cck imagefield using imagecache with a carousel_preset, and tell it to link the image to the corresponding video node.
Then I set the style to 'Views Carousel' and configure it to get the magic.
At this point i've got a carousel in a block with all the videos thumbnails linking to their respective nodes.
Now I want the carousel to appear only on the video node page, and i want it to know which term is associated with this node, and i want it to show only the video nodes of the same term.
I found 2 methods to do this:
METHOD 1:
I set a 'Taxonomy:term id' argument with basic validation. I don't provide a default argument.
Then I take my node-video.tpl and inject this little snippet where i want to place the carousel:
<?phpif (!$teaser):
foreach ( (array)$node->taxonomy as $term ) {
print '<h3 class="video-carousel-title">Category: ' . $term->name . ' :</h3>';
print views_embed_view('video_carousel_view', $display_id = 'block_1', $term->tid);
}
endif;
?>
This works well for me, but there could be problems if the node is associated with several terms, I don't know.
In case, I found
METHOD 2:
I set a 'Taxonomy:term id' argument with basic validation.
Now i provide a default argument: PHP Code.
<?php$node=node_load(arg(1));
if($node) {
$terms = array();
foreach($node->taxonomy as $term){
if($term->vid == 2){ //vocabulary ID
$terms[]=$term->tid;
}
}
return implode('+', $terms);
} else {
return FALSE;
}
?>
(without the php tags)
Again, not sure if it works well with multiple terms cause i don't use them.
Now i can use my carousel as a block acting like I want.
I chose method 1 because I wanted the carousel inside the node, and not in a block.
At this point, the carousel is working, and shows only the video thumbnails relating to the same category of the video node that i'm viewing. That's pretty cool, and I'm pretty happy :)
Now I can sort the order of the carousel's items by using the different sort filters of my view.
Ok, let's sort them chronologically, old school , maybe we'll try to play with draggableviews later.
Chronologically is good.
And here comes the challenge.
My carousel displays 3 items at a time.
Say i've got 20 items , 20 videos of the same category.
If I want to navigate through the videos using the carousel, i'm screwed
Every time i'll go to a new node the carousel will be reset and i'll have to click and click again to reach the 20th video.
I want the video thumbnail of the node that i'm viewing to be in the center of my carousel, so that the left one would represent the previous video, and the right one the next video.
So I learned and I've made this patch.
That was an experience.
(this post is long enough)
Now I activate the 'node aware index start' and set the 'display offset' to 1, and here it goes!
And I can style the "active" node by tweaking the .jcarousel-item-0 selector.
Now my carousel is working exactly like I wanted.
Yipee!
...
I hope that could be useful for someone.
Cheers
LeMartialou