You are here

function commerce_file_field_widget_form in Commerce File 7

Implements hook_field_widget_form().

File

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

Code

function commerce_file_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Add display_field setting to field because file_field_widget_form() assumes it is set.
  $field['settings']['display_field'] = 0;

  // ensure uri scheme on license file field if no items
  $controlled_field_names = _commerce_file_get_field_names();
  if (empty($items) && empty($field['settings']['uri_scheme']) && $instance['field_name'] == $controlled_field_names['license_file']) {
    $available_scheme_options = _commerce_file_get_private_stream_wrappers_options();

    // set uri_scheme to first available
    $field['settings']['uri_scheme'] = key($available_scheme_options);
  }

  // build elements with file_field
  $elements = file_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);

  // add properties to each element for widget process
  foreach (element_children($elements) as $delta) {
    $elements[$delta]['#entity_type'] = $instance['entity_type'];
    $elements[$delta]['#value_callback'] = '_commerce_file_field_widget_value';
    $elements[$delta]['#process'][] = '_commerce_file_field_widget_process';
    if ($instance['entity_type'] == COMMERCE_FILE_LICENSE_ENTITY_NAME) {
      $elements[$delta]['#inheritable'] = FALSE;
    }

    // add license limit settings
    $element_settings = isset($elements[$delta]['#default_value']) ? $elements[$delta]['#default_value'] : array();
    _commerce_file_field_add_license_elements($elements[$delta], 'widget', $element_settings, $instance['settings']);
  }
  return $elements;
}