xiaowang/kirby/config/sections/mixins/parent.php

48 lines
1 KiB
PHP
Raw Normal View History

2021-10-29 18:05:46 +02:00
<?php
2022-12-19 16:26:24 +01:00
use Kirby\Cms\File;
use Kirby\Cms\Page;
use Kirby\Cms\Site;
use Kirby\Cms\User;
2021-10-29 18:05:46 +02:00
use Kirby\Exception\Exception;
return [
2022-08-31 16:08:03 +02:00
'props' => [
/**
* Sets the query to a parent to find items for the list
*/
'parent' => function (string $parent = null) {
return $parent;
}
],
'methods' => [
'parentModel' => function () {
$parent = $this->parent;
2021-10-29 18:05:46 +02:00
2022-08-31 16:08:03 +02:00
if (is_string($parent) === true) {
$query = $parent;
$parent = $this->model->query($query);
2021-10-29 18:05:46 +02:00
2022-08-31 16:08:03 +02:00
if (!$parent) {
throw new Exception('The parent for the query "' . $query . '" cannot be found in the section "' . $this->name() . '"');
}
2021-10-29 18:05:46 +02:00
2022-08-31 16:08:03 +02:00
if (
2022-12-19 16:26:24 +01:00
$parent instanceof Page === false &&
$parent instanceof Site === false &&
$parent instanceof File === false &&
$parent instanceof User === false
2022-08-31 16:08:03 +02:00
) {
throw new Exception('The parent for the section "' . $this->name() . '" has to be a page, site or user object');
}
}
2021-10-29 18:05:46 +02:00
2022-08-31 16:08:03 +02:00
if ($parent === null) {
return $this->model;
}
2021-10-29 18:05:46 +02:00
2022-08-31 16:08:03 +02:00
return $parent;
}
]
2021-10-29 18:05:46 +02:00
];