function StyleForm::submitForm
File
-
modules/image_example/src/Form/StyleForm.php, line 141
Class
- StyleForm
- Form for interacting with image styles.
Namespace
Drupal\image_example\Form
Code
public function submitForm(array &$form, FormStateInterface $form_state) {
if (($fid = $form_state->getValue('image_fid')) != 0) {
if ($file = File::load($fid)) {
$file->setPermanent();
$file->save($file);
$this->fileUsage
->add($file, 'image_example', 'sample_image');
$this->messenger
->addMessage($this->t('The image @image_name was uploaded and saved. Its ID is @fid and it will be displayed using the image style @style.', [
'@image_name' => $file->getFilename(),
'@fid' => $fid,
'@style' => $form_state->getValue('style_name'),
]));
}
else {
$fid = NULL;
$this->messenger
->addWarning($this->t('The image could not be saved.'));
}
}
$old_fid = $this->state
->get('image_example.image_fid');
if (!empty($old_fid) && $old_file = File::load($old_fid)) {
$image_name = $old_file->getFilename();
$this->fileUsage
->delete($old_file, 'image_example', 'sample_image');
$old_file->delete();
$this->messenger
->addMessage($this->t('The image @image_name was removed.', [
'@image_name' => $image_name,
]));
}
$this->state
->set('image_example.image_fid', $fid);
$this->state
->set('image_example.style_name', $form_state->getValue('style_name'));
}