Data Providers & TypeScript Types
When you pass a ProcessWire Page, PageArray, Field, or User as an Inertia prop, Totoglu\Inertia\Provider\ProviderRegistry converts it into a clean, JSON-safe structure and generates matching PHP @property annotations and TypeScript interfaces.
The 3-Tier DataProvider Pipeline
Section titled “The 3-Tier DataProvider Pipeline”Providers follow a Replace, Don’t Layer resolution order where higher tiers override lower tiers for the same template or field type:
- Tier 1 (Built-in Core Providers):
ScalarFieldDataProvider: strings, numbers, booleans, dates, and opt-in module constants (INERTIA_IS_MARKUP_FIELD,INERTIA_IS_KEYVALUE_FIELD,INERTIA_IS_LINK_FIELD,INERTIA_IS_ICON_FIELD)MediaFieldDataProvider:Pageimage(includingfocus: { top, left, zoom }, dimensions, description, tags) andPagefilemetadata alongsideInertiaImage,InertiaImageFocus, andInertiaFileTypeScript interfacesPageReferenceFieldDataProvider: single and multiPage/PageArrayreferencesRepeaterFieldDataProvider: Repeater and RepeaterMatrix itemsOptionsFieldDataProvider: SelectableOption values and labels- Built-in
InertiaFieldtypeInterfacebridge adapter for customFieldtypemodules TemplateDataProviderandUserDataProvider
- Tier 2 (Module Hook): Modules hook
Inertia::registerProvidersand call$inertia->registerProvider(...). - Tier 3 (Project Auto-Discovery in
site/inertia/): Any*Provider.phpfile insidesite/inertia/undernamespace Totoglu\Inertia\Provider;is discovered automatically and takes top priority.
Customizing a Template (TemplateDataProvider)
Section titled “Customizing a Template (TemplateDataProvider)”Create site/inertia/ArticleTemplateDataProvider.php to exclude internal fields and append computed properties with matching TypeScript types:
<?php
declare(strict_types=1);
namespace Totoglu\Inertia\Provider;
use ProcessWire\Page;use ProcessWire\Template;
final class ArticleTemplateDataProvider extends TemplateDataProvider{ /** * @return list<string> */ public function templates(): array { return ['article']; }
/** * @return list<string> */ public function excludedFields(): array { return ['internal_notes']; }
/** * @return array<string, mixed> */ protected function extraData(Page $page): array { $words = str_word_count(strip_tags((string) $page->get('body')));
return [ 'readingTime' => max(1, (int) ceil($words / 200)), ]; }
/** * @return array<string, string> */ public function extraTsTypes(Template $template): array { return [ 'readingTime' => 'number', ]; }}Customizing a Fieldtype (FieldDataProvider)
Section titled “Customizing a Fieldtype (FieldDataProvider)”Create site/inertia/MapMarkerFieldDataProvider.php to control both runtime serialization and generated PHP/TypeScript types for a third-party Fieldtype:
<?php
declare(strict_types=1);
namespace Totoglu\Inertia\Provider;
use ProcessWire\Field;use ProcessWire\MapMarker;use ProcessWire\Page;
final class MapMarkerFieldDataProvider extends FieldDataProvider{ /** * @return list<string> */ public function supports(): array { return ['FieldtypeMapMarker', MapMarker::class]; }
/** * @return array{address: string, lat: float|null, lng: float|null, zoom: int}|null */ public function transform(mixed $value, ?Field $field = null, ?Page $page = null): ?array { if (!is_object($value)) { return null; }
$lat = isset($value->lat) ? (float) $value->lat : 0.0; $lng = isset($value->lng) ? (float) $value->lng : 0.0;
return [ 'address' => isset($value->address) ? trim((string) $value->address) : '', 'lat' => abs($lat) > 0.0001 ? round($lat, 6) : null, 'lng' => abs($lng) > 0.0001 ? round($lng, 6) : null, 'zoom' => isset($value->zoom) ? (int) $value->zoom : 13, ]; }
public function phpType(Field $field): string { return 'MapMarker|null'; }
public function tsType(Field $field): string { return 'InertiaMapMarker | null'; }
/** * @return array<string, string> */ public function tsDefinitions(?Field $field = null): array { return [ 'InertiaMapMarker' => <<<'TS'export interface InertiaMapMarker { address: string; lat: number | null; lng: number | null; zoom: number;}TS, ]; }}Generating TypeScript & PHP Page Classes
Section titled “Generating TypeScript & PHP Page Classes”When autoGenerateTypes and autoScaffoldPageClasses are enabled under Modules > Configure > Inertia, Inertia updates inertia.d.ts automatically whenever you save module settings or (when $config->debug is true) edit a template or field.
You can also run the generator manually from the CLI:
# Regenerate inertia.d.ts and scaffold missing site/classes/*Page.php classesphp vendor/bin/wire inertia:types
# Target specific templates and generate TypeScript definitions onlyphp vendor/bin/wire inertia:types --template=article,home --ts-only