Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / paragraphs / src / Form / ParagraphsTypeForm.php
1 <?php
2
3 namespace Drupal\paragraphs\Form;
4
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Form\SubformState;
8 use Drupal\field_ui\FieldUI;
9 use Drupal\paragraphs\ParagraphsBehaviorManager;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Form controller for paragraph type forms.
14  */
15 class ParagraphsTypeForm extends EntityForm {
16
17   /**
18    * The paragraphs behavior plugin manager service.
19    *
20    * @var \Drupal\paragraphs\ParagraphsBehaviorManager
21    */
22   protected $paragraphsBehaviorManager;
23
24   /**
25    * The entity being used by this form.
26    *
27    * @var \Drupal\paragraphs\ParagraphsTypeInterface
28    */
29   protected $entity;
30
31   /**
32    * GeneralSettingsForm constructor.
33    *
34    * @param \Drupal\paragraphs\ParagraphsBehaviorManager $paragraphs_behavior_manager
35    *   The paragraphs type feature manager service.
36    */
37   public function __construct(ParagraphsBehaviorManager $paragraphs_behavior_manager) {
38     $this->paragraphsBehaviorManager = $paragraphs_behavior_manager;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public static function create(ContainerInterface $container) {
45     return new static(
46       $container->get('plugin.manager.paragraphs.behavior')
47     );
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function form(array $form, FormStateInterface $form_state) {
54     $form = parent::form($form, $form_state);
55
56     $paragraphs_type = $this->entity;
57
58     if (!$paragraphs_type->isNew()) {
59       $form['#title'] = (t('Edit %title paragraph type', [
60         '%title' => $paragraphs_type->label(),
61       ]));
62     }
63
64     $form['label'] = array(
65       '#type' => 'textfield',
66       '#title' => $this->t('Label'),
67       '#maxlength' => 255,
68       '#default_value' => $paragraphs_type->label(),
69       '#description' => $this->t("Label for the Paragraphs type."),
70       '#required' => TRUE,
71     );
72
73     $form['id'] = array(
74       '#type' => 'machine_name',
75       '#default_value' => $paragraphs_type->id(),
76       '#machine_name' => array(
77         'exists' => 'paragraphs_type_load',
78       ),
79       '#maxlength' => 32,
80       '#disabled' => !$paragraphs_type->isNew(),
81     );
82
83     $form['icon_file'] = [
84       '#title' => $this->t('Paragraph type icon'),
85       '#type' => 'managed_file',
86       '#upload_location' => 'public://paragraphs_type_icon/',
87       '#upload_validators' => [
88         'file_validate_extensions' => ['png jpg svg'],
89       ],
90     ];
91
92     if ($file = $this->entity->getIconFile()) {
93       $form['icon_file']['#default_value'] = ['target_id' => $file->id()];
94     }
95
96     $form['description'] = [
97       '#title' => t('Description'),
98       '#type' => 'textarea',
99       '#default_value' => $paragraphs_type->getDescription(),
100       '#description' => t('This text will be displayed on the <em>Add new paragraph</em> page.'),
101     ];
102
103     // Loop over the plugins that can be applied to this paragraph type.
104     if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
105       $form['message'] = [
106         '#type' => 'container',
107         '#markup' => $this->t('Behavior plugins are only supported by the EXPERIMENTAL paragraphs widget.'),
108         '#attributes' => ['class' => ['messages', 'messages--warning']]
109       ];
110       $form['behavior_plugins'] = [
111         '#type' => 'details',
112         '#title' => $this->t('Behaviors'),
113         '#tree' => TRUE,
114         '#open' => TRUE
115       ];
116       $config = $paragraphs_type->get('behavior_plugins');
117       foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
118         $description = $behavior_plugin_definition['description'];
119         $form['behavior_plugins'][$id]['enabled'] = [
120           '#type' => 'checkbox',
121           '#title' => $behavior_plugin_definition['label'],
122           '#title_display' => 'after',
123           '#description' => $description,
124           '#default_value' => !empty($config[$id]['enabled']),
125         ];
126         $form['behavior_plugins'][$id]['settings'] = [];
127         $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
128         $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
129         if ($settings = $behavior_plugin->buildConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state)) {
130           $form['behavior_plugins'][$id]['settings'] = $settings + [
131             '#type' => 'fieldset',
132             '#title' => $behavior_plugin_definition['label'],
133             '#states' => [
134               'visible' => [
135                   ':input[name="behavior_plugins[' . $id . '][enabled]"]' => ['checked' => TRUE],
136               ]
137             ]
138           ];
139         }
140       }
141     }
142
143     return $form;
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function validateForm(array &$form, FormStateInterface $form_state) {
150     parent::validateForm($form, $form_state);
151
152     $paragraphs_type = $this->entity;
153
154     $icon_fild = $form_state->getValue(['icon_file', '0']);
155     // Set the file UUID to the paragraph configuration.
156     if (!empty($icon_fild) && $file = $this->entityTypeManager->getStorage('file')->load($icon_fild)) {
157       $paragraphs_type->set('icon_uuid', $file->uuid());
158     }
159     else {
160       $paragraphs_type->set('icon_uuid', NULL);
161     }
162
163     if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
164       foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
165         // Only validate if the plugin is enabled and has settings.
166         if (isset($form['behavior_plugins'][$id]['settings']) && $form_state->getValue(['behavior_plugins', $id, 'enabled'])) {
167           $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
168           $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
169           $behavior_plugin->validateConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state);
170         }
171       }
172     }
173   }
174
175   /**
176    * {@inheritdoc}
177    */
178   public function save(array $form, FormStateInterface $form_state) {
179     $paragraphs_type = $this->entity;
180
181     if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
182       foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
183         $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
184
185         // If the behavior is enabled, initialize the configuration with the
186         // enabled key and then let it process the form input.
187         if ($form_state->getValue(['behavior_plugins', $id, 'enabled'])) {
188           $behavior_plugin->setConfiguration(['enabled' => TRUE]);
189           if (isset($form['behavior_plugins'][$id]['settings'])) {
190             $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
191             $behavior_plugin->submitConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state);
192           }
193         }
194         else {
195           // The plugin is not enabled, reset to default configuration.
196           $behavior_plugin->setConfiguration([]);
197         }
198       }
199     }
200
201     $status = $paragraphs_type->save();
202     drupal_set_message($this->t('Saved the %label Paragraphs type.', array(
203       '%label' => $paragraphs_type->label(),
204     )));
205     if (($status == SAVED_NEW && \Drupal::moduleHandler()->moduleExists('field_ui'))
206       && $route_info = FieldUI::getOverviewRouteInfo('paragraph', $paragraphs_type->id())) {
207       $form_state->setRedirectUrl($route_info);
208     }
209     else {
210       $form_state->setRedirect('entity.paragraphs_type.collection');
211     }
212   }
213
214   /**
215    * {@inheritdoc}
216    */
217   protected function actions(array $form, FormStateInterface $form_state) {
218     $form = parent::actions($form, $form_state);
219
220     // We want to display the button only on add page.
221     if ($this->entity->isNew() && \Drupal::moduleHandler()->moduleExists('field_ui')) {
222       $form['submit']['#value'] = $this->t('Save and manage fields');
223     }
224
225     return $form;
226   }
227
228 }