Skip to content
Jason Austin edited this page Aug 19, 2013 · 1 revision

OTF provides a way to store metadata about an entity without the need to modify a database table. Custom Fields can be assigned to any object and allow application administrators to specify what kind of metadata should be tracked.

For example, think of an object you may track like a computer. An application administrator may want to know certain meta information about the computer like the screen dimension, but this is not something that is going to be required to make a programmatic decision with in the app. Rather than add screen dimension to the computer object, you can just add those options via custom fields.

This should be done only when metadata information may not be known when creating the app, or for requirements that may change over time.

Custom Field Types

Custom Field Types are enclosures around Ot_Var_Abstract objects. They use these objects to transfer data, but they also make the application aware that they are used specifically for custom fields.

Custom Field Types are defined in the bootstrap file and registered via the Ot_CustomAttribute_FieldTypeRegister();

$varTypes = array();

$varTypes[] = new Ot_CustomAttribute_FieldType('text', 'Text', 'Ot_Var_Type_Text');

$ftr = new Ot_CustomAttribute_FieldTypeRegister();
$ftr->registerFieldTypes($varTypes);

You can create your own fieldtypes as long as they implement an Ot_Var_Abstract object.

Custom Field Hosts

Hosts are objects that can have custom fields attached to them. These have to be registered in your bootstrap file.

$hosts = array();

$hosts[] = new Ot_CustomAttribute_Host('Computer', 'Computer', 'Computer object');

$cfor = new Ot_CustomAttribute_HostRegister();
$cfor->registerHosts($hosts);

This will expose the Computer model to add in custom field types to it.

Adding custom fields to hosts

You can add custom fields to your hosts via the admin interface. Just go to App Config -> Custom Fields

Programmatically access custom fields

Once custom fields are added to your host, you will want to be able to read the content and write to it.

Reading Data

$cahr = new Ot_CustomAttribute_HostRegister();
        
$thisHost = $cahr->getHost('Computer');
        
$attributes = $thisHost->getAttributes($computerId);
        
$data->customAttributes = array();
        
$computerMeta = array()
foreach ($attributes as $a) {
    $computerMeta[$a['var']->getName()] = $a['var']->getValue();
}

Forms

Custom field types have form helpers to be able to generate Zend_Form objects, integrating easily into your existing Zend_Form objects.

class ComputerForm extends Zend_Form
{
    public function init()
    {
        $cahr = new Ot_CustomAttribute_HostRegister();

        $thisHost = $cahr->getHost('Computer');

        $customAttributes = $thisHost->getAttributes();

        foreach ($customAttributes as $a) {
            $elm = $a['var']->renderFormElement();
            $elm->clearDecorators();
            $elm->setBelongsTo('customAttributes');

            $this->addElement($elm);
        }
    }
}

Writing Data

The following example assumes that you have a Zend_Form that has been submitted and the setBelongsTo() method is set as in the previous example.

$cahr = new Ot_CustomAttribute_HostRegister();

$thisHost = $cahr->getHost('Computer');

$customAttributes = $thisHost->getAttributes($computerId);

foreach ($customAttributes as $attributeName => $a) {

    if (array_key_exists($attributeName, $values['customAttributes'])) {

        $a['var']->setValue($values['customAttributes'][$attributeName]);

        $thisHost->saveAttribute($a['var'], $computerId, $a['attributeId']);                                
    }
}