ASU Web Community

Put that Drupal view EXACTLY where you want it with a little PHP

Friday, January 11th, 2008 - 1:37 pm
  • ngudmuns
  • ngudmuns's picture

So, a few weeks ago I was struggling with getting my views to display where I wanted them to go on my site. There was a site which had some content on the front page which I wanted users to be able to edit. I wasn't able to code the page using html, for various reasons. I instead created multiple views for some links, a featured story, etc. Views gives you the option to provide page view, or provide block view, but I wanted greater control over where on the page the content was output.

The code to access a view and display it wherever you want is actually pretty simple. I put it inside a div to give you an idea of how you can control exactly where on a page it is located:

1 <div id="mainlinks">
2 <?
3 $view = views_get_view('links_view');
4 $view->page_type = 'list';
5 echo views_build_view('embed', $view, array(arg(1)), false, 15);
6 ?>
7 </div> <!--// mainlinks -->

On line 1, i've created my div that I have specific css styling options set (color, border, background, and positioning). Line 3 will go out and grab the view entitied 'links' and assign the view and all of it's properties to the $view array. Then, I can set some options for the view, such as on line 4, I modified the array parameter page_type, which i have set to 'list.' There are a couple other types, I believe you can set teaser or table as well.

Then, on line 5, i build the view using views_build_view with some parameters. the first parameter, 'embed,' will embed the view into the page, and bypass any block/page specific things that could be applied to it. If you changed that to 'block,' it will output as a block. You guessed it, 'page' will output as a page. You can also choose 'result.' Choosing this will give you an $info array with the following information:

query - the query run
countquery - count query run if limiting was required
summary
level - the level of the missing argument
result - a database object that you can use db_fetch_object on

Also, 'items' will return the $info array, but will give you an array of objects containing the results of the query. 'queries' returns an array but doesn't actually run them.

The second parameter to line 5 is the name of the view array that you created on line 3. I'm not entirely sure what the 3rd and fourth arguments do, but the 5th argument (the number '15') is how many results to return.

As you can see, using a small bit of php you can take any view and customize the results to make a page look very unlike a default drupal page. This is useful for front pages, or really any spot in Drupal that you'd like to be customized via a *.tpl.php. I used this in a page-front.tpl.php file to change the look and feel of the front page. I hope somebody can use this info. I know it helped me make a better Drupal page :)

Thursday, January 24th, 2008 - 2:31 pm
  • eftucker
  • eftucker's picture

I will give it a try.