Skip to content

Commit 7cc90b5

Browse files
refact: Validate page moves against page list fields
1 parent b85d42f commit 7cc90b5

2 files changed

Lines changed: 25 additions & 26 deletions

File tree

psalm-baseline.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,6 @@
279279
<code><![CDATA[pattern]]></code>
280280
</UndefinedMethod>
281281
</file>
282-
<file src="src/Guards/PageValidators.php">
283-
<RedundantCondition>
284-
<code><![CDATA[$allowed !== []]]></code>
285-
<code><![CDATA[$allowed !== []]]></code>
286-
</RedundantCondition>
287-
</file>
288282
<file src="src/Http/Cookie.php">
289283
<InvalidArgument>
290284
<code><![CDATA[false]]></code>

src/Guards/PageValidators.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Kirby\Exception\DuplicateException;
1010
use Kirby\Exception\LogicException;
1111
use Kirby\Exception\PermissionException;
12+
use Kirby\Form\Field\PageListField;
13+
use Kirby\Form\Fields;
1214
use Kirby\Toolkit\A;
1315
use Kirby\Toolkit\Str;
1416

@@ -210,33 +212,36 @@ public function validateMoveToTemplate(Site|Page $parent): void
210212
{
211213
$allowed = [];
212214

213-
// collect all allowed subpage templates
214-
// from all pages sections in the blueprint
215-
// (only consider page sections that list pages
216-
// of the targeted new parent page)
217-
$sections = array_filter(
218-
$parent->blueprint()->sections(),
219-
fn ($section) =>
220-
$section->type() === 'pages' &&
221-
$section->parent()->is($parent)
222-
);
215+
// collect all allowed subpage templates from all page list
216+
// fields in the blueprint (only consider fields that list
217+
// pages of the targeted new parent page)
218+
$hasPageList = false;
219+
220+
foreach (Fields::for($parent) as $field) {
221+
if (
222+
$field instanceof PageListField === false ||
223+
$field->parentModel()->is($parent) === false
224+
) {
225+
continue;
226+
}
227+
228+
$hasPageList = true;
223229

224-
// check if the parent has at least one pages section
225-
if ($sections === []) {
230+
// go through all allowed templates and
231+
// add the name to the allowlist
232+
foreach ($field->templates() as $template) {
233+
$allowed[] = $template;
234+
}
235+
}
236+
237+
// check if the parent has at least one page list field
238+
if ($hasPageList === false) {
226239
throw new LogicException(
227240
key: 'page.move.noSections',
228241
data: ['parent' => $parent->id() ?? '/']
229242
);
230243
}
231244

232-
// go through all allowed templates and
233-
// add the name to the allowlist
234-
foreach ($sections as $section) {
235-
foreach ($section->templates() as $template) {
236-
$allowed[] = $template;
237-
}
238-
}
239-
240245
// check if the template of this page is allowed as subpage type
241246
// for the potential new parent
242247
if (

0 commit comments

Comments
 (0)