*/ public function toArray(Request $request): array { $locale = app(FormLocaleResolver::class)->resolve( $this->resource->schema, $request->user(), ); $this->resource->loadMissing('options'); return [ 'id' => $this->id, 'form_schema_id' => $this->form_schema_id, 'form_schema_section_id' => $this->form_schema_section_id, 'library_field_id' => $this->library_field_id, 'field_type' => $this->field_type, 'slug' => $this->slug, 'label' => $this->resolvedLabel($locale), 'help_text' => $this->resolvedHelpText($locale), 'section' => $this->section, 'options' => $this->resource->options->isNotEmpty() ? app(FormFieldOptionService::class)->toJsonShape($this->resource->options) : null, 'available_tags' => $this->when( $this->field_type === FormFieldType::TAG_PICKER->value, fn () => $this->availableTags(), ), 'validation_rules' => app(FormFieldValidationRuleService::class)->toJsonShape( $this->resource->validationRules, ), 'configs' => app(FormFieldConfigService::class)->toJsonShape( $this->resource->configs, ), 'is_required' => (bool) $this->is_required, 'is_filterable' => (bool) $this->is_filterable, 'is_portal_visible' => (bool) $this->is_portal_visible, 'is_admin_only' => (bool) $this->is_admin_only, 'is_unique' => (bool) $this->is_unique, 'is_pii' => (bool) $this->is_pii, 'display_width' => $this->display_width instanceof \BackedEnum ? $this->display_width->value : $this->display_width, 'binding' => app(FormFieldBindingService::class)->toJsonShape( $this->resource->bindings->first(), ), 'conditional_logic' => app(FormFieldConditionalLogicService::class)->toJsonShape( $this->resource->rootConditionalLogicGroup(), ), 'role_restrictions' => $this->role_restrictions, 'translations' => $this->translations, 'value_storage_hint' => $this->value_storage_hint instanceof \BackedEnum ? $this->value_storage_hint->value : $this->value_storage_hint, 'review_required' => (bool) $this->review_required, 'sort_order' => (int) $this->sort_order, ]; } private function resolvedLabel(string $locale): string { $translations = $this->translations ?? []; if (isset($translations[$locale]['label']) && $translations[$locale]['label'] !== '') { return (string) $translations[$locale]['label']; } return (string) $this->label; } private function resolvedHelpText(string $locale): ?string { $translations = $this->translations ?? []; if (isset($translations[$locale]['help_text'])) { return (string) $translations[$locale]['help_text']; } return $this->help_text; } /** * @return array> */ private function availableTags(): array { $organisationId = $this->resource->schema?->organisation_id; if ($organisationId === null) { return []; } $configs = app(FormFieldConfigService::class)->toJsonShape($this->resource->configs); $categoryFilter = (array) ($configs['tag_categories']['categories'] ?? []); $query = PersonTag::withoutGlobalScopes() ->where('organisation_id', $organisationId) ->where('is_active', true); if ($categoryFilter !== []) { $query->whereIn('category', $categoryFilter); } return $query->get(['id', 'name', 'category']) ->map(fn ($t) => [ 'id' => (string) $t->id, 'name' => (string) $t->name, 'category' => (string) $t->category, ]) ->all(); } }