You are here

function _sf_entity_import_preprocess_entity in Salesforce Suite 7.2

Helper function to populate a new entity with other properties besides the stub properties.

Parameters

object $entity: The entity object, passed by reference so it can be modified by this function.

string $entity_type: The entity type for the entity being created.

string $bundle_name: Name of the bundle for the given entity. (Optional)

Return value

void

1 call to _sf_entity_import_preprocess_entity()
sf_entity_import in sf_entity/sf_entity.module
Imports data from Salesforce into a Drupal entity

File

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

Code

function _sf_entity_import_preprocess_entity(&$entity, $entity_type, $bundle_name = NULL) {

  // Loads the information about all entity types.
  $entities = entity_get_info();
  $base_tables = array();
  foreach ($entities as $ent_type => $ent) {
    $base_tables[$ent_type] = $ent['base table'];
  }
  if (is_object($entity)) {

    // Loads the schema for the entity to get core fields.
    // @todo: Collect from $entity['schema_fields_sql'] to pull from tables besides base table.
    $schema = drupal_get_schema($base_tables[$entity_type]);
    if (isset($schema['fields'])) {
      $core_fields = array_keys($schema['fields']);
    }

    // If there is a $bundle_name set, then load the FieldAPI fields for this entity also.
    if (isset($bundle_name)) {
      $fieldapi_fields = array_keys(field_info_instances($entity_type, $bundle_name));
    }

    // Set the fields on the entity itself.
    if (isset($core_fields) && is_array($core_fields)) {
      foreach ($core_fields as $fieldname) {
        $entity->{$fieldname} = '';
      }
    }
    if (isset($fieldapi_fields) && is_array($fieldapi_fields)) {
      foreach ($fieldapi_fields as $fieldname) {

        // Note: This only works for sf_entity_import() since the actual field array structure
        // is set in the sf_entity_field_import_default() function.
        $entity->{$fieldname} = '';
      }
    }
  }
  if ($entity_type == 'node') {
    if (!empty($bundle_name)) {

      // If a bundle name is set, then set it as the node type.
      $entity->type = $bundle_name;

      // After that node_object_prepare() can be called to set default values.
      node_object_prepare($entity);
    }

    // Status property must be set.
    if (!property_exists($entity, 'status')) {
      $entity->status = 1;
    }

    // To save a node, a uid must be set.
    // (Otherwise the comment_node_insert() call will fail, if comment.module is enabled.)
    // To do this correctly, the UID must be pulled from a fieldmap of some sort, but that would require
    // another Salesforce API function, to match the supplied sfid, without also providing a fieldmap.
    if (!isset($entity->uid) || empty($entity->uid)) {

      // @todo: Provide a variable to set the proper uid.
      // For now, just save the node with anonymous ownership
      // (since that is the only uid guaranteed to exist).
      $entity->uid = 0;
    }
  }
  elseif ($entity_type == 'taxonomy_term') {
    $vocab_data = taxonomy_vocabulary_machine_name_load($bundle_name);
    $vid = $vocab_data->vid;
    $entity->vid = $vid;
  }
}