Drupal tutorial: Embedding a view
At some stage almost everyone running a website based on Drupal will find that they want to add a view somewhere on the site where it can't easily be done. Most often, views can be added to blocks to accompany content types or pages and the block visibility rules can be used to tweak who gets to see the view and when. Views can easily be added to panel pages, and most have their own dedicated page anyway. Despite all this, you might still need to know how to add a view manually...
Luckily, Drupal provides a PHP function that can be used to embed a view anywhere on the website. Before we start adding code snippets all over the show, remember that some views require a context in which to operate. For example, a view may require a node ID to be passed as an argument and this could lead to unexpected behaviour if the view is embedded somewhere where that argument is unavailable. It's important to think out all the possibilites beforehand to avoid disappointment.
The great thing about using this PHP function is that you really don't have to know anything about PHP programming - it's more a case of cutting and pasting and matching a few variables to suit your view. Here is the function:
In this case:
- $name - The name of the view to embed.
- $display_id - The display id to embed. If unsure, use 'default', as it will always be valid.
Used in context, and assuming there is a view called blogs with a display id of block_1, we can print out the view using the following PHP statement:
To embed any view somewhere on your own site, simply copy and paste the above statement into the page and change blogs to match the name of your view, and block_1 to match the name of a page or block in that view. If you're unsure of what the display id is for a particular view, then viewing the source code while browsing the view will give you the answer.
The class atrribute that houses the view will always denote the display id. In the above case, it was shown as:
As you can see, one of the classes tells us that the view-display-id is block_1. To prove this all works, I will embed the above statement directly in here for you to view the results. Here we go:
Other people also read
Et voila! Remember that you must have the input or text format set to allow PHP in order for this to work within a node. There's nothing stopping you from using this method anywhere on your website (bearing in mind the considerations mentioned at the start), although you can often avoid having to use PHP snippets by using what Drupal already provides - blocks, panels, etc.
You are online with David Mercer
"Bestselling author of books about the web, eCommerce and marketing." Wikipedia
Inspiring and empowering entrepreneurs, businesses, bloggers and startups through social, marketing, and business advice.
30 000+ readers and counting
"This made my day" - Guy Kawasaki
Author of Enchantment and The Art of the Start
Sponsor this webpage for cents a day
- Real SEO backlinks
- Premium page position
- Large ad space
- Displays every hit
- Targeted audience
- High conversions
Become a recognized visionary
Share your insights into the future of your niche. Gain a following and drive brand recognition, traffic and sales.
Social SEO website reviews
Add your site to the world's premiere web directory and bookmarking service. With social integration and SEO juice to deliver multiple backlinks & great ROI.
Boost book & product sales
Track, analyze and promote any book or product sold through Amazon, anywhere in the world from around 3c per day.
topulo.us predictions
- Social influence will overtake web traffic as the primary metric for advertisers and marketers
- "Steve jobs" by Walter isaacson will sell more than 200 000 copies in 2012 on Amazon.com alone.
- Apple's iPad will sink below 50% market share by mid 2013
- Google search plus your world will kill off Twitter within two years
- Cloud printing with cloud ready printers will kill off the fax machine
TopRank
- Online Marketing News: Facebook IPO, Harmonize with Google+, Pinterest Dominates Traffic
- Essential Shifts in Online Marketing: Are You On Board? Are You Optimized?
- Blogger Relations: Push or Pull? How to Get Bloggers to Mention Your Brand
- Getting Your Internet Marketing Dream Job: How to Interview & Succeed at a Top Agency
- Google+ Optimization vs. Community Building: Pros & Cons of Google SPYW
terms of use | privacy | about | write for us | affiliates | employment | partner with us | advertise | contact
© site prebuilder 2009





















