3.4.0
This commit is contained in:
@@ -2,14 +2,11 @@
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Cms\Field;
|
||||
use Kirby\Cms\File;
|
||||
use Kirby\Cms\Files;
|
||||
use Kirby\Cms\Html;
|
||||
use Kirby\Cms\Page;
|
||||
use Kirby\Cms\Structure;
|
||||
use Kirby\Cms\Url;
|
||||
use Kirby\Data\Json;
|
||||
use Kirby\Data\Yaml;
|
||||
use Kirby\Data\Data;
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Toolkit\Str;
|
||||
@@ -67,9 +64,8 @@ return function (App $app) {
|
||||
'toData' => function (Field $field, string $method = ',') {
|
||||
switch ($method) {
|
||||
case 'yaml':
|
||||
return Yaml::decode($field->value);
|
||||
case 'json':
|
||||
return Json::decode($field->value);
|
||||
return Data::decode($field->value, $method);
|
||||
default:
|
||||
return $field->split($method);
|
||||
}
|
||||
@@ -216,7 +212,7 @@ return function (App $app) {
|
||||
*/
|
||||
'toStructure' => function (Field $field) {
|
||||
try {
|
||||
return new Structure(Yaml::decode($field->value), $field->parent());
|
||||
return new Structure(Data::decode($field->value, 'yaml'), $field->parent());
|
||||
} catch (Exception $e) {
|
||||
if ($field->parent() === null) {
|
||||
$message = 'Invalid structure data for "' . $field->key() . '" field';
|
||||
@@ -287,6 +283,17 @@ return function (App $app) {
|
||||
|
||||
// manipulators
|
||||
|
||||
/**
|
||||
* Applies the callback function to the field
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @param \Kirby\Cms\Field $field
|
||||
* @param Closure $callback
|
||||
*/
|
||||
'callback' => function (Field $field, Closure $callback) {
|
||||
return $callback($field);
|
||||
},
|
||||
|
||||
/**
|
||||
* Escapes the field value to be safely used in HTML
|
||||
* templates without the risk of XSS attacks
|
||||
@@ -309,7 +316,7 @@ return function (App $app) {
|
||||
* @param string $rep
|
||||
* @return \Kirby\Cms\Field
|
||||
*/
|
||||
'excerpt' => function (Field $field, int $chars = 0, bool $strip = true, string $rep = '…') {
|
||||
'excerpt' => function (Field $field, int $chars = 0, bool $strip = true, string $rep = ' …') {
|
||||
$field->value = Str::excerpt($field->kirbytext()->value(), $chars, $strip, $rep);
|
||||
return $field;
|
||||
},
|
||||
@@ -326,14 +333,20 @@ return function (App $app) {
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts all line breaks in the field content to `<br>` tags.
|
||||
* Strips all block-level HTML elements from the field value,
|
||||
* it can be safely placed inside of other inline elements
|
||||
* without the risk of breaking the HTML structure.
|
||||
* @since 3.3.0
|
||||
*
|
||||
* @param \Kirby\Cms\Field $field
|
||||
* @return \Kirby\Cms\Field
|
||||
*/
|
||||
'nl2br' => function (Field $field) {
|
||||
$field->value = nl2br($field->value, false);
|
||||
'inline' => function (Field $field) {
|
||||
// List of valid inline elements taken from: https://developer.mozilla.org/de/docs/Web/HTML/Inline_elemente
|
||||
// Obsolete elements, script tags, image maps and form elements have
|
||||
// been excluded for safety reasons and as they are most likely not
|
||||
// needed in most cases.
|
||||
$field->value = strip_tags($field->value, '<b><i><small><abbr><cite><code><dfn><em><kbd><strong><samp><var><a><bdo><br><img><q><span><sub><sup>');
|
||||
return $field;
|
||||
},
|
||||
|
||||
@@ -384,24 +397,6 @@ return function (App $app) {
|
||||
return $field;
|
||||
},
|
||||
|
||||
/**
|
||||
* Strips all block-level HTML elements from the field value,
|
||||
* it can be safely placed inside of other inline elements
|
||||
* without the risk of breaking the HTML structure.
|
||||
* @since 3.3.0
|
||||
*
|
||||
* @param \Kirby\Cms\Field $field
|
||||
* @return \Kirby\Cms\Field
|
||||
*/
|
||||
'inline' => function (Field $field) {
|
||||
// List of valid inline elements taken from: https://developer.mozilla.org/de/docs/Web/HTML/Inline_elemente
|
||||
// Obsolete elements, script tags, image maps and form elements have
|
||||
// been excluded for safety reasons and as they are most likely not
|
||||
// needed in most cases.
|
||||
$field->value = strip_tags($field->value, '<b><i><small><abbr><cite><code><dfn><em><kbd><strong><samp><var><a><bdo><br><img><q><span><sub><sup>');
|
||||
return $field;
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts the field content to lowercase
|
||||
*
|
||||
@@ -425,13 +420,55 @@ return function (App $app) {
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts the field content to valid XML
|
||||
* Converts all line breaks in the field content to `<br>` tags.
|
||||
* @since 3.3.0
|
||||
*
|
||||
* @param \Kirby\Cms\Field $field
|
||||
* @return \Kirby\Cms\Field
|
||||
*/
|
||||
'xml' => function (Field $field) {
|
||||
$field->value = Xml::encode($field->value);
|
||||
'nl2br' => function (Field $field) {
|
||||
$field->value = nl2br($field->value, false);
|
||||
return $field;
|
||||
},
|
||||
|
||||
/**
|
||||
* Uses the field value as Kirby query
|
||||
*
|
||||
* @param \Kirby\Cms\Field $field
|
||||
* @param string|null $expect
|
||||
* @return mixed
|
||||
*/
|
||||
'query' => function (Field $field, string $expect = null) use ($app) {
|
||||
if ($parent = $field->parent()) {
|
||||
return $parent->query($field->value, $expect);
|
||||
}
|
||||
|
||||
return Str::query($field->value, [
|
||||
'kirby' => $app,
|
||||
'site' => $app->site(),
|
||||
'page' => $app->page()
|
||||
]);
|
||||
},
|
||||
|
||||
/**
|
||||
* It parses any queries found in the field value.
|
||||
*
|
||||
* @param \Kirby\Cms\Field $field
|
||||
* @param array $data
|
||||
* @param string $fallback Fallback for tokens in the template that cannot be replaced
|
||||
* @return \Kirby\Cms\Field
|
||||
*/
|
||||
'replace' => function (Field $field, array $data = [], string $fallback = '') use ($app) {
|
||||
if ($parent = $field->parent()) {
|
||||
$field->value = $field->parent()->toString($field->value, $data, $fallback);
|
||||
} else {
|
||||
$field->value = Str::template($field->value, array_replace([
|
||||
'kirby' => $app,
|
||||
'site' => $app->site(),
|
||||
'page' => $app->page()
|
||||
], $data), $fallback);
|
||||
}
|
||||
|
||||
return $field;
|
||||
},
|
||||
|
||||
@@ -475,7 +512,7 @@ return function (App $app) {
|
||||
* Splits the field content into an array
|
||||
*
|
||||
* @param \Kirby\Cms\Field $field
|
||||
* @return \Kirby\cms\Field
|
||||
* @return array
|
||||
*/
|
||||
'split' => function (Field $field, $separator = ',') {
|
||||
return Str::split((string)$field->value, $separator);
|
||||
@@ -504,6 +541,17 @@ return function (App $app) {
|
||||
return $field;
|
||||
},
|
||||
|
||||
/**
|
||||
* Converts the field content to valid XML
|
||||
*
|
||||
* @param \Kirby\Cms\Field $field
|
||||
* @return \Kirby\Cms\Field
|
||||
*/
|
||||
'xml' => function (Field $field) {
|
||||
$field->value = Xml::encode($field->value);
|
||||
return $field;
|
||||
},
|
||||
|
||||
// aliases
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user