SawRed Development Blog.

Using views_embed_view


Posted on 24th June, by Matt in Drupal. No Comments

The need arised to display a single content type — which included a conditional field that separated the content into two categories depending on that condition — in a block depending on which category’s landing page a user arrived on. Following? No? Alright….

Assume you have two factions of one website, for example’s sake, lets say you’re building a summer camp website that has a girls side and a boys side. A cookie is set when a user arrives at domain.org/boyscamp or /girlscamp that simply states which page they landed on. Both sides have campfire nights that they would like to publish on the website using the same form, with a condition boys or girls, but only one of the sides is going to display in a single block. The website is the same for both sides with the exception of the landing page — both sides access the same nodes, so how do we display this side specific block on all nodes?

There’s several ways to go about this actually. You could use Context, with Context PHP and pull the cookie value and create 2 Contexts, one with each cookie evaluating true. But perhaps you don’t want to install Context just for a single purpose — it is a lot of module for one block.

A second method is to create a PHP block and use views_embed_view with an elseif based on the cookie value:

if ($_COOKIE["cookiename"] == "site1") {
print views_embed_view('Campfire','block_1');
} elseif ($_COOKIE["cookiename"] == "site2")  {
print views_embed_view('Campfire','block_2');
}

Either way, you’ll now have both Views in a single block.





Leave a Reply

Connect with Facebook