function FileManagementEditFileForm::buildForm in File Management 8
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ FileManagementEditFileForm.php, line 66
Class
- FileManagementEditFileForm
- Provides a form for editing files.
Namespace
Drupal\file_management\FormCode
function buildForm(array $form, FormStateInterface $form_state, FileInterface $file = NULL) {
if (empty($file)) {
// drupal_set_message
// return to previous page or file overview page (use route)
}
$form['existing_file_details'] = [
'#type' => 'details',
'#title' => $this
->t('Existing file details'),
'#open' => FALSE,
];
$form['existing_file_details'] += $this->fileManagement
->getFileInformation($file);
$form['new_file_details'] = [
'#type' => 'details',
'#title' => $this
->t('New file details'),
'#open' => TRUE,
];
$form['new_file_details']['old_fid'] = [
'#type' => 'hidden',
'#value' => $file
->id(),
];
$form['new_file_details']['new_file'] = [
'#title' => $this
->t('New file'),
'#type' => 'file',
'#description' => $this
->t('The new file to be used.<br />' . 'Leave empty to keep the existing file.<br />' . '<strong>Important:</strong> The filename will not be changed unless you specify a new filename below.'),
];
$form['new_file_details']['new_path'] = [
'#title' => $this
->t('New path'),
'#type' => 'textfield',
'#description' => $this
->t('The new path of the file.' . ' Please specify the full new path.<br />' . ' If no steam wrapper is defined, the existing one will be kept.<br />' . 'Leave empty to keep the existing file where it is.'),
];
$form['new_file_details']['new_filename'] = [
'#title' => $this
->t('New filename'),
'#type' => 'textfield',
'#description' => $this
->t('The new filename.<br />' . 'Leave empty to keep the existing filename.'),
];
$allowed_file_extensions = $this->fileManagement
->getAllowedFileExtensions($file);
if (!empty($allowed_file_extensions)) {
$allowed_file_extensions = implode(' ', $allowed_file_extensions);
$form['new_file_details']['new_filename']['#description'] .= '<br />' . $this
->t('Allowed types: @extensions.', [
'@extensions' => $allowed_file_extensions,
]);
}
$form['new_file_details']['actions']['#type'] = 'actions';
$form['new_file_details']['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
];
$this->fileManagement
->addBackButton($form['new_file_details']['actions'], $this
->t('Cancel'));
return $form;
}