You are here

function _file_feeds_atom_set_target_element_value_alter in Feeds Atom 7

Helper function for hook_feeds_atom_set_target_element_value_alter().

Drupal 7 splits image and file fields. However, the needed hook logic is the same for both.

2 calls to _file_feeds_atom_set_target_element_value_alter()
file_feeds_atom_set_target_element_value_alter in ./feeds_atom.module
@todo, update this for D7
image_feeds_atom_set_target_element_value_alter in ./feeds_atom.module
Implementation of hook_feeds_atom_set_target_element_value_alter().

File

./feeds_atom.module, line 165
Contains the main functionality for feeds_atom.

Code

function _file_feeds_atom_set_target_element_value_alter(&$value, $field_name, $field_type = 'file') {

  // Use static variables in combination with the FeedsEnclosureUnique class
  // to ensure that imported files are not downloaded more than once.
  static $enclosures = array();
  static $files = array();
  $field_info = field_info_field($field_name);
  if (!empty($value) && !empty($field_info['type']) && $field_info['type'] == $field_type) {
    foreach ($value as $language_key => $field_language_instances) {
      if ($language_key[0] === '#') {
        continue;
      }
      foreach ($field_language_instances as $i => $instance) {

        // This is only the case if the field in question is a filefield and
        // not the #attributes element.
        if (!empty($instance['full_url'])) {
          if (empty($enclosures[$instance['full_url']])) {

            // @todo D7 isn't actually doing anything yet to ensure uniqueness.
            $enclosures[$instance['full_url']] = new FeedsEnclosureUnique($instance['full_url'], $instance['filemime']);

            // @todo can't hardcode public
            $files[$instance['full_url']] = $enclosures[$instance['full_url']]
              ->getFile('public://');
          }
          $value[$language_key][$i] = (array) $files[$instance['full_url']] + $value[$language_key][$i];
        }
      }
    }
  }
}