julienmonnerie/kirby/config/fields/writer.php

101 lines
2.8 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
2025-04-21 18:57:21 +02:00
use Kirby\Exception\InvalidArgumentException;
2022-06-17 17:51:59 +02:00
use Kirby\Sane\Sane;
2025-04-21 18:57:21 +02:00
use Kirby\Toolkit\V;
2022-06-17 17:51:59 +02:00
return [
2022-08-31 15:02:43 +02:00
'props' => [
2025-04-21 18:57:21 +02:00
/**
* Enables/disables the character counter in the top right corner
*/
'counter' => function (bool $counter = true) {
return $counter;
},
/**
* Available heading levels
*/
'headings' => function (array|null $headings = null) {
return array_intersect($headings ?? range(1, 6), range(1, 6));
},
2022-08-31 15:02:43 +02:00
/**
* Enables inline mode, which will not wrap new lines in paragraphs and creates hard breaks instead.
*
* @param bool $inline
*/
'inline' => function (bool $inline = false) {
return $inline;
},
/**
2025-04-21 18:57:21 +02:00
* Sets the allowed HTML formats. Available formats: `bold`, `italic`, `underline`, `strike`, `code`, `link`, `email`. Activate/deactivate them all by passing `true`/`false`. Default marks are `bold`, `italic`, `underline`, `strike`, `link`, `email`
2022-08-31 15:02:43 +02:00
* @param array|bool $marks
*/
2025-04-21 18:57:21 +02:00
'marks' => function ($marks = null) {
2022-08-31 15:02:43 +02:00
return $marks;
},
/**
2025-04-21 18:57:21 +02:00
* Maximum number of allowed characters
*/
'maxlength' => function (int|null $maxlength = null) {
return $maxlength;
},
/**
* Minimum number of required characters
*/
'minlength' => function (int|null $minlength = null) {
return $minlength;
},
/**
* Sets the allowed nodes. Available nodes: `paragraph`, `heading`, `bulletList`, `orderedList`, `quote`. Activate/deactivate them all by passing `true`/`false`. Default nodes are `paragraph`, `heading`, `bulletList`, `orderedList`.
2022-08-31 15:02:43 +02:00
* @param array|bool|null $nodes
*/
'nodes' => function ($nodes = null) {
return $nodes;
2025-04-21 18:57:21 +02:00
},
/**
* Toolbar options, incl. `marks` (to narrow down which marks should have toolbar buttons), `nodes` (to narrow down which nodes should have toolbar dropdown entries) and `inline` to set the position of the toolbar (false = sticking on top of the field)
*/
'toolbar' => function ($toolbar = null) {
return $toolbar;
2022-08-31 15:02:43 +02:00
}
],
'computed' => [
'value' => function () {
$value = trim($this->value ?? '');
2025-04-21 18:57:21 +02:00
$value = Sane::sanitize($value, 'html');
// convert non-breaking spaces to HTML entity
// as that's how ProseMirror handles it internally;
// will allow comparing saved and current content
$value = str_replace(' ', '&nbsp;', $value);
return $value;
2022-08-31 15:02:43 +02:00
}
],
2025-04-21 18:57:21 +02:00
'validations' => [
'minlength' => function ($value) {
if (
$this->minlength &&
V::minLength(strip_tags($value), $this->minlength) === false
) {
2025-07-11 14:41:34 +02:00
throw new InvalidArgumentException(
key: 'validation.minlength',
data: ['min' => $this->minlength]
);
2025-04-21 18:57:21 +02:00
}
},
'maxlength' => function ($value) {
if (
$this->maxlength &&
V::maxLength(strip_tags($value), $this->maxlength) === false
) {
2025-07-11 14:41:34 +02:00
throw new InvalidArgumentException(
key: 'validation.maxlength',
data: ['max' => $this->maxlength]
);
2025-04-21 18:57:21 +02:00
}
},
]
2022-06-17 17:51:59 +02:00
];