Building a responsive website with Display Suite

Posted on 25 Apr
I've been excitied about Display Suite recently, so this blog entry is just a cursory overview. For more info on Display Suite - watch the screencasts. Also, I think most people are familiar with responsive web design so I just touch basics. More info on that over at A List Apart though.
Display Suite
The nice thing about Display Suite is it takes preprocess logic out of template.php and makes it drag & drop configurable in the administration area. Things like;
- Hiding the page title for certain pages
- Displaying blocks related to a node on their pages
- Rearranging node fields into column layouts without touching any templates
- Adding view modes without using info_alter hook
- Adding custom entity and preprocess fields
These are all configurable through the Display Suite admin.
Also, 0 templates. With Display Suite & View Modes - you can build pretty complex layouts without touching any templates and writing very little code. Technically, you could use a theme with 0 template files (drupal core templates), just a .info file and a css file.
Field-based layouts
Customizing layouts has become quite a bit easier in Drupal 7 with field-based layouts. Content fields like the body, title, display date etc. can be output in different regions. Display Suite, like panels allows you to configure region-based layouts for any content types. So for example the blog could be configured to use a layout like the grid / image at the top of this post instead of the traditional vertical layout.
Under Structure -> Layout -> Display Suite and 'Manage Display' you can select a new region-based layout for any content type. Display Suite comes with some built in layouts but you can create your own.

Building custom Display Suite layouts is easy with drush. On mac osx, drush is easily installed using homebrew.
I develop / test sites on my desktop using MAMP in a folder called "development" so I 'cd' into it with the command line.
drush help ds-build
This command shows syntax for creating custom display suite layouts.
drush ds-build "lasso layout" --regions="Left, Nested Top, Nested Center, Nested Bottom, Right" --css="lasso_layout.css"
This builds a folder called "lasso_layout" that includes a template file called "lasso-layout.tpl.php" which contains my new regions. I've also included a css file, "lasso_layout.css". Inside lasso-layout.tpl.php I've wrapped the three nested regions in a div, "wrap-nested" which is the hypothetical lasso. This custom layout generated by drush includes the barebones regions and .inc settings file that are needed but can be further configured just like a custom panels layout (they are the same and use ctools) -- explained here on Drupal.org.
This is html / php included in the layout tpl file ("lasso-layout.tpl.php");
<div class="<?php print $classes; ?> clearfix"> <?php if (isset($title_suffix['contextual_links'])): ?> <?php print render($title_suffix['contextual_links']); ?> <?php endif; ?> <?php if ($left): ?> <div class="ds-left<?php print $left_classes; ?>"> <?php print $left; ?> </div> <?php endif; ?> <div class="wrap-nested"> <?php if ($nested_top): ?> <div class="ds-nested-top<?php print $nested_top_classes; ?>"> <?php print $nested_top; ?> </div> <?php endif; ?> <?php if ($nested_center): ?> <div class="ds-nested-center<?php print $nested_center_classes; ?>"> <?php print $nested_center; ?> </div> <?php endif; ?> <?php if ($nested_bottom): ?> <div class="ds-nested-bottom<?php print $nested_bottom_classes; ?>"> <?php print $nested_bottom; ?> </div> <?php endif; ?> </div><!-- /.wrap-nested --> <?php if ($right): ?> <div class="ds-right<?php print $right_classes; ?>"> <?php print $right; ?> </div> <?php endif; ?> </div>
That's it. Customize the html if you need but don't add grid classes yet, that comes next and can be done in the administration through display suite. Move the layout into your theme folder and flush the cache.
You can then move fields into the new "lasso layout".

Responsive layout
Display Suite layout features include the ability to add html classes to your fields for css styling. This makes it prime for including grid columns into your layout. You first need to include all of your grid classes then they will be available as point and click for the layout fields.
In the administration under Structure -> Display Suite -> Layout you can configure the layout for any content types under 'Manage Display'. At the bottom is a tab option for, 'Extra classes for regions'. In 'Manage region styles' you can include all your grid classes
Responsive grid

Gridpak responsive layout generator. Building a responsive grid is fairly easy these days as outlined in Ethan Marcotte's book. His formula is as so;
- Fluid grid formula: target/content = result
- Fluid images: img{max-width:100%;}
- Media queries: @media screen and (min-width: 320px) etc.
But if you don't want to go to all that trouble of building your own grid from scratch there are some nice generators out there that can help you along your way. One such nice one is gridpak. You can configure your number of columns, gutter width's and breakpoints. Then just download a zip file with all your grid classes and pop them into Display Suite.
View Modes
You can't talk about Display Suite without talking about view modes. View Modes are a powerful new feature in Drupal 7. Traditionally you have two view modes, a "teaser" view mode and a "full" view mode. The teaser obviously displays a shorter version of the full view mode and links to the full version -- which shows all the content. Now you can create your own view modes. "Lasso layout" can be just one view mode for the blog, another view mode could be a different custom region-based layout.
Through Display Suite you can create new view modes under Structure -> Content Types -> Manage Display -> Manage View Modes.
Adding custom view modes in a module without using Display Suite need to implement hook_entity_info_alter(), example;
/** * Implements hook_entity_info_alter() */ function mymodule_entity_info_alter(&info) { info['node']['view modes'] += array( 'Slideshow' => array( 'label' => t('Slideshow'), 'custom settings' => TRUE, ), ); }
Example, one view mode could be a slideshow. Just show an image field in that particular view mode and wrap it with some slideshow widget or jquery.

More great info on view modes in this excellent screencast.
Display View Modes not View fields
Views is so powerful you can build any layout with it by putting together view fields, filtering by content types, sorting etc.. It's drawback is too many template files when you need to customize and include php.
Display view modes not fields and cut down on templates and php.

Custom Fields
For extra logic, create custom fields. Structure -> Display Suite -> Fields.

<?php print $entity->type; ?>
Print the article type. $entity is the object for either the node, taxonomy term or user.
I'm still playing around with View Modes and Display Suite so maybe I'll blog more on them later. But there's more real awesome features under the "Extra's" area of Display Suite. Including "region to block", display blocks semantically by node id. Configure the field templates (remove html). Integration with panels and more.
Finally though -- you can write less preprocess functions and customize fewer template files.
Media Queries and mobile first
I was talking about responsive web design...
I won't go into this much since it's been done and is best explained in the short but concise A Book Apart and Luke's blog. The best way to build media queries is using the mobile first method. This entails building the site for how it will look in a mobile browser first then using media quieries for the breakpoints as the layout expands for tablet's and desktop's. In this case, media queries use the 'min-width' syntax instead of the 'max-width'.
@media screen and (min-width: 320px) { }
Meta Tags
Don't forget meta tags so mobile browsers stop natively using a 980pixel layout and instead use your responsive layout. These can go inside html.tpl.php.
<meta name="MobileOptimized" content="width"> <meta name="HandheldFriendly" content="true"> <meta name="viewport" content="width=device-width"> <meta http-equiv="cleartype" content="on">
Helpful modules
Some modules I've found helpful for building responsive layouts include;
- Adaptive image for responsive image handling
- CSS Emdedded Images converts your images to data uri's so it's http request is included only with the stylesheet
- Respond.js provide's javascript support for media queries in Internet Explorer 6-8
- Boost Cache is an excellent module which I use all the time. It creates cached static pages which can dramatically decrease page load times for anonymous users.
- SASS !! , sass is great. Just use it.







