Okay
  Print

Field System - Display Object

When a registered field is called via Property Factory to build its markup for front end display purposes, the field system will search for the entry value of objects.display in the field configuration array and when found the entry value will be treated as the class name when building the display object.

The display object should extend VTCore_Property_Models_Element and follow the rules of VTCore_Html_Base / VTCore_Bootstrap_Element_Base when building the markup.

Example of a simple display object



 /**
 * Class for building the field status
 *
 * @author [email protected]
 *
 */
class VTCore_Property_Element_Status
extends VTCore_Property_Models_Element {

  /**
   * Define the default context array
   * The context will be merged with user supplied context
   */
  protected $context = array(
    'type' => 'div',
    'attributes' => array(
      'class' => array(
        'property-element-status',
      ),
    ),

    // The field values
    'status' => false,

    // Icons
    'icon' => false,
    'icon_type' => false,
    'label' => false,

    'value_attributes' => array(
      'type' => 'div',
      'search' => 'value',
      'attributes' => array(
        'class' => array(
          'property-value',
        ),
      ),
    ),

    'label_attributes' => array(
      'type' => 'div',
      'attributes' => array(
        'class' => array(
          'property-label',
        ),
      ),
    ),
  );



  /**
   * Overriding parent method
   * @see VTCore_Html_Base::buildElement()
   */
  public function buildElement() {

    // Removing context with empty value, including '', false, 0
    $this->cleanEmptyContext();

    // Build size
    if ($this->getContext('status')) {

      // Call the original method, this is invoked to ensure
      // if in the future parent class has implemented new
      // logic it will get called.
      parent::buildElement();

      // Let the property element models handle the icon and label
      $this->buildLabel();

      // Grab status terms
      $term_ids = $this->getContext('status');
      if (is_array($term_ids)) {
        $term_ids = array_shift($term_ids);
      }

      // Build status
      $term = get_term($term_ids, 'property_status');

      if (!empty($term) && !is_wp_error($term)) {
        $this
          ->addChildren(new VTCore_Html_Element($this->getContext('value_attributes')))
          ->lastChild()
          ->setText($term->name);
      }
    }
    
    // Seems that the field is trully empty, dont build any markup!
    else {
      $this->setType(false);
    }
  }


  /**
   * Extending base method for checking if element is trully empty or not
   */
  public function checkVisibility() {
    return (bool) $this->getContext('status');
  }
}