Skip to content

Commit 997b2cf

Browse files
ngaspariStyleCIBot
andauthored
Cf form relation (#103)
* Update Form.php mass assign custom field IDs to Form * Update Form.php handle empty CFs * Apply fixes from StyleCI * Update pull_request.yml runs-on: ubuntu-24.04 * Update Form.php for testing. there's one instance used mutliple times * Update pull_request.yml pull_req --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent d2c9ed3 commit 997b2cf

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

.github/workflows/pull_request.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ on:
77
jobs:
88
build:
99

10-
runs-on: ubuntu-20.04
10+
runs-on: ubuntu-24.04
1111

1212
steps:
1313
- uses: actions/checkout@v2
1414

1515
- name: Setup PHP
1616
uses: shivammathur/setup-php@v2
1717
with:
18-
php-version: '8.1'
18+
php-version: '8.2'
1919
extensions: mbstring, intl
2020
ini-values: post_max_size=256M, max_execution_time=180
2121
coverage: xdebug
@@ -26,7 +26,7 @@ jobs:
2626

2727
- name: Cache Composer packages
2828
id: composer-cache
29-
uses: actions/cache@v2
29+
uses: actions/cache@v4
3030
with:
3131
path: vendor
3232
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
@@ -44,7 +44,7 @@ jobs:
4444
- name: PHP STatic ANalyser (phpstan)
4545
uses: php-actions/phpstan@v3
4646
with:
47+
version: latest
4748
path: 'src'
48-
php_version: '8.1'
49+
php_version: '8.2'
4950
level: 0
50-
memory_limit: 256M

src/App/Models/Form.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class Form extends Model implements \Asseco\CustomFields\App\Contracts\Form
3636
'definition' => 'array',
3737
];
3838

39+
private array $_customFieldNames = [];
40+
3941
protected static function newFactory()
4042
{
4143
return FormFactory::new();
@@ -70,14 +72,16 @@ protected function relateCustomFieldsFromDefinition()
7072
{
7173
$components = Arr::get($this->definition, 'components', []);
7274

75+
$this->_customFieldNames = [];
7376
$this->extractCustomFields($components);
77+
$this->relateCustomFields();
7478
}
7579

7680
protected function extractCustomFields(array $components): void
7781
{
7882
foreach ($components as $componentKey => $component) {
7983
if ($componentKey === 'key') {
80-
$this->relateCustomField($component);
84+
$this->_customFieldNames[] = $component;
8185
}
8286

8387
if (!is_array($component)) {
@@ -103,6 +107,21 @@ protected function relateCustomField(string $key): void
103107
}
104108
}
105109

110+
protected function relateCustomFields(): void
111+
{
112+
if (empty($this->_customFieldNames)) {
113+
return;
114+
}
115+
116+
/** @var CustomField $customFieldClass */
117+
$customFieldClass = app(CustomField::class);
118+
$customFieldIds = $customFieldClass::query()->whereIn('name', array_unique($this->_customFieldNames))->get()->pluck('id');
119+
120+
if (!empty($customFieldIds)) {
121+
$this->customFields()->attach($customFieldIds);
122+
}
123+
}
124+
106125
public function customFields(): BelongsToMany
107126
{
108127
return $this->belongsToMany(get_class(app(CustomField::class)))->withTimestamps();
@@ -112,7 +131,7 @@ public function customFields(): BelongsToMany
112131
* @param array $formData
113132
* @return array
114133
*
115-
* @throws Exception
134+
* @throws Exception|\Throwable
116135
*/
117136
public function validate(array $formData): array
118137
{

0 commit comments

Comments
 (0)