2

What is the proper way to expand feeds to import the entities themselves instead of the values for already existing entities (like a node type). For example let's say we have various forms collected in a store that need to be synced with on outside database. It is simple to use a custom processor to do this manually, but what is the proper way to pull the information from the feed programmatically.

We obviously have to declare what the variables being mapped by the parser (which we are going to use) will be.

// Mapping Variables
public function getMappingTargets() {

    $targets = parent::getMappingTargets();
    $targets += array(
      'test_machine_name' => array(
        'name' => t('Machine Name'), 
        'description' => t('machine name.'), 
        'callback' => 'mymodule_set_target',
        'optional_unique' => TRUE, 
      ),
      'test_label' => array(
        'name' => t('Label'), 
        'description' => t('This will be the label.'),
        'callback' => 'mymodule_set_target', 
      ), 
    ); 
    return $targets;
  }

The problem comes in when trying to use those mapped values from the parser when saving the entity.

// Create a new Entity
    public function newEntity(FeedsSource $source) {
          $feeder = $this->getMappingTargets();

      $field_collection = new stdClass(); 
      $field_collection->id = $feeder['test_machine_name']; 
      $field_collection->title = $feeder['test_label']; 
      $field_collection->description = ''; 
          $field_collection->machine_name = $feeder['test_machine_name'];
          $field_collection->log = 'Created by Importer';
      return $field_collection; 
    }

    // Load an existing Entity
    public function entityLoad(FeedsSource $source, $entity_id) {
          if ($this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
                $field_collection = field_collection_load($entity_id);
                $field_collection->log = 'Updated by Importer';
              }
              else {
                $field_collection->log = 'Replaced by Importer';
              }
              return $field_collection; 
    }

// Save Entity
public function entitySave($entity) {
  $t = get_t();

  $fields_array = array(
    array(
      'field' => array(
        'field_name' => $entity->machine_name,
        'label' => $t($entity->title),
        'cardinality' => -1,
        'type' => 'field_collection',
      ),
      'instance' => array(
        'field_name' => $entity->machine_name,
        'entity_type' => '$type',
        'bundle' => '$bundle',
        'label' => $t($entity->title),
        'description' => '',
        'widget' => array('type' => 'field_collection_embed'),
        'required' => 1,
      ),
    ),
  );

  // Loop through fields array and create field and instance
  foreach ($fields_array as $field) {
    // Check if field already exists
    if (!field_info_field($field['field']['field_name'])) {
      field_create_field($field['field']);
    }

    // Check if instance exists
    if (!field_info_instance($field['instance']['entity_type'], $field['instance']['field_name'], $field['instance']['bundle'])) {
      field_create_instance($field['instance']);
    }
  }   
}

Everything saves properly and is imported into the correct places, but the values from the mapped fields are never pulled. Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.