-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.php
executable file
·102 lines (91 loc) · 3.08 KB
/
index.php
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
<?php
use Kirby\Cms\Template;
include __DIR__ . '/lib/models.php';
include __DIR__ . '/lib/functions.php';
include __DIR__ . '/lib/collection.php';
$moduleRegistry = createModuleRegistry();
Kirby::plugin('medienbaecker/modules', [
'templates' => $moduleRegistry['templates'],
'pageModels' => $moduleRegistry['pageModels'],
'blueprints' => $moduleRegistry['blueprints'],
'sections' => [
'modules' => include __DIR__ . '/lib/sections/modules.php'
],
'fields' => [
'modules_redirect' => include __DIR__ . '/lib/fields/redirect.php'
],
'pageMethods' => [
'renderModules' => function (array $params = []) {
foreach ($this->modules() as $module) {
$moduleTemplate = new Template($module->intendedTemplate());
echo $moduleTemplate->render([
'page' => $this,
'module' => $module,
'site' => $this->site(),
...$params
]);
}
},
'hasModules' => function () {
$modules = array_filter($this->blueprint()->sections(), function ($section) {
return 'modules' === $section->type();
});
return count($modules) > 0;
},
'modules' => function () {
$modules = new ModulesCollection;
if ($rawModules = $this->find('modules')) {
foreach ($rawModules->childrenAndDrafts() as $module) {
if (!$module->isListed() && !$module->isDraft()) continue;
if ($module->isDraft && !$module->isVerified(get('token'))) continue;
$modules->append($module);
}
}
return $modules;
},
'isModule' => function () {
return Str::startsWith($this->intendedTemplate(), 'module.');
},
'uniqueModuleSlug' => function() {
$slug = $this->title()->slug();
$siblings = $this->parent()?->parent()?->childrenAndDrafts() ?? new Pages();
// if the slug is already unique, return it
if ($siblings->filterBy('slug', $slug)->count() === 0) {
return $slug;
}
// if the slug is not unique, add a number
$i = 2;
while ($siblings->filterBy('slug', $slug . '-' . $i)->count() > 0) {
$i++;
}
return $slug . '-' . $i;
}
],
'api' => [
'routes' => include __DIR__ . '/lib/routes.php',
],
'translations' => include __DIR__ . '/lib/translations.php',
'commands' => [
'make:module' => [
'description' => 'Creates a new module',
'args' => [
'name' => [
'description' => 'The name of the module',
]
],
'command' => static function ($cli): void {
$kirby = $cli->kirby();
$name = $cli->argOrPrompt('name', 'Enter a name for the module:');
$blueprintFile = $kirby->root('site') . '/modules/' . $name . '/'. $name . '.yml';
$snippetFile = $kirby->root('site') . '/modules/' . $name . '/'. $name . '.php';
$cli->make($blueprintFile, 'title: {{ title }}', [
'title' => ucfirst($name)
]);
$cli->make($snippetFile, '<!-- {{ title }} -->', [
'title' => ucfirst($name)
]);
$cli->success('The module has been created');
}
]
]
]);