d17551907c41ee22fd19a6928018c573af736e87
[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       '#disabled' => !$paragraphs_type->isNew(),
80     );
81
82     // Loop over the plugins that can be applied to this paragraph type.
83     if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
84       $form['message'] = [
85         '#type' => 'container',
86         '#markup' => $this->t('Behavior plugins are only supported by the EXPERIMENTAL paragraphs widget.'),
87         '#attributes' => ['class' => ['messages', 'messages--warning']]
88       ];
89       $form['behavior_plugins'] = [
90         '#type' => 'details',
91         '#title' => $this->t('Behaviors'),
92         '#tree' => TRUE,
93         '#open' => TRUE
94       ];
95       $config = $paragraphs_type->get('behavior_plugins');
96       foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
97         $description = $behavior_plugin_definition['description'];
98         $form['behavior_plugins'][$id]['enabled'] = [
99           '#type' => 'checkbox',
100           '#title' => $behavior_plugin_definition['label'],
101           '#title_display' => 'after',
102           '#description' => $description,
103           '#default_value' => !empty($config[$id]['enabled']),
104         ];
105         $form['behavior_plugins'][$id]['settings'] = [];
106         $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
107         $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
108         if ($settings = $behavior_plugin->buildConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state)) {
109           $form['behavior_plugins'][$id]['settings'] = $settings + [
110             '#type' => 'fieldset',
111             '#title' => $behavior_plugin_definition['label'],
112             '#states' => [
113               'visible' => [
114                   ':input[name="behavior_plugins[' . $id . '][enabled]"]' => ['checked' => TRUE],
115               ]
116             ]
117           ];
118         }
119       }
120     }
121
122     return $form;
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function validateForm(array &$form, FormStateInterface $form_state) {
129     parent::validateForm($form, $form_state);
130
131     $paragraphs_type = $this->entity;
132
133     if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
134       foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
135         // Only validate if the plugin is enabled and has settings.
136         if (isset($form['behavior_plugins'][$id]['settings']) && $form_state->getValue(['behavior_plugins', $id, 'enabled'])) {
137           $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
138           $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
139           $behavior_plugin->validateConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state);
140         }
141       }
142     }
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function save(array $form, FormStateInterface $form_state) {
149     $paragraphs_type = $this->entity;
150
151     if ($behavior_plugin_definitions = $this->paragraphsBehaviorManager->getApplicableDefinitions($paragraphs_type)) {
152       foreach ($behavior_plugin_definitions as $id => $behavior_plugin_definition) {
153         $behavior_plugin = $paragraphs_type->getBehaviorPlugin($id);
154
155         // If the behavior is enabled, initialize the configuration with the
156         // enabled key and then let it process the form input.
157         if ($form_state->getValue(['behavior_plugins', $id, 'enabled'])) {
158           $behavior_plugin->setConfiguration(['enabled' => TRUE]);
159           if (isset($form['behavior_plugins'][$id]['settings'])) {
160             $subform_state = SubformState::createForSubform($form['behavior_plugins'][$id]['settings'], $form, $form_state);
161             $behavior_plugin->submitConfigurationForm($form['behavior_plugins'][$id]['settings'], $subform_state);
162           }
163         }
164         else {
165           // The plugin is not enabled, reset to default configuration.
166           $behavior_plugin->setConfiguration([]);
167         }
168       }
169     }
170
171     $status = $paragraphs_type->save();
172     drupal_set_message($this->t('Saved the %label Paragraphs type.', array(
173       '%label' => $paragraphs_type->label(),
174     )));
175     if (($status == SAVED_NEW && \Drupal::moduleHandler()->moduleExists('field_ui'))
176       && $route_info = FieldUI::getOverviewRouteInfo('paragraph', $paragraphs_type->id())) {
177       $form_state->setRedirectUrl($route_info);
178     }
179     else {
180       $form_state->setRedirect('entity.paragraphs_type.collection');
181     }
182   }
183
184   /**
185    * {@inheritdoc}
186    */
187   protected function actions(array $form, FormStateInterface $form_state) {
188     $form = parent::actions($form, $form_state);
189
190     // We want to display the button only on add page.
191     if ($this->entity->isNew() && \Drupal::moduleHandler()->moduleExists('field_ui')) {
192       $form['submit']['#value'] = $this->t('Save and manage fields');
193     }
194
195     return $form;
196   }
197
198 }