3 namespace Drupal\image_widget_crop_examples\Form;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Form\ConfigFormBase;
8 use Drupal\file\Entity\File;
9 use Drupal\file\FileUsage\FileUsageInterface;
10 use Drupal\image_widget_crop\ImageWidgetCropInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Configure ImageWidgetCrop general settings for this site.
16 class ImageWidgetCropExamplesForm extends ConfigFormBase {
19 * The settings of image_widget_crop configuration.
23 * @see \Drupal\Core\Config\Config
28 * File usage interface to configurate an file object.
30 * @var Drupal\file\FileUsage\FileUsageInterface
35 * Created file entity.
37 * @var \Drupal\file\Entity\File|null
39 protected $file = NULL;
42 * Instance of API ImageWidgetCropManager.
44 * @var \Drupal\image_widget_crop\ImageWidgetCropInterface
46 protected $imageWidgetCropManager;
49 * Constructs a CropWidgetForm object.
51 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
52 * The factory for configuration objects.
53 * @param \Drupal\file\FileUsage\FileUsageInterface $file_usage
55 * @param \Drupal\image_widget_crop\ImageWidgetCropInterface $iwc_manager
56 * The ImageWidgetCrop manager service.
58 public function __construct(ConfigFactoryInterface $config_factory, FileUsageInterface $file_usage, ImageWidgetCropInterface $iwc_manager) {
59 parent::__construct($config_factory);
60 $this->settings = $this->config('image_widget_crop_examples.settings');
61 $this->fileUsage = $file_usage;
62 $this->imageWidgetCropManager = $iwc_manager;
68 public static function create(ContainerInterface $container) {
70 $container->get('config.factory'),
71 $container->get('file.usage'),
72 $container->get('image_widget_crop.manager')
79 public function getFormId() {
80 return 'image_widget_crop_examples_settings_form';
86 protected function getEditableConfigNames() {
87 return ['image_widget_crop_examples.settings'];
93 public function buildForm(array $form, FormStateInterface $form_state) {
96 '#type' => 'textfield',
97 '#title' => $this->t('Title'),
98 '#default_value' => $this->settings->get('settings.title'),
102 '#title' => $this->t('Background Pictures'),
103 '#type' => 'managed_file',
104 '#description' => $this->t('The uploaded image will be displayed on this page using the image style choosen below.'),
105 '#default_value' => $this->settings->get('settings.file'),
106 '#upload_location' => 'public://image_widget_crop_examples/pictures',
107 '#multiple' => FALSE,
110 // In this example we haven't an ajax form element to load it after upload,
111 // we need to upload file, save and crop file to provide a more simple,
112 // and explicit example.
113 $fid = isset($this->settings->get('settings.file')[0]) ? $this->settings->get('settings.file')[0] : NULL;
115 /* @var \Drupal\file\FileInterface $file */
116 $file = File::load($fid);
117 // The key of element are hardcoded into buildCropToForm function,
118 // ATM that is mendatory but can change easily.
119 $form['image_crop'] = [
120 '#type' => 'image_crop',
122 '#crop_type_list' => ['crop_16_9'],
123 '#crop_preview_image_style' => 'crop_thumbnail',
124 '#show_default_crop' => TRUE,
125 '#show_crop_area' => FALSE,
126 '#warn_mupltiple_usages' => TRUE,
130 return parent::buildForm($form, $form_state);
134 * Helper to expose file entity element.
136 * This method is mendatory to works with "buildCropToForm",
137 * for unicity with File entity compatibility.
139 * @return \Drupal\file\Entity\File|null
140 * File saved by file_manager element.
142 * @see \Drupal\image_widget_crop\ImageWidgetCropManager::buildCropToForm
144 public function getEntity() {
151 public function submitForm(array &$form, FormStateInterface $form_state) {
152 parent::submitForm($form, $form_state);
155 ->set("settings.file", $form_state->getValue('file'))
156 ->set("settings.title", $form_state->getValue('title'))
157 ->set("settings.image_crop", $form_state->getValue('image_crop'));
159 /* @var \Drupal\file\FileInterface $file */
160 $file = !empty($form_state->getValue('file')[0]) ? File::load($form_state->getValue('file')[0]) : NULL;
162 $this->fileUsage->add($file, 'image_widget_crop_examples', 'form', $file->id());
163 $file->setPermanent();
167 $this->settings->save();
169 if (!empty($form_state->getValue('image_crop'))) {
170 // Call IWC manager to attach crop defined into image file.
171 $this->imageWidgetCropManager->buildCropToForm($form_state);