f3b796a14a8504af766e2c33d3fca77a94074913
[yaffs-website] / web / modules / contrib / image_widget_crop / modules / image_widget_crop_examples / src / Form / ImageWidgetCropExamplesForm.php
1 <?php
2
3 namespace Drupal\image_widget_crop_examples\Form;
4
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;
12
13 /**
14  * Configure ImageWidgetCrop general settings for this site.
15  */
16 class ImageWidgetCropExamplesForm extends ConfigFormBase {
17
18   /**
19    * The settings of image_widget_crop configuration.
20    *
21    * @var array
22    *
23    * @see \Drupal\Core\Config\Config
24    */
25   protected $settings;
26
27   /**
28    * File usage interface to configurate an file object.
29    *
30    * @var Drupal\file\FileUsage\FileUsageInterface
31    */
32   protected $fileUsage;
33
34   /**
35    * Created file entity.
36    *
37    * @var \Drupal\file\Entity\File|null
38    */
39   protected $file = NULL;
40
41   /**
42    * Instance of API ImageWidgetCropManager.
43    *
44    * @var \Drupal\image_widget_crop\ImageWidgetCropInterface
45    */
46   protected $imageWidgetCropManager;
47
48   /**
49    * Constructs a CropWidgetForm object.
50    *
51    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
52    *   The factory for configuration objects.
53    * @param \Drupal\file\FileUsage\FileUsageInterface $file_usage
54    *   File usage service.
55    * @param \Drupal\image_widget_crop\ImageWidgetCropInterface $iwc_manager
56    *   The ImageWidgetCrop manager service.
57    */
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;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public static function create(ContainerInterface $container) {
69     return new static (
70       $container->get('config.factory'),
71       $container->get('file.usage'),
72       $container->get('image_widget_crop.manager')
73     );
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function getFormId() {
80     return 'image_widget_crop_examples_settings_form';
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   protected function getEditableConfigNames() {
87     return ['image_widget_crop_examples.settings'];
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function buildForm(array $form, FormStateInterface $form_state) {
94
95     $form['title'] = [
96       '#type' => 'textfield',
97       '#title' => $this->t('Title'),
98       '#default_value' => $this->settings->get('settings.title'),
99     ];
100
101     $form['file'] = [
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,
108     ];
109
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;
114     if ($fid) {
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',
121         '#file' => $file,
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,
127       ];
128     }
129
130     return parent::buildForm($form, $form_state);
131   }
132
133   /**
134    * Helper to expose file entity element.
135    *
136    * This method is mendatory to works with "buildCropToForm",
137    * for unicity with File entity compatibility.
138    *
139    * @return \Drupal\file\Entity\File|null
140    *   File saved by file_manager element.
141    *
142    * @see \Drupal\image_widget_crop\ImageWidgetCropManager::buildCropToForm
143    */
144   public function getEntity() {
145     return $this->file;
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function submitForm(array &$form, FormStateInterface $form_state) {
152     parent::submitForm($form, $form_state);
153
154     $this->settings
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'));
158
159     /* @var \Drupal\file\FileInterface $file */
160     $file = !empty($form_state->getValue('file')[0]) ? File::load($form_state->getValue('file')[0]) : NULL;
161     if (!empty($file)) {
162       $this->fileUsage->add($file, 'image_widget_crop_examples', 'form', $file->id());
163       $file->setPermanent();
164       $file->save();
165       $this->file = $file;
166     }
167     $this->settings->save();
168
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);
172     }
173   }
174
175 }