Skip to content

Commit 3483cff

Browse files
committed
Added AvailableWebform entity
1 parent 9aee823 commit 3483cff

3 files changed

Lines changed: 67 additions & 17 deletions

File tree

src/Controller/WebformController.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Drupal\Core\Routing\TrustedRedirectResponse;
1111
use Drupal\Core\Serialization\Yaml;
1212
use Drupal\Core\Url;
13+
use Drupal\os2forms_sync\Entity\AvailableWebform;
1314
use Drupal\os2forms_sync\Helper\ImportHelper;
1415
use Drupal\os2forms_sync\Helper\WebformHelper;
1516
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -79,14 +80,14 @@ public function index(): array {
7980
// Filter available webforms.
8081
switch ($this->requestStack->getCurrentRequest()->get(self::FILTER_QUERY_NAME)) {
8182
case self::FILTER_WEBFORMS_IMPORTED:
82-
$webforms = array_filter($webforms, static function (array $webform) use ($importedWebforms) {
83-
return isset($importedWebforms[$webform['links']['self']]);
83+
$webforms = array_filter($webforms, static function (AvailableWebform $webform) use ($importedWebforms) {
84+
return isset($importedWebforms[$webform->sourceUrl]);
8485
});
8586
break;
8687

8788
case self::FILTER_WEBFORMS_NOT_IMPORTED:
88-
$webforms = array_filter($webforms, static function (array $webform) use ($importedWebforms) {
89-
return !isset($importedWebforms[$webform['links']['self']]);
89+
$webforms = array_filter($webforms, static function (AvailableWebform $webform) use ($importedWebforms) {
90+
return !isset($importedWebforms[$webform->sourceUrl]);
9091
});
9192
break;
9293
}
@@ -137,12 +138,12 @@ public function index(): array {
137138
}
138139
else {
139140
foreach ($webforms as $webform) {
140-
$attributes = $webform['attributes'];
141+
$attributes = $webform->attributes;
141142
$form = $this->webformHelper->getSubmissionForm($attributes['elements']);
142143
// Make sure that the form cannot be submitted (hopefully).
143144
$form['#attributes']['onsubmit'] = 'return false';
144145

145-
$sourceUrl = $webform['links']['self'];
146+
$sourceUrl = $webform->sourceUrl;
146147
$importedWebform = $importedWebforms[$sourceUrl] ?? NULL;
147148

148149
$item = [

src/Entity/AvailableWebform.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_sync\Entity;
4+
5+
/**
6+
* Available webform.
7+
*/
8+
class AvailableWebform {
9+
/**
10+
* The webform id.
11+
*
12+
* @var string
13+
*/
14+
public string $id;
15+
16+
/**
17+
* The source url.
18+
*
19+
* @var string
20+
*/
21+
public string $sourceUrl;
22+
23+
/**
24+
* The attributes.
25+
*
26+
* @var array
27+
*
28+
* @phpstan-var array<string, mixed>
29+
*/
30+
public array $attributes;
31+
32+
/**
33+
* Constructor.
34+
*
35+
* @phpstan-param array<string, mixed> $data
36+
*/
37+
public function __construct(array $data) {
38+
$this->id = $data['id'];
39+
$this->sourceUrl = $data['links']['self'];
40+
$this->attributes = $data['attributes'];
41+
}
42+
43+
}

src/Helper/ImportHelper.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Drupal\Core\Entity\EntityTypeManagerInterface;
1010
use Drupal\Core\Serialization\Yaml;
1111
use Drupal\Core\StringTranslation\StringTranslationTrait;
12+
use Drupal\os2forms_sync\Entity\AvailableWebform;
1213
use Drupal\os2forms_sync\Entity\ImportedWebform;
1314
use Drupal\webform\Entity\Webform;
1415
use Drupal\webform\WebformEntityStorageInterface;
@@ -314,7 +315,8 @@ public function deleteWebform(WebformInterface $webform): void {
314315
/**
315316
* Get available published webforms.
316317
*
317-
* @phpstan-return array<mixed>
318+
* @return \Drupal\os2forms_sync\Entity\AvailableWebform[]|array
319+
* The available webforms.
318320
*/
319321
public function getAvailableWebforms(): array {
320322
$sources = array_unique($this->settings->getSources());
@@ -329,16 +331,22 @@ public function getAvailableWebforms(): array {
329331
);
330332

331333
if ($ttl > 0 && $hit = $this->cache->get($cacheKey)) {
332-
return $hit->data;
334+
$webforms = $hit->data;
333335
}
336+
else {
337+
$webforms = $this->fetchAvailableWebforms($sources);
334338

335-
$webforms = $this->fetchAvailableWebforms($sources);
336-
337-
if ($ttl > 0) {
338-
$this->cache->set($cacheKey, $webforms, time() + $ttl);
339+
if ($ttl > 0) {
340+
$this->cache->set($cacheKey, $webforms, time() + $ttl);
341+
}
339342
}
340343

341-
return $webforms;
344+
return array_map(
345+
static function ($webform) {
346+
return new AvailableWebform($webform);
347+
},
348+
$webforms
349+
);
342350
}
343351

344352
/**
@@ -367,12 +375,10 @@ public function fetchAvailableWebforms(array $sources): array {
367375
* @param string $url
368376
* The webform url.
369377
*
370-
* @return array|null
378+
* @return \Drupal\os2forms_sync\Entity\AvailableWebform|null
371379
* The webform if any.
372-
*
373-
* @phpstan-return null|array<string, mixed>
374380
*/
375-
public function getAvailableWebform(string $url): ?array {
381+
public function getAvailableWebform(string $url): ?AvailableWebform {
376382
$webforms = $this->getAvailableWebforms();
377383
foreach ($webforms as $webform) {
378384
if ($url === ($webform['links']['self'] ?? NULL)) {

0 commit comments

Comments
 (0)