You are here

function media_feeds_set_target in Media Feeds 7

Same name and namespace in other branches
  1. 7.2 media_feeds.module \media_feeds_set_target()

The actual mapping happens here.

1 string reference to 'media_feeds_set_target'
media_feeds_feeds_processor_targets_alter in ./media_feeds.module
Implements hook_feeds_processor_targets_alter().

File

./media_feeds.module, line 48
Implementation of the Feeds mapping API for the media module.

Code

function media_feeds_set_target($source, $entity, $target, $values, $config = array()) {
  if (empty($values)) {
    return;
  }
  if (!is_array($values)) {
    $values = array(
      $values,
    );
  }
  $field_info = field_info_field($target);
  $field = array(
    LANGUAGE_NONE => array(),
  );

  // Get the provider class name by splitting $target.
  // MediaFeedsInternetProvider is the default for the secound value.
  list($target, $provider) = explode(':', $target, 2) + array(
    1 => 'MediaFeedsInternetProvider',
  );
  foreach ($values as $value) {
    try {

      // Find a provider to create a file object.
      $provider = new $provider($value, $config);

      // Validate and get a file object.
      $provider
        ->validate();
      $file = $provider
        ->getFileObject();
      if (!$file) {
        drupal_set_message(t('Failed to get the file object for %value.', array(
          '%value' => (string) $value,
        )));
        continue;
      }

      // Look for the field instance settings.
      $instance = field_info_instance($entity->feeds_item->entity_type, $target, $entity->type);
      $allowed_types = $instance['widget']['settings']['allowed_types'];
      $allowed_schemes = $instance['widget']['settings']['allowed_schemes'];

      // Validate the type.
      $errors = media_file_validate_types($file, $allowed_types);
      if (count($errors)) {
        foreach ($errors as $error) {
          drupal_set_message(filter_xss($error), 'error');
        }
        continue;
      }

      // Validate the URI scheme.
      // If public is allowed, also allow schemes supported by
      // MediaInternetFileHandler (http, ...).
      if (in_array('public', $allowed_schemes)) {
        $fromurlschemes = media_variable_get('fromurl_supported_schemes');
        foreach ($fromurlschemes as $fromurlscheme) {
          $allowed_schemes[$fromurlscheme] = $fromurlscheme;
        }
      }
      $scheme = file_uri_scheme($file->uri);
      if (!in_array($scheme, $allowed_schemes)) {
        drupal_set_message(t('Scheme %scheme not allowed for %target.', array(
          '%scheme' => $scheme . '://',
          '%target' => $target,
        )), 'error');
        continue;
      }

      // Save file.
      $file = $provider
        ->save();
      if (!$file) {
        drupal_set_message(t('Failed to save the file.'), 'error');
        continue;
      }

      // Apply author settings to the file.
      if (empty($file->uid) && !empty($entity->uid) || $file->uid != $entity->uid) {
        $file->uid = $entity->uid;
        if (!file_save($file)) {
          drupal_set_message(t('Failed to apply the author settings to the file.'), 'warning');
        }
      }

      // Attach the file to the field.
      $field[LANGUAGE_NONE][]['fid'] = $file->fid;
    } catch (MediaInternetNoHandlerException $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    } catch (MediaInternetValidationException $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
  }
  $entity->{$target} = $field;
}