Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Introduction to Context and Bean

Video loading...

  • 0:05
    Context and Bean Layout Tools for Drupal
  • 0:08
    Introduction to Context and Bean with Kyle Hofmeyer
  • 0:15
    In this series of lessons,
  • 0:16
    we're going to cover two modules that assist in laying your site
  • 0:19
    out when it comes to placing content in regions.
  • 0:22
    When it comes to performing layout in Drupal
  • 0:24
    there are more modules than I can even begin to discuss. Most
  • 0:27
    of these modules take over the entire system of layout
  • 0:29
    and can be very complex, but what if you're looking to build a site
  • 0:33
    and want some layout tools that enhance the current way Drupal
  • 0:35
    works, giving you just enough power to get the job done
  • 0:38
    and being able to be export to code?
  • 0:41
    The point and click interface of Drupal could be a very handy tool
  • 0:44
    but to perform these point and click operations on a live site not only
  • 0:48
    is scary but just a bad idea. Even the wrong click
  • 0:51
    could take down a site so most site builders these days are always
  • 0:54
    checking to make sure the tools they use
  • 0:56
    can export their configurations for a proper transition
  • 1:00
    to a different working environment.
  • 1:03
    The two modules we'll be learning are Context and Bean,
  • 1:06
    layout tools for your Drupal toolbox.
  • 1:08
    Both of these tools take layout and the block system to the next level
  • 1:12
    without being overly complex.
  • 1:14
    They both offer the ability to export your configurations
  • 1:17
    and have many advantages over what core offers.
  • 1:20
    So what is this series all about?
  • 1:22
    Well, I'm going to discuss how we can use the Context and Bean module
  • 1:26
    to lay out our sites, basically laying out our sites with blocks.
  • 1:30
    These modules are like the block system
  • 1:32
    but they're a lot more powerful.
  • 1:33
    What we're also going to do in this lesson
  • 1:35
    is we're going to take a look at some advantages, disadvantages,
  • 1:39
    and use cases for using both of these modules.
  • 1:41
    You're going to hear the words advantages and disadvantages quite
  • 1:44
    a bit throughout this series so I just want to make you
  • 1:46
    aware of that, but that's really what we're talking about
  • 1:49
    is why would you use these two modules in the first place
  • 1:51
    when I've already got the core block system?
  • 1:56
    So Context, what is Context?
  • 1:58
    Well, the Context module allows for contextual based layouts
  • 2:01
    based on conditions and reactions.
  • 2:03
    A context may have a single or multiple conditions
  • 2:06
    and/or reactions, and a site is not limited to the number of contexts
  • 2:10
    to build its layout.
  • 2:12
    This definition I put together, one of the most important parts
  • 2:15
    is the fact that you are not limited to the number
  • 2:18
    of contexts you can use.
  • 2:20
    So a perfect example of this you may have a context that places a block
  • 2:23
    in your footer, well you can have a completely different context that
  • 2:27
    places that exact same block in another completely
  • 2:30
    different region.
  • 2:32
    So you can continue to just stack up your context
  • 2:34
    and use different conditions, which we're going to see what conditions
  • 2:37
    really are and different reactions, which we're going to learn what
  • 2:40
    those are and you use all these things to basically use context
  • 2:43
    to start placing all the blocks on your website.
  • 2:49
    So why would we want to use Context?
  • 2:51
    Well, one of the biggest reasons that we'd like to use Context
  • 2:54
    is that we all know that the block system is just, it's too limiting.
  • 2:58
    You can basically place a block and maybe turn its visibility on or off
  • 3:02
    depending on a few criteria but you can't reuse blocks,
  • 3:06
    you can't once a block is in place, it's in place that's it
  • 3:09
    you're done, you can't do other things with it.
  • 3:12
    Also the block system configurations are not able to be stored in code
  • 3:16
    so that is a huge reason why we'd want to use the context module,
  • 3:20
    all the stuff we're going to be doing is exportable.
  • 3:25
    So let's go over some of the advantages.
  • 3:27
    Well, there's more methods for block placements and visibility.
  • 3:31
    You have the ability to hide an entire region based on conditions,
  • 3:34
    we don't really necessarily cover this, it's there,
  • 3:36
    it's just a huge advantage because you can actually instead of hiding
  • 3:40
    six blocks because a certain role you don't want to see those blocks,
  • 3:43
    well, why not just hide the entire sidebar?
  • 3:46
    Blocks are going to be reusable across different contexts,
  • 3:49
    and again, all configurations are going
  • 3:52
    to be exportable for site deployment.
  • 3:57
    So what is a use case that you may have
  • 3:58
    that you want to use a Context module for?
  • 4:01
    Well, in this series we're going to basically go over our demo site
  • 4:05
    and in that site what we did was create two different home
  • 4:08
    pages for the anonymous and authenticated user.
  • 4:10
    So now we can move blocks based on that homepage, based on their role,
  • 4:15
    so we'll have context that place the blocks
  • 4:17
    and also use context that look at their user role
  • 4:20
    depending on those roles where are the blocks going to go?
  • 4:25
    You may also want to use the Context module for sectional layout
  • 4:27
    of home pages within inner pages.
  • 4:31
    We're going to show an example of this
  • 4:32
    with our blog page, how the home part of the blog
  • 4:36
    looks different than when you look at an actual blog, and of course,
  • 4:39
    as we've mentioned you can have role based layout, so depending
  • 4:42
    on the role where are things going to be?
  • 4:47
    The Bean module-- the Bean module, why is it called Bean?
  • 4:50
    Because it stands for block entities aren't nodes.
  • 4:53
    So basically we use the Bean module to turn blocks into entities.
  • 4:59
    Well, let's take a look at what this means.
  • 5:01
    The Bean module may be used separately from Context
  • 5:03
    but together you have more power with layout
  • 5:06
    as well as exportable configuration.
  • 5:08
    Bean module turns blocks into field-able entities
  • 5:12
    similar to content types.
  • 5:14
    A bean, in simplest terms, is a better block.
  • 5:17
    So basically what I was saying was by making a block
  • 5:19
    an entity you get all the advantages of an entity itself.
  • 5:22
    Fields, managing those fields, managing displays
  • 5:26
    all those great things are why we want to use the Bean module.
  • 5:28
    But why use the Bean module?
  • 5:32
    Well, site builders choose to use the Bean module because we all know
  • 5:35
    like I've said before the block system just,
  • 5:37
    it doesn't get the job done.
  • 5:39
    All you get are very limited set of tools.
  • 5:42
    So with Bean you have what is equivalent to content
  • 5:44
    types for blocks.
  • 5:46
    This is great for content creators and a more flexible block system.
  • 5:52
    Advantages of using the Bean module.
  • 5:54
    Well, we're going to get block types which are pretty much
  • 5:57
    the same thing as content types but now for blocks.
  • 5:59
    Blocks are going to become field-able,
  • 6:01
    all our configurations are going to become exportable,
  • 6:03
    we're going to get display modes and view modes
  • 6:07
    and another thing we're going to cover
  • 6:08
    is that we get much better granular permissions with Bean
  • 6:12
    than we do with the core block system.
  • 6:16
    What is the use case for using the block system?
  • 6:18
    Well, a great use case in this demo is that we make an image block.
  • 6:23
    We can actually have a block that has an image field that you can use
  • 6:26
    with all the other things that you do when it comes to images.
  • 6:29
    You can have different image styles all of that,
  • 6:31
    so there's a great use case right there.
  • 6:33
    What if you want more complex blocks without views?
  • 6:35
    So now you can start like building a block with four
  • 6:38
    or five fields in it instead of taking fields from different nodes
  • 6:41
    and then building a block with it.
  • 6:43
    Also this way content creators are able to build a block content.
  • 6:48
    Now that might be a little bit confusing
  • 6:49
    but the great thing is now someone can create an image block
  • 6:52
    and through those permissions we discussed
  • 6:54
    they don't have to worry about all the other stuff that is part
  • 6:57
    of the block system or technically the bean system like right
  • 7:01
    now if they can create a block, they can
  • 7:03
    do a lot more like administer blocks.
  • 7:05
    So that's what we're going to be doing throughout this entire series
  • 7:08
    is just going over all the reasons why
  • 7:10
    you'd want to use Context and Bean.

Introduction to Context and Bean

Loading...

As Drupal site-builders and developers we are all very aware that Drupal 7 is not the most useful product out of the box. We constantly add modules and custom code to make Drupal do what we need. There is nothing wrong with this, in fact it is what attracts people to using Drupal in the first place.  The block system that comes with core is what we get after installation as our real only means to laying "stuff" out for our website. People have done things like turning nodes into blocks, or making every block on our a site a view. These concepts work, but have a lot of draw backs for usability and performance.  There are lots of layout tools to use, but this series is going to take a look at the Context and Bean modules.  These two modules are really two completely different modules but when used together give us some pretty powerful options in place of Drupal's core block system.

Additional resources: