Skip to content

Cf form relation #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ on:
jobs:
build:

runs-on: ubuntu-20.04
runs-on: ubuntu-24.04

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
extensions: mbstring, intl
ini-values: post_max_size=256M, max_execution_time=180
coverage: xdebug
Expand All @@ -26,7 +26,7 @@ jobs:

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
Expand All @@ -44,7 +44,7 @@ jobs:
- name: PHP STatic ANalyser (phpstan)
uses: php-actions/phpstan@v3
with:
version: latest
path: 'src'
php_version: '8.1'
php_version: '8.2'
level: 0
memory_limit: 256M
23 changes: 21 additions & 2 deletions src/App/Models/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Form extends Model implements \Asseco\CustomFields\App\Contracts\Form
'definition' => 'array',
];

private array $_customFieldNames = [];

protected static function newFactory()
{
return FormFactory::new();
Expand Down Expand Up @@ -70,14 +72,16 @@ protected function relateCustomFieldsFromDefinition()
{
$components = Arr::get($this->definition, 'components', []);

$this->_customFieldNames = [];
$this->extractCustomFields($components);
$this->relateCustomFields();
}

protected function extractCustomFields(array $components): void
{
foreach ($components as $componentKey => $component) {
if ($componentKey === 'key') {
$this->relateCustomField($component);
$this->_customFieldNames[] = $component;
}

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

protected function relateCustomFields(): void
{
if (empty($this->_customFieldNames)) {
return;
}

/** @var CustomField $customFieldClass */
$customFieldClass = app(CustomField::class);
$customFieldIds = $customFieldClass::query()->whereIn('name', array_unique($this->_customFieldNames))->get()->pluck('id');

if (!empty($customFieldIds)) {
$this->customFields()->attach($customFieldIds);
}
}

public function customFields(): BelongsToMany
{
return $this->belongsToMany(get_class(app(CustomField::class)))->withTimestamps();
Expand All @@ -112,7 +131,7 @@ public function customFields(): BelongsToMany
* @param array $formData
* @return array
*
* @throws Exception
* @throws Exception|\Throwable
*/
public function validate(array $formData): array
{
Expand Down