You are here

function _commerce_file_field_widget_process in Commerce File 7

An element #process callback for the commerce_file_commerce_file field type.

  • add file widget styles
  • determine if there is a default file scheme available
1 string reference to '_commerce_file_field_widget_process'
commerce_file_field_widget_form in includes/commerce_file.field.inc
Implements hook_field_widget_form().

File

includes/commerce_file.field.inc, line 429
Implement an commerce_file field, based on the file module's file field.

Code

function _commerce_file_field_widget_process($element, &$form_state, $form) {
  $item = $element['#value'];
  $item['fid'] = $element['fid']['#value'];
  $field = field_widget_field($element, $form_state);
  $instance = field_widget_instance($element, $form_state);

  // add styles for file widget
  $element['#theme'] = 'file_widget';
  $element['#attached']['css'][] = drupal_get_path('module', 'commerce_file') . '/css/commerce_file.forms.css';
  if (isset($element['#attributes']['class'])) {
    $element['#attributes']['class'] = array_merge($element['#attributes']['class'], array(
      'file-commerce-file',
    ));
  }
  else {
    $element['#attributes']['class'] = array(
      'file-commerce-file',
    );
  }

  // only check scheme if not any field generated on install
  if (!in_array($instance['field_name'], _commerce_file_get_field_names())) {

    // get scheme options
    $available_scheme_options = _commerce_file_get_private_stream_wrappers_options();
    $scheme_options = _commerce_file_get_private_stream_wrappers_options(array(
      $field['settings']['uri_scheme'],
    ));

    // Warn if there is no scheme selected for this field
    $default_scheme = NULL;
    if (empty($item['fid']) && empty($scheme_options)) {
      $element['#disabled'] = TRUE;
      $error_msg = t('You cannot upload a file. Commerce File requires a private file scheme. Visit <a href="!url">admin/config/media/file-system</a> to set your private file path. Optionally, a private scheme other than Drupal\'s may be implemented.', array(
        '!url' => url('admin/config/media/file-system'),
      ));
      if (!empty($available_scheme_options)) {
        $error_msg .= t('<br />There are private file schemes available on your system. Visit the field settings to select a private scheme allowed for this field.');
      }
      form_error($element, $error_msg);
    }
  }
  return $element;
}