Using Custom Fields in Display Suite for Drupal 7

ArticleJanuary 10, 2013

Looking for even more flexibility with your Display Suite custom fields? Check out the follow up post, Advanced Custom Fields in Display Suite, to learn about hooking into the DS API and creating custom fields in code.

Anyone who uses Display Suite to manage the layout of their site’s various entities during the web development process can attest to how great a tool it is. The default layouts it comes with cover a majority of design needs and the extra “fields” it creates for you to place in your layouts (page title, author information, links, etc.) mean greater flexibility without having to write a bunch of templates and preprocess hooks. Instead you are provided with a drag and drop interface, not unlike the one on Drupal’s core block placement page.

However the really amazing feature is the ability to create custom fields that we can position inside our layouts. These fields could be things we may not want content editors to be able to edit, but still need to fall within the content of our entities. In this post we’re going to go over the 4 different types of fields that can be created.

First, the Basics

The drupal fields editor menu

You can view all of the custom Display Suite fields on it’s admin page at MYDOMAIN.com/admin/structure/ds/fields. When you add a new field, you can restrict which entities, which bundles, even which view modes are able to use the new field. To restrict by entity simply check which ones should have access to the new field. To restrict by bundles and view modes, use the “Limit Field” textarea to place each restriction on a new line. For example, the field in the screenshot will only be available on blog nodes, for all view modes.

An example blog post wysiwig

Code Fields

An example code fields wysiwig

The first type of field you can create is also the most basic, the Code Field. As the name suggests, this allows you to place snippets of HTML into your pages. Simply choose your input filter and provide your markup in the textarea. You may also use PHP if you have the core PHP Filter module enabled. Of course custom PHP should be placed into it’s own module, and this is possible with Display Suite’s API. However that is a bit out of the scope of this post.

Block Fields

An example block fields wysiwig

Have you ever wished you could place a block inside your page’s content? As the name of the Block Fields suggests, it allows you to do just that. First pick any block in your site to be used as a field. Then decide whether or not you want the whole block or only the block’s content and there you go. 

Preprocess Field 

This field is one of the trickier ones. There is no configuration aside from the basics. The key to this field is it’s machine name. If the machine name matches that of the name of a variable in your node.tpl.php it will pull that information into the field.

Dynamic Field 

the drupal dynamic field menu

This is arguably the most powerful of the different types of fields you can create. With this you can place almost any part of your site, blocks, menus, forms, etc. Almost any part of your site is available to you. After creating the field and placing it in your display, you’ll see a configuration cog on the righthand side. Click that and you’ll be able to pick which content goes into your field.

Continuing with the blog example— let’s say you want a section of related posts by taxonomy terms. You would first need a View of your blog posts. Then you’d need a contextual filter that accepts the term of the node we are viewing. Traditional blocks make passing the arguments difficult because they are not directly available. A series of relationships or some custom PHP perhaps could get the job done, but wouldn’t it be nice to pass our arguments directly in? If you enable the Views Content Panes module, part of the Chaos Tools Suite,  you can do just that. Simply create your view and on the field settings, you can manually pass your arguments directly into the view using tokens.

And that's just the Beginning

There are infinite possibilities when it comes to what can be done with these fields. If we add in Token support in Code fields and all the different configuration options in a Dynamic Field, then custom fields in Display Suite let us take our entity layouts to the next level. More and more during drupal development projects I find myself relying on these fields for layouts instead of other tools.