Skip to content

Commit db5b507

Browse files
committed
Cleaned up webform import list
1 parent 6dadeea commit db5b507

9 files changed

Lines changed: 109 additions & 106 deletions

assets/js/webform-index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
document.addEventListener('DOMContentLoaded', () => {
2+
const webforms = document.querySelectorAll('.os2forms-sync-webform-index .os2forms-sync-webform')
3+
const searchInput = document.querySelector('.os2forms-sync-webform-index [type="search"]')
4+
5+
const liveSearch = () => {
6+
console.log('liveSearch', searchInput.value)
7+
const query = searchInput.value
8+
webforms.forEach(webform => {
9+
webform.hidden = !webform.innerText.toLowerCase().includes(query)
10+
})
11+
}
12+
13+
let typingTimer
14+
const typingDelay = 250
15+
16+
searchInput.addEventListener('keyup', () => {
17+
clearTimeout(typingTimer)
18+
typingTimer = setTimeout(liveSearch, typingDelay)
19+
})
20+
})

os2forms_sync.libraries.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
webform-index:
2+
version: 1.x
3+
js:
4+
assets/js/webform-index.js: {}

os2forms_sync.module

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,22 @@ function os2forms_sync_webform_third_party_settings_form_alter(array &$form, For
2222
}
2323

2424
/**
25-
* Implements hook_webform_delete().
25+
* Implements hook_theme().
2626
*
27-
* @see ImportHelper::deleteWebform()
27+
* @see WebformHelper::theme()
28+
*
29+
* @phpstan-param array<string, mixed> $existing
30+
* @phpstan-return array<string, mixed>
2831
*/
29-
function os2forms_sync_webform_delete(WebformInterface $webform): void {
30-
\Drupal::service(ImportHelper::class)->deleteWebform($webform);
32+
function os2forms_sync_theme(array $existing, string $type, string $theme, string $path): array {
33+
return \Drupal::service(WebformHelper::class)->theme($existing, $type, $theme, $path);
3134
}
3235

3336
/**
34-
* Implements hook_theme().
37+
* Implements hook_webform_delete().
3538
*
36-
* @phpstan-param array<string, mixed> $existing
37-
* @phpstan-return array<string, mixed>
39+
* @see ImportHelper::deleteWebform()
3840
*/
39-
function os2forms_sync_theme(array $existing, string $type, string $theme, string $path): array {
40-
return [
41-
'os2forms_sync_webforms_index' => [
42-
'variables' => [
43-
'webforms' => NULL,
44-
],
45-
],
46-
'os2forms_sync_webform_import' => [
47-
'variables' => [
48-
'url' => NULL,
49-
'webform' => NULL,
50-
],
51-
],
52-
];
41+
function os2forms_sync_webform_delete(WebformInterface $webform): void {
42+
\Drupal::service(ImportHelper::class)->deleteWebform($webform);
5343
}

os2forms_sync.routing.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ os2forms_sync.webform.import:
1717
path: '/admin/os2forms/sync/webform/import'
1818
defaults:
1919
_controller: '\Drupal\os2forms_sync\Controller\WebformController::import'
20+
methods: [POST]
2021
requirements:
2122
_permission: 'access webform overview'
2223

src/Controller/WebformController.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,15 @@ public static function create(ContainerInterface $container): self {
5656
*/
5757
public function index(): array {
5858
$webforms = $this->importHelper->getAvailableWebforms();
59+
$settingsUrl = Url::fromRoute('os2forms_sync.admin.settings');
60+
if (!$settingsUrl->access($this->currentUser())) {
61+
$settingsUrl = NULL;
62+
}
5963

6064
return [
61-
'#theme' => 'os2forms_sync_webforms_index',
65+
'#theme' => 'os2forms_sync_webform_index',
6266
'#webforms' => $webforms,
67+
'#settings_url' => $settingsUrl,
6368
];
6469
}
6570

@@ -79,27 +84,18 @@ public function import() {
7984
}
8085

8186
$referrer = $request->query->get('referer');
82-
if ('POST' === $request->getMethod()) {
83-
try {
84-
$webform = $this->importHelper->import($url);
85-
$this->messenger()->addStatus($this->t('Webform @title imported.', ['@title' => $webform->get('title')]));
8687

87-
return new TrustedRedirectResponse($referrer ?? Url::fromRoute('entity.webform.edit_form', ['webform' => $webform->id()])->toString(TRUE)->getGeneratedUrl());
88-
}
89-
catch (\Exception $exception) {
90-
$this->messenger()->addError($exception->getMessage());
91-
}
88+
try {
89+
$webform = $this->importHelper->import($url);
90+
$this->messenger()->addStatus($this->t('Webform @title imported.', ['@title' => $webform->get('title')]));
9291

93-
return new TrustedRedirectResponse($referrer ?? Url::fromRoute('os2forms_sync.webform.import', ['url' => $url])->toString(TRUE)->getGeneratedUrl());
92+
return new TrustedRedirectResponse($referrer ?? Url::fromRoute('entity.webform.edit_form', ['webform' => $webform->id()])->toString(TRUE)->getGeneratedUrl());
93+
}
94+
catch (\Exception $exception) {
95+
$this->messenger()->addError($exception->getMessage());
9496
}
9597

96-
$webform = $this->importHelper->getAvailableWebform($url);
97-
98-
return [
99-
'#theme' => 'os2forms_sync_webform_import',
100-
'#url' => $url,
101-
'#webform' => $webform,
102-
];
98+
return new TrustedRedirectResponse($referrer ?? Url::fromRoute('os2forms_sync.webform.import', ['url' => $url])->toString(TRUE)->getGeneratedUrl());
10399
}
104100

105101
}

src/Helper/WebformHelper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,21 @@ public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterf
191191
}
192192
}
193193

194+
/**
195+
* Implements hook_theme().
196+
*
197+
* @phpstan-param array<string, mixed> $existing
198+
* @phpstan-return array<string, mixed>
199+
*/
200+
public function theme(array $existing, string $type, string $theme, string $path): array {
201+
return [
202+
'os2forms_sync_webform_index' => [
203+
'variables' => [
204+
'webforms' => NULL,
205+
'settings_url' => NULL,
206+
],
207+
],
208+
];
209+
}
210+
194211
}

templates/os2forms-sync-webform-import.html.twig

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{{ attach_library('os2forms_sync/webform-index') }}
2+
3+
<section class="os2forms-sync-webform-index">
4+
<h1>{{ 'Webforms'|t }}</h1>
5+
{% if webforms %}
6+
<div>
7+
<input type="search" class="form-element form-autocomplete" placeholder="{{ 'Search …'|t }}" />
8+
</div>
9+
<form method="post">
10+
{% for webform in webforms %}
11+
<div class="os2forms-sync-webform">
12+
<h2>{{ webform.attributes.title|raw }}</h2>
13+
<div class="description">
14+
{{ webform.attributes.description|raw }}
15+
</div>
16+
<div class="category">
17+
{{ webform.attributes.category }}
18+
</div>
19+
20+
<details>
21+
<summary>{{ 'Elements'|t }}</summary>
22+
23+
<pre>{{ webform.attributes.elements|yaml_encode }}</pre>
24+
</details>
25+
26+
<div class="source">
27+
<a href="{{ webform.links.self }}">{{ webform.links.self }}</a>
28+
</div>
29+
30+
<button class="button" formaction="{{ path('os2forms_sync.webform.import', {url: webform.links.self}) }}">{{ 'Import webform'|t }}</button>
31+
</div>
32+
{% endfor %}
33+
</form>
34+
{% else %}
35+
<div class="alert alert-warning">{{ 'No webforms'|t }}</div>
36+
{% endif %}
37+
38+
{% if settings_url|default() %}
39+
<hr>
40+
<a href="{{ settings_url }}">{{ 'O2Forms sync settings'|t }}</a>
41+
{% endif %}
42+
</section>

templates/os2forms-sync-webforms-index.html.twig

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)