Controlling the contents of a column

From Achievo/ATK Wiki

Revision as of 23:14, 7 February 2008 by Ivo (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

ATK Howto: Controlling the contents of a column

Complexity: easy
Author: Ivo Jansch <ivo at achievo.org>

List of other Howto's

By default in recordlists, the values of a record are displayed. Sometimes, you will want to control what's displayed in a column.

For example, you may want to have a 'status' field have a different color depending on the status. Sometimes, you'll want to show images or icons instead of the value of the record.

There are 2 ways to do this.

Creating a display override for a single node

ATK features 'Magic Methods' to control the behaviour of things like recordlist columns.

You can add the following code to your node to override the display of a value:

 function content_display($record, $mode)
 {
   $attribute = &$this->getAttribute("content");
   $original = $attribute->display($record, $mode);
 
   if ($mode=="list")
   {
     $original = "<b>".$original."</b>";
   }
 
   return $original;
 }


In this case, the display of the field 'content' is altered. By checking the $mode, you can control whether you want to change the display in all situations, or just in record lists. Note that the name of the function is derived from the field you want to alter. If your field is named 'status', you would name the function 'status_display'.

You can easily modify this example to return an img tag instead of text, if you need it.

Creating a custom attribute

If you need to change the display in many nodes, and want to have a more generic, reusable approach, you can create a custom attribute with its own display logic.

See this howto for instructions on creating a custom attribute.

Personal tools
Navigation