You are here

function file_entity_edit in File Entity (fieldable files) 7.3

Same name and namespace in other branches
  1. 7 file_entity.pages.inc \file_entity_edit()
  2. 7.2 file_entity.pages.inc \file_entity_edit()

Page callback: Form constructor for the file edit form.

Path: file/%file/edit

@todo Rename this form to file_edit_form to ease into core.

Parameters

object $file: A file object from file_load().

See also

file_entity_menu()

3 string references to 'file_entity_edit'
file_entity_menu in ./file_entity.module
Implements hook_menu().
file_entity_metadata_form_file in ./file_entity.module
Entity API callback to get the form of a file entity.
file_entity_page_edit in ./file_entity.pages.inc
Page callback for the file edit form.

File

./file_entity.pages.inc, line 773
Supports file operations including View, Edit, and Delete.

Code

function file_entity_edit($form, &$form_state, $file) {
  drupal_set_title(t('<em>Edit @type</em> @title', array(
    '@type' => $file->type,
    '@title' => $file->filename,
  )), PASS_THROUGH);
  $form_state['file'] = $file;
  $form['#attributes']['class'][] = 'file-form';
  if (!empty($file->type)) {
    $form['#attributes']['class'][] = 'file-' . $file->type . '-form';
  }

  // Basic file information.
  // These elements are just values so they are not even sent to the client.
  foreach (array(
    'fid',
    'type',
    'uid',
    'timestamp',
  ) as $key) {
    $form[$key] = array(
      '#type' => 'value',
      '#value' => isset($file->{$key}) ? $file->{$key} : NULL,
    );
  }
  $form['filename'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $file->filename,
    '#required' => TRUE,
    '#maxlength' => 255,
    '#weight' => -10,
  );

  // Add a 'replace this file' upload field if the file is writeable.
  if (file_entity_file_is_writeable($file)) {

    // Set up replacement file validation.
    $replacement_options = !empty($form_state['#upload_options']) ? $form_state['#upload_options'] : array();

    // The replacement file must have an extension valid for the original type.
    $file_extensions = array();
    $file_type_name = isset($file->type) ? $file->type : file_get_type($file);
    if (!empty($replacement_options['file_extensions'])) {
      $file_extensions = explode(' ', $replacement_options['file_extensions']);
    }
    elseif ($file_type_name && ($file_type = file_type_load($file_type_name))) {
      $file_extensions = file_type_get_valid_extensions($file_type);
    }

    // Set allowed file extensions.
    if (!empty($file_extensions)) {

      // Set to type based file extensions.
      $replacement_options['file_extensions'] = implode(' ', $file_extensions);
    }
    else {

      // Fallback to the extension of the current file.
      $replacement_options['file_extensions'] = pathinfo($file->uri, PATHINFO_EXTENSION);
    }
    $form['replace_upload'] = array(
      '#type' => 'file',
      '#title' => t('Replace file'),
      '#description' => t('This file will replace the existing file. This action cannot be undone.'),
      '#upload_validators' => file_entity_get_upload_validators($replacement_options),
      '#pre_render' => array(
        'file_entity_upload_validators_pre_render',
      ),
    );
    $form['replace_keep_original_filename'] = array(
      '#type' => 'checkbox',
      '#title' => t('Keep original filename'),
      '#default_value' => variable_get('file_entity_file_replace_options_keep_original_filename', FALSE),
      '#description' => t('Rename the newly uploaded file to the name of the original file. This action cannot be undone.'),
    );
  }
  $form['preview'] = file_view_file($file, 'preview');
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 99,
  );

  // File destination information for administrators.
  $form['destination'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('administer files') && file_entity_file_is_writeable($file),
    '#title' => t('Destination'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'additional_settings',
    '#attributes' => array(
      'class' => array(
        'file-form-destination',
      ),
    ),
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'file_entity') . '/file_entity.js',
      ),
    ),
  );
  $options = array();
  foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $info) {
    $options[$scheme] = check_plain($info['name']);
  }
  $form['destination']['scheme'] = array(
    '#type' => 'radios',
    '#title' => t('Destination'),
    '#options' => $options,
    '#default_value' => file_uri_scheme($file->uri),
  );

  // File user information for administrators.
  $form['user'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('administer files'),
    '#title' => t('User information'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'additional_settings',
    '#attributes' => array(
      'class' => array(
        'file-form-user',
      ),
    ),
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'file_entity') . '/file_entity.js',
        array(
          'type' => 'setting',
          'data' => array(
            'anonymous' => variable_get('anonymous', t('Anonymous')),
          ),
        ),
      ),
    ),
    '#weight' => 90,
  );
  $form['user']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Associated with'),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
    '#default_value' => !empty($file->uid) && ($user = user_load($file->uid)) ? $user->name : '',
    '#weight' => -1,
    '#description' => t('Leave blank for %anonymous.', array(
      '%anonymous' => variable_get('anonymous', t('Anonymous')),
    )),
  );

  // Add the buttons.
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 5,
    '#submit' => array(
      'file_entity_edit_submit',
    ),
    '#validate' => array(
      'file_entity_edit_validate',
    ),
  );
  $form['actions']['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
    '#weight' => 10,
    '#submit' => array(
      'file_entity_edit_delete_submit',
    ),
    '#access' => file_entity_access('delete', $file),
  );

  // Build the URL for the cancel button taking into account that there might be
  // a "destination" that includes query string variables.
  $parameters = drupal_get_query_parameters();
  $destination = isset($parameters['destination']) ? $parameters['destination'] : 'file/' . $file->fid;
  $url = drupal_parse_url($destination);
  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => $url['path'],
    '#options' => array(
      'query' => $url['query'],
    ),
    '#weight' => 15,
  );
  $langcode = function_exists('entity_language') ? entity_language('file', $file) : NULL;
  field_attach_form('file', $file, $form, $form_state, $langcode);
  return $form;
}