From 68d9ec9b42cf778a9996382b986e37347b1c0944 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 28 Mar 2024 10:23:18 -0400 Subject: [PATCH] Fix issue processwire/processwire-issues#1895 --- wire/core/WireTextTools.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/wire/core/WireTextTools.php b/wire/core/WireTextTools.php index e5d653de..9d74ab30 100644 --- a/wire/core/WireTextTools.php +++ b/wire/core/WireTextTools.php @@ -981,7 +981,8 @@ class WireTextTools extends Wire { * - `tagOpen` (string): The required opening tag character(s), default is '{' * - `tagClose` (string): The optional closing tag character(s), default is '}' * - `recursive` (bool): If replacement value contains tags, populate those too? (default=false) - * - `removeNullTags` (bool): If a tag resolves to a NULL, remove it? If false, tag will remain. (default=true) + * - `removeNullTags` (bool): If a tag resolves to a NULL (i.e. field not present), remove it? (default=true) + * - `removeEmptyTags` (bool): If a tag value resolves to blank string, false or NULL, remove it? (default=true) 3.0.237+ * - `entityEncode` (bool): Entity encode the values pulled from $vars? (default=false) * - `entityDecode` (bool): Entity decode the values pulled from $vars? (default=false) * - `allowMarkup` (bool): Allow markup to appear in populated variables? (default=true) @@ -995,7 +996,8 @@ class WireTextTools extends Wire { 'tagOpen' => '{', // opening tag (required) 'tagClose' => '}', // closing tag (optional) 'recursive' => false, // if replacement value contains tags, populate those too? - 'removeNullTags' => true, // if a tag value resolves to a NULL, remove it? If false, tag will be left in tact. + 'removeNullTags' => true, // If a tag resolves to a NULL (i.e. field not present on page), remove it? + 'removeEmptyTags' => true, // If a tag value resolves to blank string, false or null, remove it? 'entityEncode' => false, // entity encode values pulled from $vars? 'entityDecode' => false, // entity decode values pulled from $vars? 'allowMarkup' => true, // allow markup to appear in populated variables? @@ -1015,6 +1017,7 @@ class WireTextTools extends Wire { if(is_object($vars)) { if($vars instanceof Page) { $fieldValue = $options['allowMarkup'] ? $vars->getMarkup($fieldName) : $vars->getText($fieldName); + if($fieldValue === '' && $vars->get($fieldName) === null) $fieldValue = null; } else if($vars instanceof WireData) { $fieldValue = $vars->get($fieldName); } else { @@ -1023,6 +1026,9 @@ class WireTextTools extends Wire { } else if(is_array($vars)) { $fieldValue = isset($vars[$fieldName]) ? $vars[$fieldName] : null; } + + // if value resolves to empty and we are not removing empty tags, then do not add to replacements + if(empty($fieldValue) && !strlen("$fieldValue") && !$options['removeEmptyTags']) continue; // if value resolves to null and we are not removing null tags, then do not add to replacements if($fieldValue === null && !$options['removeNullTags']) continue;