Skip to content

Commit 9aee823

Browse files
committed
Cleaned up
1 parent c5e3f8a commit 9aee823

3 files changed

Lines changed: 107 additions & 87 deletions

File tree

assets/css/webform-index.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@
1616
display: flex;
1717
justify-content: space-between;
1818
}
19+
20+
.os2forms-sync-webform-index .settings {
21+
margin-top: 2em;
22+
}

src/Controller/WebformController.php

Lines changed: 103 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -125,121 +125,138 @@ public function index(): array {
125125
],
126126
];
127127

128-
foreach ($webforms as $webform) {
129-
$attributes = $webform['attributes'];
130-
$form = $this->webformHelper->getSubmissionForm($attributes['elements']);
131-
// Make sure that the form cannot be submitted (hopefully).
132-
$form['#attributes']['onsubmit'] = 'return false';
133-
134-
$sourceUrl = $webform['links']['self'];
135-
$importedWebform = $importedWebforms[$sourceUrl] ?? NULL;
136-
137-
$item = [
138-
'#type' => 'fieldset',
139-
'#title' => $attributes['title'] ?? 'xxx',
140-
'#attributes' => ['class' => ['os2forms-sync-webform']],
141-
142-
'description' => [
143-
'#type' => 'container',
144-
'#attributes' => [
145-
'class' => ['description'],
146-
'data-indexed' => strip_tags($attributes['description']),
128+
if (empty($webforms)) {
129+
$elements['info'] = [
130+
'#theme' => 'status_messages',
131+
'#message_list' => [
132+
'warning' => [
133+
$this->t('No webforms found'),
147134
],
148-
'#markup' => Markup::create($attributes['description']),
149-
],
150-
151-
'form_display' => [
152-
'#type' => 'details',
153-
'#title' => $this->t('Form display'),
154-
'form' => $form,
155135
],
136+
];
137+
}
138+
else {
139+
foreach ($webforms as $webform) {
140+
$attributes = $webform['attributes'];
141+
$form = $this->webformHelper->getSubmissionForm($attributes['elements']);
142+
// Make sure that the form cannot be submitted (hopefully).
143+
$form['#attributes']['onsubmit'] = 'return false';
144+
145+
$sourceUrl = $webform['links']['self'];
146+
$importedWebform = $importedWebforms[$sourceUrl] ?? NULL;
147+
148+
$item = [
149+
'#type' => 'fieldset',
150+
'#title' => $attributes['title'] ?? 'xxx',
151+
'#attributes' => ['class' => ['os2forms-sync-webform']],
152+
153+
'description' => [
154+
'#type' => 'container',
155+
'#attributes' => [
156+
'class' => ['description'],
157+
'data-indexed' => strip_tags($attributes['description']),
158+
],
159+
'#markup' => Markup::create($attributes['description']),
160+
],
156161

157-
'elements' => [
158-
'#type' => 'details',
159-
'#title' => $this->t('Elements'),
160-
'#markup' => '<pre>' . Yaml::encode($attributes['elements']) . '</pre>',
161-
],
162+
'form_display' => [
163+
'#type' => 'details',
164+
'#title' => $this->t('Form display'),
165+
'form' => $form,
166+
],
162167

163-
'metadata' => [
164-
'#type' => 'container',
165-
'#attributes' => [
166-
'class' => ['metadata'],
168+
'elements' => [
169+
'#type' => 'details',
170+
'#title' => $this->t('Elements'),
171+
'#markup' => '<pre>' . Yaml::encode($attributes['elements']) . '</pre>',
167172
],
168173

169-
'category' => [
174+
'metadata' => [
170175
'#type' => 'container',
171176
'#attributes' => [
172-
'class' => ['category'],
173-
'data-indexed' => strip_tags($attributes['category']),
177+
'class' => ['metadata'],
174178
],
175179

176-
'label' => [
177-
'#type' => 'label',
178-
'#title' => $this->t('Category'),
179-
'#title_display' => 'above',
180-
],
181-
182-
'value' => [
183-
'#type' => 'html_tag',
184-
'#tag' => 'span',
180+
'category' => [
181+
'#type' => 'container',
185182
'#attributes' => [
186-
'class' => ['value'],
183+
'class' => ['category'],
184+
'data-indexed' => strip_tags($attributes['category']),
187185
],
188-
'#value' => $attributes['category'],
189-
],
190-
],
191186

192-
'source_url' => [
193-
'#type' => 'container',
187+
'label' => [
188+
'#type' => 'label',
189+
'#title' => $this->t('Category'),
190+
'#title_display' => 'above',
191+
],
194192

195-
'label' => [
196-
'#type' => 'label',
197-
'#title' => $this->t('Source url'),
198-
'#title_display' => 'above',
193+
'value' => [
194+
'#type' => 'html_tag',
195+
'#tag' => 'span',
196+
'#attributes' => [
197+
'class' => ['value'],
198+
],
199+
'#value' => $attributes['category'],
200+
],
199201
],
200202

201-
'value' => [
202-
'#type' => 'html_tag',
203-
'#tag' => 'span',
204-
'#attributes' => [
205-
'class' => ['value'],
203+
'source_url' => [
204+
'#type' => 'container',
205+
206+
'label' => [
207+
'#type' => 'label',
208+
'#title' => $this->t('Source url'),
209+
'#title_display' => 'above',
210+
],
211+
212+
'value' => [
213+
'#type' => 'html_tag',
214+
'#tag' => 'span',
215+
'#attributes' => [
216+
'class' => ['value'],
217+
],
218+
'link' => (new Link($sourceUrl, Url::fromUri($sourceUrl)))->toRenderable(),
206219
],
207-
'link' => (new Link($sourceUrl, Url::fromUri($sourceUrl)))->toRenderable(),
208220
],
209221
],
210-
],
211-
];
212-
213-
$item['import_form'] = [
214-
'#type' => 'html_tag',
215-
'#tag' => 'form',
216-
'#attributes' => [
217-
'method' => 'post',
218-
'action' => Url::fromRoute('os2forms_sync.webform.import', ['url' => $sourceUrl])->toString(TRUE)->getGeneratedUrl(),
219-
],
222+
];
220223

221-
'button' => [
222-
'#type' => 'button',
223-
'#value' => NULL === $importedWebform ? $this->t('Import webform') : $this->t('Update webform'),
224-
],
225-
];
224+
$item['import_form'] = [
225+
'#type' => 'html_tag',
226+
'#tag' => 'form',
227+
'#attributes' => [
228+
'method' => 'post',
229+
'action' => Url::fromRoute('os2forms_sync.webform.import',
230+
['url' => $sourceUrl])->toString(TRUE)->getGeneratedUrl(),
231+
],
226232

227-
if (NULL !== $importedWebform) {
228-
$item['import_form']['info'] = [
229-
'#markup' => $this->t('<a href=":webform_url">Webform</a> updated at @updated_at.', [
230-
':webform_url' => Url::fromRoute('entity.webform.edit_form', ['webform' => $importedWebform->webformId])->toString(TRUE)->getGeneratedUrl(),
231-
'@updated_at' => $importedWebform->updatedAt->format(DrupalDateTime::FORMAT),
232-
]),
233+
'button' => [
234+
'#type' => 'button',
235+
'#value' => NULL === $importedWebform ? $this->t('Import webform') : $this->t('Update webform'),
236+
],
233237
];
234-
}
235238

236-
$elements[] = $item;
239+
if (NULL !== $importedWebform) {
240+
$item['import_form']['info'] = [
241+
'#markup' => $this->t('<a href=":webform_url">Webform</a> updated at @updated_at.', [
242+
':webform_url' => Url::fromRoute('entity.webform.edit_form',
243+
['webform' => $importedWebform->webformId])->toString(TRUE)->getGeneratedUrl(),
244+
'@updated_at' => $importedWebform->updatedAt->format(DrupalDateTime::FORMAT),
245+
]),
246+
];
247+
}
248+
249+
$elements[] = $item;
250+
}
237251
}
238252

239253
$settingsUrl = Url::fromRoute('os2forms_sync.admin.settings');
240254
if ($settingsUrl->access($this->currentUser())) {
241255
$elements['settings'] = [
242256
'#type' => 'container',
257+
'#attributes' => [
258+
'class' => ['settings'],
259+
],
243260

244261
'link' => (new Link($this->t('O2Forms sync settings'), $settingsUrl))->toRenderable(),
245262
];

src/Helper/WebformHelper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Drupal\os2forms_sync\Helper;
44

5-
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
65
use Drupal\Component\Utility\Random;
76
use Drupal\Core\Datetime\DrupalDateTime;
87
use Drupal\Core\Entity\EntityTypeManagerInterface;

0 commit comments

Comments
 (0)