Skip to content

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.

Providers follow a Replace, Don’t Layer resolution order where higher tiers override lower tiers for the same template or field type:

  1. 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 (including focus: { top, left, zoom }, dimensions, description, tags) and Pagefile metadata alongside InertiaImage, InertiaImageFocus, and InertiaFile TypeScript interfaces
    • PageReferenceFieldDataProvider: single and multi Page / PageArray references
    • RepeaterFieldDataProvider: Repeater and RepeaterMatrix items
    • OptionsFieldDataProvider: SelectableOption values and labels
    • Built-in InertiaFieldtypeInterface bridge adapter for custom Fieldtype modules
    • TemplateDataProvider and UserDataProvider
  2. Tier 2 (Module Hook): Modules hook Inertia::registerProviders and call $inertia->registerProvider(...).
  3. Tier 3 (Project Auto-Discovery in site/inertia/): Any *Provider.php file inside site/inertia/ under namespace 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,
];
}
}

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:

Terminal window
# Regenerate inertia.d.ts and scaffold missing site/classes/*Page.php classes
php vendor/bin/wire inertia:types
# Target specific templates and generate TypeScript definitions only
php vendor/bin/wire inertia:types --template=article,home --ts-only

Next steps