You are here

function sf_entity_fieldmap_objects in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 7 sf_entity/sf_entity.module \sf_entity_fieldmap_objects()

File

sf_entity/sf_entity.module, line 305
Integrates fieldable entities with the Salesforce API.

Code

function sf_entity_fieldmap_objects($type) {
  $objects = array();

  // Define the data fields available for Drupal objects.
  if ($type == 'drupal') {
    $entities = field_info_bundles();

    // For each entity-bundle-field-column combo, assign a field definition.
    foreach ($entities as $entity_name => $bundles) {
      $entity_info = entity_get_info($entity_name);
      if (!$entity_info['fieldable']) {
        continue;
      }
      $objects[$entity_name] = array();
      foreach ($bundles as $bundle_name => $bundle_info) {
        $objects[$entity_name][$bundle_name] = array(
          'label' => $entity_info['label'] . ': ' . $bundle_info['label'],
          'fields' => array(),
        );

        // Add entity keys (id, revision, & bundle).
        foreach ($entity_info['entity keys'] as $key => $value) {
          if (empty($value)) {
            continue;
          }
          $objects[$entity_name][$bundle_name]['fields'][$value] = array(
            'label' => $value,
            'group' => 'IDs',
            'type' => SALESFORCE_FIELD_SOURCE_ONLY,
          );
        }

        // Add uid if it is available.
        if ($entity_name == 'node' || $entity_name == 'user') {
          $objects[$entity_name][$bundle_name]['fields']['uid'] = array(
            'label' => 'uid',
            'group' => 'IDs',
            'type' => SALESFORCE_FIELD_SOURCE_ONLY,
          );
        }

        // For each Field API field column, add a definition.
        $fields = field_info_instances($entity_name, $bundle_name);
        foreach ($fields as $field_name => $field_info) {
          $more_field_info = field_info_field($field_name);
          foreach ($more_field_info['columns'] as $col_name => $col_data) {

            // There's probably no reason to clutter the admin UI with the
            // "format" value for this field.
            if ($col_name == 'format') {
              continue;
            }

            // Set the export and import handler based on the field type.
            // Currently only standard Field API fields and reference fields are supported.
            switch ($more_field_info['type']) {
              case 'taxonomy_term_reference':
                $export_handler = '_sf_entity_export_termreference';
                $import_handler = '_sf_entity_import_termreference';
                break;
              case 'node_reference':
                $export_handler = '_sf_entity_export_nodereference';
                $import_handler = '_sf_entity_import_nodereference';
                break;
              case 'user_reference':
                $export_handler = '_sf_entity_export_userreference';
                $import_handler = '_sf_entity_import_userreference';
                break;
              default:
                $export_handler = 'sf_entity_export_field_default';
                $import_handler = 'sf_entity_import_field_default';
                break;
            }
            $data = array(
              'label' => t('@label (@column)', array(
                '@label' => $field_info['label'],
                '@column' => $col_name,
              )),
              'group' => $bundle_info['label'] . ' Fields',
              'export' => $export_handler,
              'import' => $import_handler,
            );
            $key = $field_name . ':' . $col_name;
            $objects[$entity_name][$bundle_name]['fields'][$key] = $data;
          }
        }

        // Add core fields which are outside of Field API.
        switch ($entity_name) {
          case 'node':
            $node_type = node_type_load($bundle_name);
            if ($node_type->has_title) {
              $objects['node'][$bundle_name]['fields']['title'] = array(
                'label' => $node_type->title_label,
                'group' => 'Node Fields',
              );
            }
            $objects['node'][$bundle_name]['fields'] += array(
              'type' => array(
                'label' => t('Node type'),
                'group' => 'Node Fields',
              ),
              'status' => array(
                'label' => t('Is the node published?'),
                'group' => 'Node Fields',
              ),
              'promote' => array(
                'label' => t('Is the node promoted?'),
                'group' => 'Node Fields',
              ),
              'name' => array(
                'label' => t('Author\'s username'),
                'group' => 'Node Fields',
                'type' => SALESFORCE_FIELD_SOURCE_ONLY,
              ),
              'log' => array(
                'label' => t('Log message for current revision'),
                'group' => 'Node Fields',
              ),
            );
            $objects['node'][$bundle_name]['fields']['created'] = array(
              'label' => 'Created timestamp',
              'export' => '_sf_entity_export_date',
              'import' => '_sf_entity_import_date',
              'group' => 'Node Fields',
            );
            $objects['node'][$bundle_name]['fields']['changed'] = array(
              'label' => 'Last changed timestamp',
              'export' => '_sf_entity_export_date',
              'import' => '_sf_entity_import_date',
              'group' => 'Node Fields',
            );

            // @todo: Write export/import handlers for this field
            //        More long-term: Figure out how to handle translation sets, and translatable fields.
            $objects['node'][$bundle_name]['fields']['language'] = array(
              'label' => 'Language of node content',
              'group' => 'Node Fields',
            );
            $objects['node'][$bundle_name]['fields']['mail'] = array(
              'label' => 'Author\'s e-mail address',
              'export' => '_sf_entity_export_author_email',
              'group' => 'Node Fields',
              'type' => SALESFORCE_FIELD_SOURCE_ONLY,
            );
            break;
          case 'user':
            $objects['user'][$bundle_name]['fields'] += array(
              'name' => array(
                'label' => t('Username'),
                'group' => 'User Fields',
              ),
              'pass' => array(
                'label' => t('Password'),
                'group' => 'User Fields',
              ),
              'mail' => array(
                'label' => t('E-mail address'),
                'group' => 'User Fields',
              ),
              'status' => array(
                'label' => t('Account status'),
                'group' => 'User Fields',
              ),
              'picture' => array(
                'label' => t('Picture'),
                'group' => 'User Fields',
              ),
            );
            $objects['user'][$bundle_name]['fields']['created'] = array(
              'label' => 'Created timestamp',
              'export' => '_sf_entity_export_date',
              'import' => '_sf_entity_import_date',
              'group' => 'User Fields',
            );
            $objects['user'][$bundle_name]['fields']['access'] = array(
              'label' => 'Last access timestamp',
              'export' => '_sf_entity_export_date',
              'import' => '_sf_entity_import_date',
              'group' => 'User Fields',
            );
            $objects['user'][$bundle_name]['fields']['login'] = array(
              'label' => 'Last login timestamp',
              'export' => '_sf_entity_export_date',
              'import' => '_sf_entity_import_date',
              'group' => 'User Fields',
            );
            break;
        }
      }
    }
  }
  return $objects;
}