You are here

function asset_field in Asset 5.2

Same name and namespace in other branches
  1. 5 modules/asset_content.inc \asset_field()
  2. 6 modules/asset_content.inc \asset_field()

Implementation of hook_field().

File

modules/asset_content.inc, line 71

Code

function asset_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      if ($field['allowed_extensions']) {
        $allowed_extensions = array_map('trim', explode(',', $field['allowed_extensions']));
      }
      foreach ($items as $delta => $item) {
        $error_field = $field['field_name'] . '][' . $delta . '][aid';
        if (!$item['aid']) {
          continue;
        }
        $asset = asset_load($item['aid']);

        // make sure selected asset is allowed type
        if (is_array($field['allowed_types']) && !$field['allowed_types'][$asset->type]) {
          form_set_error($error_field, t('The selected asset type is not allowed.'));
        }

        // make sure selected asset is allowed extension
        if ($asset->type == 'file' && $allowed_extensions) {
          $info = pathinfo($asset->file['filename']);
          if (!in_array($info['extension'], $allowed_extensions)) {
            form_set_error($error_field, t('The selected asset is not one of the allowed extensions.'));
          }
        }

        // make sure asset supports one of the valid formatters
        if ($field['valid_formatters']) {
          $valid = FALSE;
          foreach ($field['valid_formatters'] as $format) {
            if ($asset->formatters[$format]) {
              $valid = TRUE;
              break;
            }
          }
          if (!$valid) {
            form_set_error($error_field, t('Asset does not support the specified formatters.'));
          }
        }
      }
      break;
    case 'view':
      break;
  }
}