You are here

function _entityreference_feeds_get_targets in Entity reference feeds 7

@file This is the main module file for entity reference feeds.

2 calls to _entityreference_feeds_get_targets()
entityreference_feeds_entity_presave in ./entityreference_feeds.module
Implements hook_entity_presave().
entityreference_feeds_feeds_processor_targets_alter in ./entityreference_feeds.module
Implements hook_feeds_processor_targets_alter().

File

./entityreference_feeds.module, line 7
This is the main module file for entity reference feeds.

Code

function _entityreference_feeds_get_targets($field_name, $target_type, $target_bundle) {
  $targets_cache =& drupal_static('entityreference_feeds_feeds_processor_targets', array());
  $entity_info = entity_get_info($target_type);
  if (!isset($targets_cache[$field_name][$target_type][$target_bundle])) {
    $info = array(
      'bundle' => $target_bundle,
    );
    $_targets = array();
    $target_key = $field_name . ':' . $target_bundle;
    $wrapper = entity_metadata_wrapper($target_type, NULL, $info);

    // @todo: maybe restrict to data types feeds can deal with.
    foreach ($wrapper
      ->getPropertyInfo() as $name => $property_info) {

      /* We leave fields to feeds mappers in accordance with the principle of
            least astonishment. They are also more forgiving and "robust" in some
            cases, for file/image-fields for example. Node targets for properties
            like "title" etc are inaccessable to us as they are found in the
            node-processor. Besides, using entity-api for this is a much more
            generic approach and will work for more entity-types than just node
         */
      if (!isset($property_info['field']) || !$property_info['field']) {
        if (!empty($property_info['setter callback'])) {
          $_targets[$target_key . ':entityreference_feeds:' . $name] = array(
            'name' => t('@field_name (@bundle: @target)', array(
              '@field_name' => $field_name,
              '@target' => $property_info['label'],
              '@bundle' => $target_bundle,
            )),
            'description' => isset($property['description']) ? t('@field_name: @target in @type type @bundle', array(
              '@field_name' => $field_name,
              '@target' => $property_info['label'],
              '@type' => $target_type,
              '@bundle' => $target_bundle,
            )) : NULL,
            'callback' => 'entityreference_feeds_feeds_set_target',
          );
        }
      }
    }

    // Add general GUID target.
    $_targets[$target_key . ':entityreference_feeds:guid'] = array(
      'name' => t('@field_name (@bundle: @target)', array(
        '@field_name' => $field_name,
        '@target' => t('GUID'),
        '@bundle' => $target_bundle,
      )),
      'description' => t('The globally unique identifier of the item. E. g. the feed item GUID in the case of a syndication feed.'),
      //'real_target' => $field_name,

      //'optional_unique' => FALSE, //@todo set this to false?
      'callback' => 'entityreference_feeds_feeds_set_target',
    );

    // @todo: this is ugly as hell, but needed because of limitations of
    // drupal_alter (taking a maximum of 3 parameters). Should do the job
    // though, hopefully with no side-effects
    $entity_targets = array(
      'entityreference_feeds_processed' => array(),
    );

    // Let other modules expose mapping targets.
    $entity_targets += module_invoke_all('feeds_processor_targets', $target_type, $target_bundle);
    drupal_alter('feeds_processor_targets', $entity_targets, $target_type, $target_bundle);
    foreach ($entity_targets as $target_target => $entity_target) {
      if (isset($entity_target['name'])) {
        $entity_target['name'] = t('@field_name (@bundle: @target)', array(
          '@field_name' => $field_name,
          '@target' => $target_target,
          '@bundle' => $target_bundle,
        ));
      }
      if (isset($entity_target['description'])) {
        $entity_target['description'] = t('@field_name: @target in @type type @bundle', array(
          '@field_name' => $field_name,
          '@target' => $target_target,
          '@type' => $target_type,
          '@bundle' => $target_bundle,
        ));
      }
      $callback = isset($entity_target['callback']) ? $entity_target['callback'] : 'entityreference_feeds';
      $entity_target['callback'] = 'entityreference_feeds_feeds_set_target';
      $_targets[$target_key . ':' . $callback . ':' . $target_target] = $entity_target;
    }
    if (!isset($targets_cache[$field_name])) {
      $targets_cache[$field_name] = array();
    }
    if (!isset($targets_cache[$field_name][$target_type])) {
      $targets_cache[$field_name][$target_type] = array();
    }
    $targets_cache[$field_name][$target_type][$target_bundle] = $_targets;
  }
  return $targets_cache[$field_name][$target_type][$target_bundle];
}