Drupal 7 Context module

By shane
Thu, 2012-12-20 13:37
0 comments
Daily Dose of Drupal Episode #72

The Drupal 7 Context module allows you to easily organize your site into manageable sections (a.k.a "contexts"). These contexts can be used to change the layout of the page depending on where you are on the site. For instance, you may need certain blocks to show up in the sidebar on the homepage, and then change that to show up differently on blog pages.

Note: You will need to install the Drupal 7 Chaos tools (ctools) module in order to use the Drupal 7 context module.

In this episode you will learn:

  • The basics of the Drupal 7 context module and how it can be used to better architect the sections of your Drupal website
  • How to add contexts to your Drupal websites
  • How to display the same block in multiple regions depending on your current context

Hello everyone and welcome to another Daily Dose of Drupal, we’re on Episode Number 72 today. I’m Shane, you can follow me on Twitter @smthomas3, also sign up for the newsletter at codekarate.com and follow me on Google + as well.
Today we’re going to be going over a module that I’ve used on a few Drupal sites, it’s called the context module and this module … it basically extends and allows you to … I would say modify the architecture of your Drupal website and instead of just having the basic blocks, it actually extends traditional block system a little bit to allow you to be more flexible in your layouts.

So it allows you to organize your sites into what I would consider to be sections or logical pieces that you can then change the display on. So let’s say for instance you wanted it to be display a certain block and with top of the test site here.
A certain block on the left side bar on the homepage but you want it to reuse that same block up in the header section on another page. With a traditional Drupal Block system you can’t do that but you can reuse the same block in multiple regions. However; with Context that becomes possible.

It could do a lot of other cool stuff as well. There’s other modules you can use besides Context such as maybe they make panels work for this and a few other things but we’ll go over context module I would recommend trying out yourself and seeing if you like it or not. Some people have different opinions on the module.

I happen to like it but only in certain situations if you can have a really clean architecture for your site and break it down into organized sections. So we’ll go ahead and get started; hop over to the modules page in the Context section there is Context and Context UI, there’s also Context Layouts which allows you to use theme layer to with different regions but I’m not going to go over that, I’m just going to go over the Context and the Context UI module, you can of course set permissions, you can also configure it, it’s right under here under Structure Context. Here it basically allows you to organize into different sections.

So in this example I’m going to set it up so on the homepage I’ll display the navigation on the left side bar but on article pages I’ll put it in a different position, maybe the right sidebar, maybe the up here in the header, we’ll see what happens. Okay so let’s go ahead and click Add, we’ll call this the homepage and we’re going to add a position and I believe we can go ahead and select Path and for the Path we’ll use Front which is the front page, we’re only going to have one condition here and the reaction is going to be … we’re going to change the blocks, you can of course change the Breadcrumb, change different menus, there’s a whole bunch of different things so it’s not just for blocks but that’s one of the most common uses of it.

So on the homepage; for Sidebar; first we’re going to go ahead and find this navigation menu and we’re going to add it to the first sidebar, we’ll also use a powered by Drupal and put that in the first sidebar as well. So you can see you can organize it just like you can on the block’s interface and we’ll go ahead and save this but before we do that we’ll just walk you through on how to do this.
So it’s the homepage, it’s going to run through these checks of this condition. It’s going to go ahead and see that we … if we run the front page then this is what’s going to happen; we’re going to display these blocks in whichever regions we have then displayed in. So let’s save this, only lower case letters, go ahead and save it. Okay so let’s go ahead and take a look.

So this should be working, you can see the powered by Drupal navigation is there, let’s go back into it one more time and just to confirm it is working we’re going to add one more block just to show you that it works, we’ll add something to the featured section, we’ll go ahead and show recent content in the featured section on the homepage. You’ll notice now it’s quite a bit different, you have a homepage with recent content in the featured section navigation and the powered by Drupal.

This isn’t I think you can do with blocks of course but let’s say when we go to an article page maybe we want the navigation to be on the right, maybe we want the powered by Drupal link to be in the footer on other pages. So we’ll go ahead and create a new context for that.

And like I said before; it’s really good to really think about how you want to organize your site, if you have a homepage that might be a separate context you may have a blog portion of your site that’s going to be managed differently than other static pages for instance so really think about how you want to be doing this but you’d really don’t want to be adding a context for every single page at the site that kind of defeats the purpose so you kind of want to be able to group pages together so we’ll call this articles and we’ll say in this case node type is going to be article and if you want this contents or if you want this context to be apply on node forms, we’re going to say no and in this case we’re going to change the blocks so the navigation is going to be moved to … let’s go ahead and put the navigation in sidebar second, of course these are all arbitrary, I don’t know how this is going to look and let’s say we also want recent content … if I can find that, that’s more recent content in this second sidebar and then we’ll put the powered by Drupal into the footer so we’ll save this, now we have articles in the homepage and as we look at this you’ll notice it’s all based on this condition of the node types.

So if you’re on the node page of article it’s going to use this specific context. As you can see it adds this navigation over here in this recent content, there’s the Powered by Drupal in the footer, let’s go ahead and hop back in here and take a look at it one more time. So it’s really basic on how it works.

One of the things you’ll notice is it of course doesn’t remove the previous blocks. So if you wanted to control it completely with context you can go ahead just hide your other blocks so they are not showing up, we’ll save that, come back, you’ll notice now it’s not using the original block system anymore.

The homepage is a different structure; if you go to onto an article page you have completely different structure all together. So it’s an extremely useful module but we’re just really scratching the surface of what it can actually do but all in all you just have to remember it’s really not that confusing, you may be intimidated if you haven’t look at it before but it’s really just a set of when something … when you’re in a certain page or once you’re in a certain section of the site that’s what the conditions are for then you want to do something specific whether it displaying blocks, changes in the menus, breadcrumbs and so you can really play around with it, dig in and find ways that you can use it on your next Drupal project. Thanks again for watching the Daily Dose of Drupal and we will see you next time.

Need to learn more Drupal?

Premium Drupal Training Online (Horizontal)

Post new comment