View source
<?php
namespace Drupal\file_entity\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\file_entity\Entity\FileEntity;
use Drupal\file_entity\Entity\FileType;
use Drupal\file_entity\UploadValidatorsTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileEditForm extends ContentEntityForm {
use UploadValidatorsTrait;
protected $renderer;
protected $fileSystem;
public static function create(ContainerInterface $container) {
$form = parent::create($container);
$form->renderer = $container
->get('renderer');
$form->fileSystem = $container
->get('file_system');
return $form;
}
public function prepareEntity() {
if ($this->entity
->bundle() == FILE_TYPE_NONE) {
$this->entity
->updateBundle();
}
}
public function form(array $form, FormStateInterface $form_state) {
$file = $this->entity;
if ($this->operation == 'edit') {
if ($file
->bundle() == 'undefined') {
$type = $this
->t('file');
}
else {
$type = FileType::load($file
->bundle())
->label();
}
$form['#title'] = $this
->t('<em>Edit @type</em> "@title"', array(
'@type' => $type,
'@title' => $file
->label(),
));
if ($file
->isWritable()) {
$replacement_options = array();
$replacement_options['file_extensions'] = pathinfo($file
->getFilename(), PATHINFO_EXTENSION);
$form['replace_upload'] = array(
'#type' => 'managed_file',
'#title' => $this
->t('Replace file'),
'#upload_validators' => $this
->getUploadValidators($replacement_options),
);
$file_upload_help = array(
'#theme' => 'file_upload_help',
'#description' => $this
->t('This file will replace the existing file. This action cannot be undone.'),
'#upload_validators' => $form['replace_upload']['#upload_validators'],
);
$form['replace_upload']['#description'] = $this->renderer
->render($file_upload_help);
}
}
return parent::form($form, $form_state, $file);
}
public function save(array $form, FormStateInterface $form_state) {
$file = $this->entity;
$insert = $file
->isNew();
$file
->save();
$t_args = array(
'%title' => $file
->label(),
);
if ($insert) {
$this
->messenger()
->addMessage(t('%title has been created.', $t_args));
}
else {
$this
->messenger()
->addMessage(t('%title has been updated.', $t_args));
}
if ($file
->id()) {
$form_state
->setRedirectUrl($file
->toUrl());
}
else {
$this
->messenger()
->addMessage(t('The post could not be saved.'), 'error');
$form_state
->setRebuild();
}
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('replace_upload')) {
$file = file_save_upload('replace_upload', $form['replace_upload']['#upload_validators']);
if (!empty($file)) {
$form_state
->setValue('replace_upload', $file);
}
elseif ($file === FALSE) {
$form_state
->setError($form['replace_upload'], t('The replacement file could not be uploaded.'));
}
}
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('replace_upload')) {
$replacement = $form_state
->getValue('replace_upload')[0];
if ($replacement instanceof FileEntity) {
$entity_replacement = $replacement;
}
else {
$entity_replacement = File::load($replacement);
}
$log_args = array(
'@old' => $this->entity
->getFilename(),
'@new' => $entity_replacement
->getFileName(),
);
if ($this->fileSystem
->copy($entity_replacement
->getFileUri(), $this->entity
->getFileUri(), FileSystemInterface::EXISTS_REPLACE)) {
$entity_replacement
->delete();
\Drupal::logger('file_entity')
->info('File @old was replaced by @new', $log_args);
}
else {
\Drupal::logger('file_entity')
->notice('File @old failed to be replaced by @new', $log_args);
}
}
parent::submitForm($form, $form_state);
}
}