A few months ago I posted about how to create a custom Panels pane, a critical reference for anyone who uses Panels layouts. The other part of the toolkit for quick and awesome layouts is the Display Suite module. With DS you can create new “Display modes” for your content, to be reused around the site. For example, on one recent site I had four standard ways to display my nodes: Full, Teaser, Mini-Teaser, and Search Result. DS made this configuration a cinch.
But just as in Panels you sometimes need a pane that isn’t provided out of the box, in Display Suite you sometimes want to add a field that isn’t really a field on your content. In a recent site build, I used this capability to include information from the Organic Groups a user belongs to on his profile as it appears in search results.
DS offers some ability to create this kind of custom field through the UI, but I’m talking about more complicated outcomes where you need/want to use custom code instead. This is actually even easier than custom panels panes.
In our example, we will display the user’s name, but backwards. Obviously you can do much more complex things with this, but it’s nice to have a simple example!
Declare your fields
First we have to tell Display Suite about our new custom field. We do this with hook_ds_fields_info().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Any guesses whathappens next? That’s right, we have to write our render function under the name we just declared. You can put anything here, really anything renderable at all.
1 2 3 4 5 6 7 8 |
|
That’s it. So simple, you’ll wonder why you ever did it any other way!