Skip to content

Commit d3fa9a5

Browse files
first steps
1 parent ede54c8 commit d3fa9a5

File tree

4 files changed

+96
-24
lines changed

4 files changed

+96
-24
lines changed

src/Form/Field/BlocksField.php

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Kirby\Data\Json;
1212
use Kirby\Exception\InvalidArgumentException;
1313
use Kirby\Exception\NotFoundException;
14+
use Kirby\Form\Fields;
1415
use Kirby\Form\FieldClass;
1516
use Kirby\Form\Form;
1617
use Kirby\Form\Mixin\EmptyState;
@@ -25,29 +26,60 @@ class BlocksField extends FieldClass
2526
use Max;
2627
use Min;
2728

29+
/**
30+
* Defines the allowed block types in the blocks field. See below.
31+
*/
2832
protected Fieldsets $fieldsets;
2933
protected array $forms;
34+
35+
/**
36+
* Group name to identify all block fields that can share blocks via drag & drop
37+
*/
3038
protected string|null $group;
39+
40+
/**
41+
* Saves pretty printed JSON in text files
42+
*/
3143
protected bool $pretty;
3244
protected mixed $value = [];
3345

3446
public function __construct(
35-
string|array|null $empty = null,
36-
string|array|null $fieldsets = null,
47+
bool $autofocus = false,
48+
array $default = [],
49+
bool $disabled = false,
50+
array|string|null $empty = null,
51+
array|string|null $fieldsets = null,
52+
array|string|null $help = null,
3753
string|null $group = null,
54+
array|string|null $label = null,
55+
ModelWithContent|null $model = null,
56+
string|null $name = null,
3857
int|null $max = null,
3958
int|null $min = null,
4059
bool $pretty = false,
41-
...$props
60+
bool $required = false,
61+
Fields|null $siblings = null,
62+
bool $translate = true,
63+
array $when = [],
64+
string|null $width = '1/1',
4265
) {
43-
$this->setFieldsets(
44-
$fieldsets,
45-
$props['model'] ?? App::instance()->site()
66+
parent::__construct(
67+
autofocus: $autofocus,
68+
default: $default,
69+
disabled: $disabled,
70+
help: $help,
71+
label: $label,
72+
model: $model,
73+
name: $name,
74+
required: $required,
75+
siblings: $siblings,
76+
translate: $translate,
77+
when: $when,
78+
width: $width
4679
);
4780

48-
parent::__construct(...$props);
49-
5081
$this->setEmpty($empty);
82+
$this->setFieldsets($fieldsets);
5183
$this->setGroup($group);
5284
$this->setMax($max);
5385
$this->setMin($min);
@@ -262,17 +294,14 @@ protected function setDefault(mixed $default = null): void
262294
parent::setDefault($default);
263295
}
264296

265-
protected function setFieldsets(
266-
string|array|null $fieldsets,
267-
ModelWithContent $model
268-
): void {
297+
protected function setFieldsets(string|array|null $fieldsets): void {
269298
if (is_string($fieldsets) === true) {
270299
$fieldsets = [];
271300
}
272301

273302
$this->fieldsets = Fieldsets::factory(
274303
$fieldsets,
275-
['parent' => $model]
304+
['parent' => $this->model]
276305
);
277306
}
278307

src/Form/Mixin/Disabled.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
trait Disabled
66
{
7-
protected bool $disabled = false;
8-
97
/**
108
* If `true`, the field is no longer editable and will not be saved
119
*/
10+
protected bool $disabled = false;
11+
1212
public function disabled(): bool
1313
{
1414
return $this->disabled;

src/Reflection/DocComment.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Kirby\Reflection;
4+
5+
use ReflectionClass;
6+
use ReflectionFunction;
7+
use ReflectionMethod;
8+
use ReflectionProperty;
9+
10+
class DocComment
11+
{
12+
public function __construct(
13+
protected string $comment,
14+
) {
15+
}
16+
17+
public static function from(
18+
ReflectionClass|ReflectionFunction|ReflectionMethod|ReflectionProperty|null $object
19+
) {
20+
return new static($object?->getDocComment() ?? '');
21+
}
22+
23+
/**
24+
* Returns the cleaned docblock text of the given property.
25+
*/
26+
public function description(): string|null
27+
{
28+
$comment = preg_replace(['#^/\*\*#', '#\*/$#'], '', $this->comment);
29+
$lines = preg_split('/\R/', (string)$comment) ?: [];
30+
$lines = array_map(static fn(string $line): string => ltrim(trim($line), "* \t"), $lines);
31+
$lines = array_filter(
32+
$lines,
33+
static fn(string $line): bool => $line !== '' && str_starts_with($line, '@') === false
34+
);
35+
36+
return implode(' ', $lines);
37+
}
38+
}

src/Reflection/Field.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,29 @@ public function __construct(
1818
$this->constructor = new Constructor($field);
1919
}
2020

21+
/**
22+
* Returns field properties based on the constructor signature.
23+
*/
2124
public function props(): array
2225
{
23-
$props = [];
26+
$props = [];
27+
$ignore = ['model', 'siblings', 'props'];
2428

2529
foreach ($this->constructor->getParameters() as $parameter) {
26-
$name = $parameter->getName();
27-
$property = $this->class->hasProperty($name) ? $this->class->getProperty($name) : null;
28-
29-
dump($property?->getDocComment());
30+
$name = $parameter->getName();
3031

31-
if ($name === 'props') {
32+
if (in_array($name, $ignore) === true) {
3233
continue;
3334
}
3435

36+
$property = $this->class->hasProperty($name) ? $this->class->getProperty($name) : null;
37+
$comment = DocComment::from($property);
38+
3539
$props[$name] = [
36-
'name' => $name,
37-
'type' => (string)$parameter->getType(),
38-
'default' => $parameter->getDefaultValue()
40+
'name' => $name,
41+
'type' => (string)$parameter->getType(),
42+
'default' => $parameter->getDefaultValue(),
43+
'description' => $comment->description()
3944
];
4045
}
4146

0 commit comments

Comments
 (0)