diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index f9c4443..1359912 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -7,7 +7,7 @@ on: jobs: build: - runs-on: ubuntu-20.04 + runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v2 @@ -15,7 +15,7 @@ jobs: - 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 @@ -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') }} @@ -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 diff --git a/src/App/Models/Form.php b/src/App/Models/Form.php index 524e8ba..6b69b29 100644 --- a/src/App/Models/Form.php +++ b/src/App/Models/Form.php @@ -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(); @@ -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)) { @@ -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(); @@ -112,7 +131,7 @@ public function customFields(): BelongsToMany * @param array $formData * @return array * - * @throws Exception + * @throws Exception|\Throwable */ public function validate(array $formData): array {