-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtension.php
More file actions
115 lines (100 loc) · 4.82 KB
/
Extension.php
File metadata and controls
115 lines (100 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace Acme\Starter;
use Acme\Starter\Command\GreetCommand;
use TotalCMS\Domain\Extension\Data\AdminNavItem;
use TotalCMS\Domain\Extension\Data\DashboardWidget;
use TotalCMS\Domain\Extension\ExtensionContext;
use TotalCMS\Domain\Extension\ExtensionInterface;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* Starter extension demonstrating every extension point.
*
* Use this as a template for building your own extensions.
* Delete the parts you don't need.
*/
class Extension implements ExtensionInterface
{
public function register(ExtensionContext $context): void
{
// ── Twig Functions ──────────────────────────────────────────────
// Available in all templates: {{ starter_greet('World') }}
$context->addTwigFunction(
new TwigFunction('starter_greet', function (string $name): string {
return "Hello, {$name}!";
})
);
// ── Twig Filters ────────────────────────────────────────────────
// Available in templates: {{ text|reverse_words }}
$context->addTwigFilter(
new TwigFilter('reverse_words', function (string $text): string {
return implode(' ', array_reverse(explode(' ', $text)));
})
);
// ── CLI Commands ────────────────────────────────────────────────
// Run with: tcms acme:greet --name=World
$context->addCommand(new GreetCommand());
// ── Admin Navigation ────────────────────────────────────────────
// Adds a link to the admin sidebar
$context->addAdminNavItem(new AdminNavItem(
label: 'Starter',
icon: 'starter',
url: '/ext/acme/starter/dashboard',
permission: 'admin',
priority: 80,
));
// ── Dashboard Widget ────────────────────────────────────────────
// Adds a widget to the admin home screen
$context->addDashboardWidget(new DashboardWidget(
id: 'starter-widget',
label: 'Starter Widget',
template: 'widgets/starter.twig',
position: 'sidebar',
priority: 50,
));
// ── Event Listeners ─────────────────────────────────────────────
// React to content changes
$context->addEventListener('object.created', function (array $payload): void {
// Called after any object is created in any collection
// $payload contains: 'collection' and 'id'
error_log("[Starter] Object created: {$payload['collection']}/{$payload['id']}");
});
$context->addEventListener('object.updated', function (array $payload): void {
error_log("[Starter] Object updated: {$payload['collection']}/{$payload['id']}");
});
// ── Custom Field Types ──────────────────────────────────────────
// Register a new field type usable in schemas (class must extend FormField)
// $context->addFieldType('colorpicker', \Acme\Starter\Fields\ColorPickerField::class);
// ── Routes ──────────────────────────────────────────────────────
// Register admin pages at /ext/acme/starter/...
$context->addRoutes(function (\Slim\Routing\RouteCollectorProxy $group): void {
$group->get('/dashboard', Action\DashboardAction::class);
});
}
public function boot(ExtensionContext $context): void
{
// The boot phase runs after ALL extensions have registered.
// Use $context->get() to resolve services from the DI container.
//
// Example: read a setting configured by the admin
$greeting = $context->setting('greeting', 'Hello');
// Example: resolve a core service
// $config = $context->get(\TotalCMS\Support\Config::class);
// ── Installable Schemas ─────────────────────────────────────────
// Install a user-editable schema into tcms-data/.schemas/ (Pro+ only).
// Skips if the schema already exists. For read-only schemas managed
// by the extension, place them in the schemas/ directory instead.
//
// $context->installSchema([
// 'id' => 'starter-reviews',
// 'description' => 'Customer reviews',
// 'properties' => [
// 'rating' => ['type' => 'number', 'label' => 'Rating'],
// 'review' => ['type' => 'styledtext', 'label' => 'Review'],
// ],
// 'required' => ['id', 'rating'],
// 'index' => ['id', 'rating'],
// ]);
}
}