You are here

private function RelationFeedsProcessor::mapEndpoints in Relation 7

Map endpoints to entity keys array

Parameters

FeedsSource $source: Source information about this import.

FeedsParserResult $result: The result of the parsing stage.

Return value

array An array of mapped endpoint items

2 calls to RelationFeedsProcessor::mapEndpoints()
RelationFeedsProcessor::existingEntityId in relation_feeds/RelationFeedsProcessor.inc
RelationFeedsProcessor::map in relation_feeds/RelationFeedsProcessor.inc

File

relation_feeds/RelationFeedsProcessor.inc, line 74
Class definition of RelationFeedsProcessor.

Class

RelationFeedsProcessor
Creates relations from feed items.

Code

private function mapEndpoints(FeedsSource $source, FeedsParserResult $result) {
  $field = array();
  static $sources;
  if (!isset($sources)) {
    foreach ($this->config['mappings'] as $map) {

      // Each target can have multiple sources.
      $sources[$map['target']][] = $map['source'];
    }
  }
  $type = $this
    ->getTypeInfo();
  if ($type->directional) {
    $endpoint_types = array(
      'source_bundles',
      'target_bundles',
    );
  }
  else {
    $endpoint_types = array(
      'source_bundles',
    );
  }
  $item = $result
    ->currentItem();
  foreach ($endpoint_types as $endpoint_type) {
    foreach ($type->{$endpoint_type} as $endpoint) {
      $endpoint = explode(':', $endpoint);

      // We're using GUID
      if (isset($sources[$endpoint_type . ':' . $endpoint[0] . ':' . $endpoint[1] . ':guid'])) {
        $endpoint_sources = $sources[$endpoint_type . ':' . $endpoint[0] . ':' . $endpoint[1] . ':guid'];
        $guids = array();
        foreach ($endpoint_sources as $endpoint_source) {
          if (empty($item[$endpoint_source])) {
            throw new FeedsValidationException(t('Missing GUID at source @source.', array(
              '@source' => $endpoint_source,
            )));
          }
          $guids[] = $item[$endpoint_source];
        }
        $entity_feeds = db_select('feeds_item', 'f')
          ->condition('guid', $guids)
          ->condition('entity_type', $endpoint[0])
          ->fields('f', array(
          'guid',
          'entity_id',
          'entity_type',
        ))
          ->execute()
          ->fetchAllAssoc('guid');
        foreach ($guids as $guid) {
          if (!isset($entity_feeds[$guid])) {
            throw new FeedsValidationException(t('GUID @guid was not found.', array(
              '@guid' => $guid,
            )));
          }
          $field[] = array(
            'entity_type' => $entity_feeds[$guid]->entity_type,
            'entity_id' => $entity_feeds[$guid]->entity_id,
          );
        }
      }

      // We're using entity ID
      if (isset($sources[$endpoint_type . ':' . $endpoint[0] . ':' . $endpoint[1] . ':entity_id'])) {
        $endpoint_sources = $sources[$endpoint_type . ':' . $endpoint[0] . ':' . $endpoint[1] . ':entity_id'];
        foreach ($endpoint_sources as $endpoint_source) {
          if (empty($item[$endpoint_source])) {
            throw new FeedsValidationException(t('Missing entity id at source @source.', array(
              '@source' => $endpoint_source,
            )));
          }
          $field[] = array(
            'entity_type' => $endpoint[0],
            'entity_id' => $item[$endpoint_source],
          );
        }
      }
    }
  }
  return $field;
}