View source
<?php
namespace Drupal\image_widget_crop_examples\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\file\Entity\File;
use Drupal\file\FileUsage\FileUsageInterface;
use Drupal\image_widget_crop\ImageWidgetCropInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ImageWidgetCropExamplesForm extends ConfigFormBase {
protected $settings;
protected $fileUsage;
protected $file = NULL;
protected $imageWidgetCropManager;
public function __construct(ConfigFactoryInterface $config_factory, FileUsageInterface $file_usage, ImageWidgetCropInterface $iwc_manager) {
parent::__construct($config_factory);
$this->settings = $this
->config('image_widget_crop_examples.settings');
$this->fileUsage = $file_usage;
$this->imageWidgetCropManager = $iwc_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('file.usage'), $container
->get('image_widget_crop.manager'));
}
public function getFormId() {
return 'image_widget_crop_examples_settings_form';
}
protected function getEditableConfigNames() {
return [
'image_widget_crop_examples.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#default_value' => $this->settings
->get('settings.title'),
];
$form['file'] = [
'#title' => $this
->t('Background Pictures'),
'#type' => 'managed_file',
'#description' => $this
->t('The uploaded image will be displayed on this page using the image style chosen below.'),
'#default_value' => $this->settings
->get('settings.file'),
'#upload_location' => 'public://image_widget_crop_examples/pictures',
'#multiple' => FALSE,
];
$fid = isset($this->settings
->get('settings.file')[0]) ? $this->settings
->get('settings.file')[0] : NULL;
if ($fid) {
$file = File::load($fid);
$form['image_crop'] = [
'#type' => 'image_crop',
'#file' => $file,
'#crop_type_list' => [
'crop_16_9',
],
'#crop_preview_image_style' => 'crop_thumbnail',
'#show_default_crop' => TRUE,
'#show_crop_area' => FALSE,
'#warn_mupltiple_usages' => TRUE,
];
}
return parent::buildForm($form, $form_state);
}
public function getEntity() {
return $this->file;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->settings
->set("settings.file", $form_state
->getValue('file'))
->set("settings.title", $form_state
->getValue('title'))
->set("settings.image_crop", $form_state
->getValue('image_crop'));
$file = !empty($form_state
->getValue('file')[0]) ? File::load($form_state
->getValue('file')[0]) : NULL;
if (!empty($file)) {
$this->fileUsage
->add($file, 'image_widget_crop_examples', 'form', $file
->id());
$file
->setPermanent();
$file
->save();
$this->file = $file;
}
$this->settings
->save();
if (!empty($form_state
->getValue('image_crop'))) {
$this->imageWidgetCropManager
->buildCropToForm($form_state);
}
}
}