Namespace
Drupal\image_example\Form
File
-
modules/image_example/src/Form/StyleForm.php
View source
<?php
namespace Drupal\image_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileUsage\FileUsageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class StyleForm extends FormBase {
protected $state;
protected $fileUsage;
public static function create(ContainerInterface $container) {
$instance = new static();
$instance->setState($container->get('state'));
$instance->setFileUsage($container->get('file.usage'));
$instance->setMessenger($container->get('messenger'));
$instance->setStringTranslation($container->get('string_translation'));
return $instance;
}
protected function setState(StateInterface $state) {
$this->state = $state;
}
protected function setFileUsage(FileUsageInterface $file_usage) {
$this->fileUsage = $file_usage;
}
public function getFormId() {
return 'image_example_style_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$image_fid = $this->state
->get('image_example.image_fid');
if ($image_fid) {
$image = File::load($image_fid);
$style = $this->state
->get('image_example.style_name', 'thumbnail');
$form['image'] = [
'#theme' => 'image_style',
'#style_name' => $style,
'#uri' => $image->getFileUri(),
];
}
$form['image_fid'] = [
'#title' => $this->t('Image'),
'#type' => 'managed_file',
'#description' => $this->t('The uploaded image will be displayed on this page using the image style set in the next form field.'),
'#default_value' => $image_fid,
'#upload_location' => 'public://image_example_images/',
];
$form['style_name'] = [
'#title' => $this->t('Image style'),
'#type' => 'select',
'#description' => $this->t('Choose an image style to use when displaying this image.'),
'#options' => image_style_options(TRUE),
'#default_value' => $this->state
->get('image_example.style_name', 'thumbnail'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!$form_state->hasValue('image_fid') || !is_numeric($form_state->getValue('image_fid'))) {
$form_state->setErrorByName('image_fid', $this->t('Please select an image to upload.'));
}
}
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'));
}
}
Classes
| Title |
Deprecated |
Summary |
| StyleForm |
|
Form for interacting with image styles. |