You are here

function _civicrm_entity_render_fields in CiviCRM Entity 7.2

Default Display Suite Render handler for CiviCRM "fields".

1 string reference to '_civicrm_entity_render_fields'
civicrm_entity_ds_fields_info in ./civicrm_entity.module
Implements hook_ds_fields_info().

File

./civicrm_entity.ds.inc, line 6

Code

function _civicrm_entity_render_fields($field) {
  $settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : array();
  $settings += $field['properties']['default'];
  $wrapper = entity_metadata_wrapper($field['entity_type'], $field['entity']->id);

  //civicrm_contact civi_user field needs special handling
  switch ($field['entity_type']) {
    case 'civicrm_contact':
      $formatted_value = _civicrm_entity_contact_get_formatted_values($field, $wrapper);
      break;
    case 'civicrm_participant':
      $formatted_value = _civicrm_entity_participant_get_formatted_values($field, $wrapper, $field['entity']);
      break;
    default:
      $formatted_value = $wrapper->{$field['field_name']}
        ->value();
      break;
  }

  // primary_contact_id_contact and other fields are objects produced by the meta wrapper
  // Will be ease to generate links to the entities if it is consistent across entities
  if (is_object($formatted_value)) {
    $formatted_value = $wrapper->{$field['field_name']}->id
      ->value();
  }
  if ($field['formatter'] == 'civicrm_link') {
    global $base_url;
    if ($formatted_value) {
      $formatted_value = '<a href="' . $base_url . '/' . str_replace('_', '-', $field['properties']['link_entity']) . '/' . $formatted_value . '">' . _civicrm_entity_get_entity_label($field['properties']['link_entity'], $formatted_value) . '</a>';
    }
  }
  if ($field['formatter'] == 'civicrm_yes_no') {
    if ($formatted_value) {
      $formatted_value = 'Yes';
    }
    else {
      $formatted_value = 'No';
    }
  }
  if ($field['formatter'] == 'civicrm_true_false') {
    if ($formatted_value) {
      $formatted_value = 'True';
    }
    else {
      $formatted_value = 'False';
    }
  }
  if ($field['formatter'] == 'civicrm_option_value') {
    if ($formatted_value) {
      $formatted_value = _civicrm_entity_option_lookup($field, explode(',', $formatted_value));
    }
  }
  if ($field['formatter'] == 'civicrm_date') {
    if (!empty($formatted_value) && !empty($field['formatter_settings']['date type'])) {
      $formatted_value = format_date((int) strtotime($formatted_value), $field['formatter_settings']['date type']);
    }
  }
  if ($field['formatter'] == 'civicrm_timestamp') {
    if (!empty($formatted_value) && !empty($field['formatter_settings']['date type'])) {
      $formatted_value = format_date($formatted_value, $field['formatter_settings']['date type']);
    }
  }

  // Wrapper and class.
  if (!empty($settings['wrapper'])) {
    $wrapper = check_plain($settings['wrapper']);
    $class = !empty($settings['class']) ? ' class="' . check_plain($settings['class']) . '"' : '';
    $output = '<' . $wrapper . $class . '>' . $formatted_value . '</' . $wrapper . '>';
  }
  else {
    $output = $formatted_value;
  }
  return $output;
}