You are here

function mediafield_field_validate in D7 Media 7.4

Same name and namespace in other branches
  1. 7.2 modules/mediafield/mediafield.module \mediafield_field_validate()
  2. 7.3 modules/mediafield/mediafield.module \mediafield_field_validate()

Implements hook_field_validate().

Possible error codes:

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

File

modules/mediafield/mediafield.module, line 187
Provide a "Multimedia asset" field.

Code

function mediafield_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'),
          )),
        );
      }
    }
  }
}