You are here

function civicrm_entity_get_field_widget in CiviCRM Entity 7.2

Utility function that takes the provided field specs from a getfields call and builds a widget property array on the property metadata info

Parameters

$field_spec:

Return value

array

2 calls to civicrm_entity_get_field_widget()
_civicrm_entity_getproperties in ./civicrm_entity.module
Calculate fields for entities
_civicrm_entity_profile_generate_profile_fapi in modules/civicrm_entity_profile/includes/civicrm_entity_profile.profile.inc
Generates the FAPI for an individual CiviCRM Profile

File

./civicrm_entity.module, line 2449

Code

function civicrm_entity_get_field_widget($field_spec, $civicrm_entity) {
  $widget = array();
  if (isset($field_spec['name']) && $field_spec['name'] == 'id') {
    $widget = array(
      'widget' => 'hidden',
    );
  }
  if (!isset($field_spec['type'])) {
    $widget = array(
      'widget' => 'hidden',
    );
  }
  $field_type = 'standard';

  // "normal" properties
  if (isset($field_spec['html']['type'])) {
    $html_type = $field_spec['html']['type'];
    $field_type = 'standard';
  }
  elseif (isset($field_spec['html_type'])) {
    $html_type = $field_spec['html_type'];
    $field_type = 'custom';
  }
  if (isset($html_type)) {
    switch ($html_type) {
      case 'Text':
        $widget = array(
          'widget' => 'textfield',
          'civicrm_field_type' => $field_type,
        );
        break;
      case 'Select':
        $widget = array(
          'widget' => 'select',
          'civicrm_field_type' => $field_type,
        );
        $widget['options'] = civicrm_entity_get_field_options($field_spec['name'], $civicrm_entity);
        break;
      case 'Radio':
        $widget = array(
          'widget' => 'radios',
          'civicrm_field_type' => $field_type,
        );
        $widget['options'] = civicrm_entity_get_field_options($field_spec['name'], $civicrm_entity);
        break;
      case 'Autocomplete-Select':
        $widget = array(
          'widget' => 'textfield',
          'civicrm_field_type' => $field_type,
        );
        break;
      case 'CheckBox':
        $options = civicrm_entity_get_field_options($field_spec['name'], $civicrm_entity);
        if ($field_type == 'custom' && count($options) > 1) {
          $widget = array(
            'widget' => 'checkboxes',
            'civicrm_field_type' => $field_type,
            'options' => $options,
          );
        }
        else {
          $widget = array(
            'widget' => 'checkbox',
            'civicrm_field_type' => $field_type,
          );
        }
        break;
      case 'RichTextEditor':
        $widget = array(
          'widget' => 'text_format',
          'civicrm_field_type' => $field_type,
        );
        break;
      case 'TextArea':
        if (!empty($field_spec['description']) && strpos($field_spec['description'], 'Text and html allowed.') !== FALSE) {
          $widget = array(
            'widget' => 'text_format',
            'civicrm_field_type' => $field_type,
          );
        }
        else {
          $widget = array(
            'widget' => 'textarea',
            'civicrm_field_type' => $field_type,
          );
        }
        break;
      default:
        $widget = array(
          'widget' => 'textfield',
          'civicrm_field_type' => $field_type,
        );
        break;
    }
  }
  else {
    $widget = array(
      'widget' => 'textfield',
    );
  }

  // some date field handling
  if (isset($field_spec['type'])) {
    switch ($field_spec['type']) {
      case 4:
        $widget = array(
          'widget' => 'date_select',
          'format' => 'Y:m:d',
        );
        break;
      case 8:
        $widget = array(
          'widget' => 'date_select',
          'format' => 'H:i:s',
        );
        break;
      case 12:
        $widget = array(
          'widget' => 'date_select',
          'format' => 'Y:m:d H:i:s',
        );
        break;
    }
  }

  // FK Reference field handling
  if (!empty($field_spec['FKApiName'])) {
    $widget_subtype = civicrm_entity_fk_entities_to_produce_widget_for($field_spec['FKApiName']);
    if ($widget_subtype) {
      $widget = array(
        'widget' => 'civi_fk_reference',
        'subtype' => $widget_subtype,
        'entity' => $field_spec['FKApiName'],
      );
    }
  }
  if ($civicrm_entity == 'participant' && $field_spec['name'] == 'event_id') {
    $widget = array(
      'widget' => 'civi_fk_reference',
      'subtype' => 'select',
      'entity' => 'Event',
    );
  }

  // some one off handle for properties of some entities that don't provide html type information
  $entities_to_alter = array(
    'address',
    'phone',
    'email',
    'im',
    'relationship',
    'membership_type',
    'activity',
  );
  if (in_array(strtolower($civicrm_entity), $entities_to_alter)) {
    $checkbox_fields = array(
      'is_primary',
      'is_billing',
      'on_hold',
      'is_bulk_mail',
      'is_active',
      'is_permission_a_b',
      'is_permission_b_a',
      'is_deleted',
    );
    if (in_array($field_spec['name'], $checkbox_fields)) {
      $widget = array(
        'widget' => 'checkbox',
        'civicrm_field_type' => 'standard',
      );
    }
  }
  if ($civicrm_entity == 'membership_type' && !empty($field_spec['name']) && $field_spec['name'] == 'relationship_type_id') {
    $widget = array(
      'widget' => 'civi_fk_reference',
      'subtype' => civicrm_entity_fk_entities_to_produce_widget_for('RelationshipType'),
      'entity' => 'RelationshipType',
    );
  }
  if ($civicrm_entity == 'activity' && !empty($field_spec['name']) && $field_spec['name'] == 'result') {
    $widget = array(
      'widget' => 'select',
      'civicrm_field_type' => $field_type,
    );
    $widget['options'] = civicrm_entity_get_field_options($field_spec['name'], $civicrm_entity);
  }
  if ($civicrm_entity == 'contact' && !empty($field_spec['name']) && $field_spec['name'] == 'communication_style_id') {
    $widget = array(
      'widget' => 'select',
      'civicrm_field_type' => $field_type,
    );
    $widget['options'] = civicrm_entity_get_field_options($field_spec['name'], $civicrm_entity);
  }
  return $widget;
}