You are here

class UcAddressesUCXFHandler in Extra Fields Checkout Pane 7

Same name and namespace in other branches
  1. 6.2 includes/uc_addresses.handlers.inc \UcAddressesUCXFHandler

Field handler for all Extra Fields Pane fields.

Hierarchy

Expanded class hierarchy of UcAddressesUCXFHandler

2 string references to 'UcAddressesUCXFHandler'
uc_extra_fields_pane_uc_addresses_fields in ./uc_extra_fields_pane.uc_addresses.inc
Implements hook_uc_addresses_fields().
uc_extra_fields_pane_uc_addresses_field_handlers in ./uc_extra_fields_pane.uc_addresses.inc
Implements hook_uc_addresses_field_handlers().

File

includes/uc_addresses.handlers.inc, line 13
Integration code for Ubercart Addresses 7.x-1.x

View source
class UcAddressesUCXFHandler extends UcAddressesFieldHandler {

  // -----------------------------------------------------------------------------
  // PROPERTIES
  // -----------------------------------------------------------------------------

  /**
   * Extra Fields Pane field object.
   *
   * @var UCXF_Field
   * @access private
   */
  private $ucxf_field;

  // -----------------------------------------------------------------------------
  // CONSTRUCT
  // -----------------------------------------------------------------------------

  /**
   * Initialize.
   *
   * @access public
   * @return void
   */
  public function init() {
    parent::init();
    $fieldName = $this
      ->getFieldName();
    $this->ucxf_field = UCXF_FieldList::getFieldByName($fieldName);
  }

  // -----------------------------------------------------------------------------
  // ACTION
  // -----------------------------------------------------------------------------

  /**
   * Returns the editable field
   *
   * @param array $form
   * @param array $form_values
   * @access public
   * @return array
   */
  public function getFormField($form, $form_values) {

    // Check if the field may be shown if it is a specific pane we are generating
    // the field for.
    if (isset($form['#key_prefix']) && ($form['#key_prefix'] == 'delivery' || $form['#key_prefix'] == 'billing') && !$this->ucxf_field
      ->in_pane('extra_' . $form['#key_prefix'])) {

      // On the checkout and the order form, "#key_prefix" tells us for which pane
      // the field must be generated ("delivery" or "billing").
      // Extra Fields Pane has a setting to show the field in either the delivery
      // or the billing pane or both.
      // If the field may not be shown in one of these panes and that pane is the
      // current pane to generate a field for then:
      // - don't generate the field
      // - empty the field's value
      // - return an empty array instead.
      $this
        ->getAddress()
        ->setField($this
        ->getFieldName(), '');
      return array();
    }
    $fieldName = $this
      ->getFieldName();
    $fieldValue = $this
      ->getAddress()
      ->getField($fieldName);
    $default = isset($form_values[$fieldName]) ? $form_values[$fieldName] : $fieldValue;
    $field[$fieldName] = $this->ucxf_field
      ->generate();
    if (!is_null($default) || !isset($field[$fieldName]['#default_value'])) {
      $field[$fieldName]['#default_value'] = $default;
    }
    if (isset($form['#uc_addresses_required']) && $form['#uc_addresses_required'] === FALSE || $this
      ->isFieldRequired() === FALSE) {
      $field[$fieldName]['#required'] = FALSE;
    }

    // On the order edit form, a generated field shouldn't be a hidden field.
    // In this case the field will be set to a normal textfield, so it's editable.
    if ($this
      ->getContext() == 'order_form') {
      if ($field[$fieldName]['#type'] == 'hidden') {
        $field[$fieldName]['#type'] = 'textfield';
        if (is_null($default)) {
          $field[$fieldName]['#default_value'] = $field[$fieldName]['#value'];
        }

        // Unset value, field already has an default value.
        unset($field[$fieldName]['#value']);

        // Set title and size of field.
        $field[$fieldName]['#title'] = $this->ucxf_field
          ->output('label');
        $field[$fieldName]['#size'] = 32;
      }
    }
    elseif ($field[$fieldName]['#type'] == 'hidden' && $this->ucxf_field
      ->may_display('checkout')) {
      $field[$fieldName . '_item'] = array(
        '#type' => 'item',
        '#title' => $this->ucxf_field
          ->output('label'),
        '#markup' => $field[$fieldName]['#value'],
      );
    }

    // In case of select fields, add default value as an option if it is not an available option yet.
    if ($this
      ->getContext() == 'order_form' && $field[$fieldName]['#type'] == 'select') {
      $default_value = $field[$fieldName]['#default_value'];
      if ($default_value != '' && !isset($field[$fieldName]['#options'][$default_value])) {
        $field[$fieldName]['#options'][$default_value] = t('Deleted option: @option', array(
          '@option' => $default_value,
        ));
      }
    }
    return $field;
  }

  /**
   * Check to see if the field is enabled.
   *
   * @access public
   * @return boolean
   *	 TRUE if the field is enabled.
   */
  public function isFieldEnabled() {
    return (bool) $this->ucxf_field->enabled;
  }

  /**
   * Check to see if the field is required.
   *
   * @access public
   * @return boolean
   *	 TRUE if the field is required.
   */
  public function isFieldRequired() {
    if ($this
      ->getContext() == 'order_form') {
      return FALSE;
    }
    return (bool) $this->ucxf_field->required;
  }

  /**
   * Returns a default value for this field.
   *
   * @access public
   * @return null
   */
  public function getDefaultValue() {
    return NULL;
  }

  // -----------------------------------------------------------------------------
  // OUTPUT
  // -----------------------------------------------------------------------------

  /**
   * Output a fields value.
   *
   * @param mixed $value
   * @access public
   * @return string
   */
  public function outputValue($value = '', $format = '') {
    if ($value === '') {
      $value = $this
        ->getAddress()
        ->getField($this
        ->getFieldName());
    }
    $output = $this->ucxf_field
      ->output_value($value);
    if ($output == t('n/a') && $this
      ->getContext() == 'token') {

      // If the context is "token", don't output "n/a". Just return an
      // empty string in this case.
      // This is because tokens can be used in address labels and we
      // don't want to end up with a "n/a" in there.
      return '';
    }
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UcAddressesFieldHandler::$address private property Address object.
UcAddressesFieldHandler::$context private property The context in which this field is used.
UcAddressesFieldHandler::$definition private property The declared field definition.
UcAddressesFieldHandler::$name private property Name of this field.
UcAddressesFieldHandler::checkContext public function Checks if the field passes the context. 1
UcAddressesFieldHandler::getAddress final public function Returns the address attached to this field.
UcAddressesFieldHandler::getContext final public function Returns the context in which this field is used.
UcAddressesFieldHandler::getFieldName final public function Returns the field name.
UcAddressesFieldHandler::getFieldTitle public function Returns the title to use when displaying a field. 1
UcAddressesFieldHandler::getMappingTargets public function Returns supported mapping targets for Feeds. 2
UcAddressesFieldHandler::getOutputFormats public function Returns an array of possible output formats the handler supports. 2
UcAddressesFieldHandler::getProperty final public function Returns a property from the field definition.
UcAddressesFieldHandler::getTokenInfo public function Returns supported tokens. 1
UcAddressesFieldHandler::mapValue public function Set a fields value based on the output format. 2
UcAddressesFieldHandler::setValue public function Sets value in the address object.
UcAddressesFieldHandler::validateValue public function Check a fields' value. 2
UcAddressesFieldHandler::__construct final public function UcAddressesFormField object constructor.
UcAddressesUCXFHandler::$ucxf_field private property Extra Fields Pane field object.
UcAddressesUCXFHandler::getDefaultValue public function Returns a default value for this field. Overrides UcAddressesFieldHandler::getDefaultValue
UcAddressesUCXFHandler::getFormField public function Returns the editable field Overrides UcAddressesFieldHandler::getFormField
UcAddressesUCXFHandler::init public function Initialize. Overrides UcAddressesFieldHandler::init
UcAddressesUCXFHandler::isFieldEnabled public function Check to see if the field is enabled. Overrides UcAddressesFieldHandler::isFieldEnabled
UcAddressesUCXFHandler::isFieldRequired public function Check to see if the field is required. Overrides UcAddressesFieldHandler::isFieldRequired
UcAddressesUCXFHandler::outputValue public function Output a fields value. Overrides UcAddressesFieldHandler::outputValue