From b4ef97a47aadaae23e8e50666ca6406551607728 Mon Sep 17 00:00:00 2001 From: Jelle De Loecker Date: Tue, 13 Feb 2024 12:36:16 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20`Field#castContainedValues()`?= =?UTF-8?q?=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/class/field.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/class/field.js b/lib/class/field.js index 25a7339e..31c9ee43 100644 --- a/lib/class/field.js +++ b/lib/class/field.js @@ -663,7 +663,41 @@ Field.setMethod(function getRecordValue(record, path_to_value_hint) { }); /** - * Cast the given value to this field's type + * Cast the value (could be in a container) + * + * @author Jelle De Loecker + * @since 1.4.0 + * @version 1.4.0 + * + * @param {*} value + * @param {boolean} to_datasource Is this meant for the datasource? + * + * @return {*} + */ +Field.setMethod(function castContainedValues(value, to_datasource) { + + if (this.is_translatable && value && this.is_translatable) { + let prefix; + + for (prefix in value) { + value[prefix] = this.cast(value[prefix], to_datasource); + } + } else if (this.is_array && value) { + let i; + + for (i = 0; i < value.length; i++) { + value[i] = this.cast(value[i], to_datasource); + } + } else { + value = this.cast(value, to_datasource); + } + + return value; +}); + +/** + * Cast the given value to this field's type. + * Containers should not be passed (like the array or translation object container) * * @author Jelle De Loecker * @since 0.2.0