1 file.field.inc file_field_inline_fields_widget(array $element, array &$form_state, array $instance)

Adds the additional file entity fields to the widget.

Parameters

array $element: The element being processed.

array $form_state: A keyed array containing the current state of the form.

array $instance: The field instance.

Return value

array: The element with fields attached, or just the element if no fields to be attached.

File

core/modules/file/file.field.inc, line 887
Field module functionality for the File module.

Code

function file_field_inline_fields_widget(array $element, array &$form_state, array $instance) {
  $element['#process'] = array_merge($element['#process'], array('file_field_inline_fields_widget_process'));

  // Only act on entity forms, and when a file has been uploaded.
  $file = (object) $element['#default_value'];
  if (empty($file->fid) || !isset($element['#entity_type']) || !isset($element['#field_name'])) {
    return $element;
  }

  // Sorry, no recursive inline-editing of files. ;)
  if ($element['#entity_type'] === 'file') {
    return $element;
  }

  // Load the settings for this field.
  $widget_settings = $instance['widget']['settings'];

  // Bail out if inline file entities are not enabled on this field.
  if (!isset($widget_settings['inline_fields_option']) || $widget_settings['inline_fields_option'] == FILE_INLINE_FIELDS_NONE) {
    return $element;
  }

  // File Entity adds some extra requirements that will not be added
  // by the Plupload module, so manually append all values from the file_managed
  // table to the $file object.
  if (!isset($file->type)) {
    // Fill out the file object so that it has all of the expected data.
    $query = db_query('SELECT * FROM {file_managed} WHERE fid = :fid', array(
      ':fid' => $file->fid,
    ));
    $results = $query->fetchAssoc();
    if (!empty($results)) {
      foreach ($results as $key => $val) {
        $file->{$key} = $val;
      }
    }
  }

  // Add the entity form for the file.
  $element['entity'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array('file-inline-fields'),
    ),
    '#indentation' => ($instance['widget']['type'] == 'file_generic') ? 2 : 0,
    '#weight' => 10,
    '#parents' => array('files', $file->fid),
  );
  $file = file_load($file->fid);
  field_attach_form('file', $file, $element['entity'], $form_state, $element['#language']);

  // Reduce the display of fields if only display specific fields.
  if (isset($widget_settings['inline_fields_option']) && $widget_settings['inline_fields_option'] == FILE_INLINE_FIELDS_SOME) {
    $disabled_fields = array_diff(element_children($element['entity']), $widget_settings['inline_fields']);
    foreach ($disabled_fields as $form_key) {
      $element['entity'][$form_key]['#access'] = FALSE;
    }
  }

  return $element;
}