Using Display Suite to alter 'fields'

I use the Display Suite module - http://drupal.org/project/ds - to alter the display of both nodes and node edit forms.  Recently, on a certain content type I needed to combine the display of two number fields into one.  I found a useful method using Display Suite.

My content type is a Citation, either a publication or a presentation, with the publication version having integer fields for its starting and ending pages within the Journal.  I need these fields to be displayed together, like:

Pages: 7 - 12

I could have built this as one text field, but that would have allowed the content creator to fill in values with various formats which I really don't want.

However, Display Suite allows you to have what it calls 'Custom Fields' (sometimes referred to as 'Code Fields').  These are not to be confused with D7's built-in fields, although  you can use D7 fields via Tokens.

This image is from the Manage Display tab of the Citation content type.

Code FIeld

So what I did was create a Code Field called pages which affects only Node entities and uses the following PHP (using the Display Suite text format).

<?php if (!empty($entity->field_cite_starting_pg )) {print '[node:field-cite-starting-pg] - [node:field-cite-ending-pg]';} ?>

where [node:field-cite-starting-pg] and  [node:field-cite-ending-pg] are the tokens for the integer fields.

The !empty($entity->field_cite_starting_pg ) is needed since the page numbers are not used in the presentation version of the node.  This checks that the node has a value for the starting page number.

Pages Code Field

Addendum: After I set up the code field, I figured out that to limit the field appearing in other content types, you need to set a value in the 'Limit Field' textarea.  In my case I just entered 'citation|*" since that is the bundle type of the node entity and the '*' is for all view nodes.