You are here

function media_field_validate in D7 Media 7

Implements hook_field_validate().

Possible error codes:

  • 'media_remote_file_type_not_allowed': The remote file is not an allowed file type.

File

includes/media.fields.inc, line 319
: Provides a "Multimedia asset" field to the fields API

Code

function media_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) {
  $allowed_types = array_keys(array_filter($instance['widget']['settings']['allowed_types']));

  // @TODO: merge in stuff from media_uri_value
  foreach ($items as $delta => $item) {
    if (empty($item['fid'])) {
      return TRUE;

      //@TODO: make support for submiting with just a URI here?
    }
    $file = file_load($item['fid']);

    // Only validate allowed types if the file is remote and not local.
    if (!file_entity_file_is_local($file)) {
      if (!in_array($file->type, $allowed_types)) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'media_remote_file_type_not_allowed',
          'message' => t('%name: Only remote files with the following types are allowed: %types-allowed.', array(
            '%name' => t($instance['label']),
            '%types-allowed' => !empty($allowed_types) ? implode(', ', $allowed_types) : t('no file types selected'),
          )),
        );
      }
    }
  }
}