Custom PHP Classes
Just like ProcessWire Custom Page Classes (site/classes/), Entries supports companion PHP classes placed right next to your blueprints in site/entries/. Scaffold them automatically with:
php vendor/bin/wire make:entry seasons --kind=collection --classNaming conventions and base classes
Section titled “Naming conventions and base classes”| Blueprint Kind | Example Blueprint | Companion File Path | Base Class |
|---|---|---|---|
collection (wrapper) |
seasons |
site/entries/collections/SeasonsCollection.php |
Totoglu\Entries\Entity\EntryCollection |
collection (record) |
seasons |
site/entries/collections/SeasonItem.php |
Totoglu\Entries\Entity\EntryItem |
single |
general_settings |
site/entries/singles/GeneralSettingsSingle.php |
Totoglu\Entries\Entity\EntrySingle |
block |
itinerary_day |
site/entries/blocks/ItineraryDayBlock.php |
Totoglu\Entries\Entity\EntryBlock |
Custom EntryItem with computed columns, validation, and hooks
Section titled “Custom EntryItem with computed columns, validation, and hooks”Any public method on your EntryItem subclass can be referenced directly in blueprint columns: [title, code, dateRangeLabel] or compute: dateRangeLabel.
<?php
declare(strict_types=1);
namespace ProcessWire;
use Totoglu\Entries\Entity\EntryItem;
/** * @property string $title * @property string $code * @property int|null $start_date * @property int|null $end_date * @property float $rate_multiplier */class SeasonItem extends EntryItem{ protected string $blueprintType = 'seasons';
/** * Usable directly in blueprint `columns:` or `compute: dateRangeLabel`. */ public function dateRangeLabel(): string { $start = (int) $this->getUnformatted('start_date'); $end = (int) $this->getUnformatted('end_date'); if ($start <= 0 || $end <= 0) { return '-'; }
return date('d M', $start) . ' to ' . date('d M', $end); }
/** * Custom validation merged with blueprint field rules. * * @return array<string, string> */ public function validate(): array { $errors = parent::validate();
$start = (int) $this->getUnformatted('start_date'); $end = (int) $this->getUnformatted('end_date'); if ($start > 0 && $end > 0 && $end < $start) { $errors['end_date'] = $this->_('End Date must be on or after Start Date.'); }
return $errors; }
public function beforeSave(): void { $this->code = strtoupper(trim((string) $this->code)); }
public function afterSave(): void { $this->wire()->log->save('entries', "Saved season {$this->code}"); }
public function beforeDelete(): void { }
public function afterDelete(): void { }}Code-first inline schema() (zero YAML required)
Section titled “Code-first inline schema() (zero YAML required)”If you want both the schema definition and helper methods in a single PHP class, implement public static function schema(): array on your EntrySingle, EntryCollection, or EntryBlock subclass:
<?php
declare(strict_types=1);
namespace ProcessWire;
use Totoglu\Entries\Entity\EntrySingle;
class CharterSettingsSingle extends EntrySingle{ protected string $blueprintType = 'charter_settings';
/** * @return array<string, mixed> */ public static function schema(): array { return [ 'title' => 'Charter Settings', 'icon' => 'sliders', 'fields' => [ 'default_vat' => [ 'type' => 'float', 'label' => 'Default VAT (%)', 'default' => 20.0, ], ], ]; }
public function grossAmount(float $net): float { return round($net * (1 + ((float) $this->get('default_vat') / 100)), 2); }}Calling companion classes in templates and services
Section titled “Calling companion classes in templates and services”<?php
declare(strict_types=1);
namespace ProcessWire;
$entries = wire()->entries;
// Collection wrapper$peakSeasons = $entries->collection(SeasonsCollection::class)->find('is_peak=1, sort=-rate_multiplier');
// Single class (explicit or shorthand invocation)$settings = $entries->single(CharterSettingsSingle::class);$gross = $entries(CharterSettingsSingle::class)->grossAmount(12000.0);