Pivot References & Dynamic Schemas
When linking records together, a plain reference list is often not enough. For example, a yacht may include several amenities from an amenities collection, and each selected amenity needs its own extra metadata: how many units are onboard (quantity), where it is placed (locations), or a custom editorial note.
Annotated Pivot References turn every selected reference into a structured pivot row (kind = 'pivot') that stores both the link to the target ($row->target()) and its per-selection fields.
Defining a pivot reference in YAML
Section titled “Defining a pivot reference in YAML”Attach a pivot: block to any type: entry or type: page field (or set $field->pivotBlueprint on FieldtypeEntries for ProcessWire pages):
fields: amenities: type: entry target: amenities multiple: true label: Included Onboard Amenities pivot: display: inline # 'inline' (picker + detail cards), 'table', or 'cards' groupBy: category # Target field used to group picker options into filter tabs columns: 3 # 0 = auto-wrap pills; 1..6 = aligned grid searchable: true # Shows search and "Unselected only" filter toolbar uniqueTarget: true # Set false to allow selecting the same target multiple times orderMode: manual # 'manual' (drag sortable) or 'target' (follow target sort) pruneHidden: false # Set true to clear hidden conditional values on save blueprint: amenity_details # Optional shared fieldset or block blueprint fields: quantity: type: integer label: Quantity min: 1 index: true showIf: '@target:has_quantity=1' requiredIf: '@target:requires_quantity=1'
locations: type: select label: Placement index: true options: '@target:available_options.key:label' multiple: '@target:allow_multiple=1'
note: type: '@target:custom_input_type' label: Crew Note useLanguages: true showIf: '@target:has_note=1'Dynamic @target:<path> bindings
Section titled “Dynamic @target:<path> bindings”Pivot fields can adapt automatically to properties on the selected target EntryItem or Page:
- Conditions: Use
@target:insideshowIf,hideIf,requiredIf,enableIf,disableIf, andreadOnlyIfwith operators (=,!=,<,<=,>,>=,*=,%=), truthy checks (@target:flag), negations (!@target:flag), or dot-paths (@target:parent.code=CABIN). - Dynamic options: Point
options: '@target:available_options.key:label'at akeyvalue,blocks, or scalar field on the target record. If a previously selected option is later removed from the target, Entries preserves the stored value and displays a warning badge instead of losing data. - Dynamic properties and field types: Bind
multiple,default,min,max,minLength,maxLength,prefix,suffix,placeholder,label, ortype: '@target:<field>'(allowlisted safe types:text,textarea,integer,float,checkbox,toggle,select,date,datetime,email,url,color).
Working with pivot rows in PHP
Section titled “Working with pivot rows in PHP”Create, inspect, and eager-load pivot rows cleanly in templates or services:
<?php
declare(strict_types=1);
namespace ProcessWire;
$entries = wire()->entries;$jacuzzi = $entries->findOne('type=amenities, name=jacuzzi');$yacht = $entries->findOne('type=yachts, name=ocean-star');
if ($jacuzzi !== null && $yacht !== null) { // Create an annotated pivot row $pivotRow = $entries->createPivotRow($jacuzzi, [ 'quantity' => 2, 'locations' => ['deck'], ], 'amenity_details');
$yacht->addChild('amenities', $pivotRow); $entries->save($yacht);}
// Eager-load pivot targets in batch to avoid N+1 queries$yachts = $entries->find('type=yachts, status=published, limit=12')->with('amenities.target');foreach ($yachts->of(true) as $item) { foreach ($item->getChildren('amenities') as $row) { $target = $row->target(); if ($target !== null) { $label = $sanitizer->entities((string) $target->get('title')); $qty = (int) $row->get('quantity'); echo "<li>{$label} (x{$qty})</li>"; } }}Same-row correlation queries (@, a@, b@)
Section titled “Same-row correlation queries (@, a@, b@)”A standard selector like amenities.target.name=jacuzzi, amenities.quantity>=2 matches independently across all rows, so a yacht with 1 Jacuzzi and 2 Jet Skis would match. Prefix correlated clauses with @ (or named groups a@ and b@) so conditions apply to the exact same pivot row:
<?php
declare(strict_types=1);
namespace ProcessWire;
$entries = wire()->entries;
// Same-row match: the Jacuzzi row itself must have quantity >= 2$doubleJacuzzi = $entries->find( 'type=yachts, @amenities.target.name=jacuzzi, @amenities.quantity>=2, limit=20');
// Named groups: require BOTH Jacuzzi (qty=1) AND Jet Ski (qty>=2) on distinct rows$multiCriteria = $entries->find( 'type=yachts, [email protected]=jacuzzi, [email protected]=1, [email protected]=jet-ski, [email protected]>=2, limit=20');
// Identical syntax works on ProcessWire pages connected via FieldtypeEntries$matchingPages = wire()->pages->find( 'template=yacht, @amenities.target.name=jacuzzi, @amenities.quantity>=2, limit=20');Customizing pivot fields via PHP hook
Section titled “Customizing pivot fields via PHP hook”Hook Entries::resolvePivotFields in site/ready.php to reshape pivot fields dynamically based on the selected target:
<?php
declare(strict_types=1);
namespace ProcessWire;
$wire->addHookAfter('Entries::resolvePivotFields', function (HookEvent $event): void { $target = $event->arguments(0); $fields = (array) $event->return;
if ($target !== null && $target->get('category') === 'water_toys') { $fields['requires_license'] = [ 'type' => 'toggle', 'label' => 'Operator License Required', ]; }
$event->return = $fields;});