You are here

function _civicrm_entity_getproperties in CiviCRM Entity 7

Same name and namespace in other branches
  1. 7.2 civicrm_entity.module \_civicrm_entity_getproperties()

Calculate fields for entities

Parameters

$civicrm_entity:

string $context:

Return value

array

2 calls to _civicrm_entity_getproperties()
civicrm_entity_entity_property_info_alter in ./civicrm_entity.module
Here we declare Selected CiviCRM entities fields to Drupal.
civicrm_entity_rules_data_info in ./civicrm_entity.rules.inc
Implements of hook_rules_data_type_info().

File

./civicrm_entity.module, line 409
Implement CiviCRM entities as a Drupal Entity.

Code

function _civicrm_entity_getproperties($civicrm_entity, $context = '') {
  $info = array();
  if ($civicrm_entity == 'contact') {
    $info['civi_user'] = array(
      'label' => 'Drupal User',
      'type' => 'user',
    );
  }
  $fields = civicrm_api($civicrm_entity, 'getfields', array(
    'version' => 3,
    'action' => 'create',
  ));
  foreach ($fields['values'] as $fieldname => $field_specs) {

    // Type is empty for custom fields - we should sort that out but
    // skipping for now we are only doing 'integers' at this stage.
    $types = array(
      1 => 'integer',
      2 => 'text',
      32 => 'text',
      16 => 'integer',
    );
    if (!empty($field_specs['type']) && array_key_exists($field_specs['type'], $types)) {
      $info[$fieldname] = array(
        'label' => _civicrm_entity_get_title($field_specs),
        'type' => $types[$field_specs['type']],
        'sanitize' => 'check_plain',
        'setter callback' => 'entity_property_verbatim_set',
      );
      if (!empty($field_specs['api.required'])) {
        $info[$fieldname]['required'] = TRUE;
      }
      if ($field_specs['type'] == 16) {
        $info[$fieldname]['size'] = 'tiny';
      }

      // This is a semi-reliable way of distinguishing 'real' fields
      // from pseudo fields and custom fields and impacts on views
      // (which is only implemented in a very minor way at this stage
      // because it is 'blocked' by the default views install using
      // hook_views_data rather than hook_views_data_alter.
      if (!empty($field_specs['name'])) {
        $info[$fieldname]['schema field'] = $field_specs['name'];
      }

      // We will add contact as a related entity for FK references to
      // contact. This could be expanded to all FKs e.g event_id in
      // Participant. Could load the event at the moment we are being
      // cautious.
      if (CRM_Utils_Array::value('FKClassName', $field_specs)) {
        $fks = _civicrm_entity_chained_fks();
        if (array_key_exists($field_specs['FKClassName'], $fks)) {
          $fks_entity = $fks[$field_specs['FKClassName']];
          $info[$fieldname . '_' . $fks_entity] = array(
            'label' => _civicrm_entity_get_title($field_specs),
            'type' => 'civicrm_' . $fks_entity,
            'property_info' => array(
              'field' => $fieldname,
              'entity' => $fks_entity,
            ),
            'getter callback' => 'civicrm_entity_metadata_civicrm_entity_get_properties',
          );
        }
      }

      // @TODO We are treating contact as the only possible entity
      // which is not great - need to figure out better approach - can
      // we have more than one? Define 'civicrm_entity'?
      if ($fieldname == 'entity_id') {
        $fks_entity = 'contact';
        $info[$fieldname . '_' . $fks_entity] = array(
          'label' => _civicrm_entity_get_title($field_specs),
          'type' => 'civicrm_' . $fks_entity,
          'property_info' => array(
            'field' => $fieldname,
            'entity' => $fks_entity,
          ),
          'getter callback' => 'civicrm_entity_metadata_civicrm_entity_get_properties',
        );
      }
      if (!empty($field_specs['options'])) {

        // $info[$fieldname]['type'] = 'list<integer>';
        $info[$fieldname]['options list'] = '_civicrm_entity_rules_attach_options';
        $info[$fieldname]['options data'] = $field_specs['options'];
        if ($context == 'property_info') {
          $info[$fieldname]['property defaults']['options list'] = $field_specs['options'];
        }
      }
      $info['type'] = array(
        'label' => t('Type'),
        'description' => t('Dummy field for bundle key'),
        'type' => 'token',
        'setter callback' => 'entity_property_verbatim_set',
        'required' => FALSE,
        'property defaults' => array(
          'civicrm_' . strtolower($civicrm_entity),
        ),
      );
    }
  }
  return $info;
}