-
-
Notifications
You must be signed in to change notification settings - Fork 530
Fix fatal error in trim output filter #16757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 3.x
Are you sure you want to change the base?
Conversation
@jenswittmann does that happen outside of pdotools? |
Yes, you can try this with a Snippet that return an Array:
When you use |
@@ -63,7 +63,7 @@ public function filter(&$element) | |||
|
|||
$this->log('Processing Modifier: ' . $m_cmd . ' (parameters: ' . $m_val . ')'); | |||
|
|||
$output = trim($output); | |||
$output = trim((string) $output); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather see the solution test for string rather than cast all values to that type. If I'm not mistaken, there would be legitimate cases where the value being worked with is not a string (e.g., int or bool, etc.), so maybe this:
$output = trim((string) $output); | |
$output = is_string($output) ? trim($output) : $output; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be argued that all output filters should be operating only on strings. Perhaps it is worth looking at bypassing filtering altogether if the value is not a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so; what about the comparison filters (gt, lt, etc.) acting on the integer output of a snippet? The number TV, as it stands, does only output a string representation. But with that TV type I could see there being an input option to force an integer, or a new filter that acts on a strict comparison. I think it just keeps your options (so to speak) open if you don't coerce values passed to the filter to string. That's my 2¢ at least ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I rephrase this to say scalar value, would you agree? Though technically, all elements in MODX that appear in content HAVE to return a string to be useful. You can obviously create snippets that return other (non-scalar) types in situations where you are using that snippet programmatically in PHP code, but when used in content, it is essentially useless. Since output filters are meant to act on the value returned to be used in content, I think my point is valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, definitely agree with bypassing non-scalar vals. So maybe at the top of the filter method, add this:
if (!is_scalar($element->_output)) {
return;
}
Then still test for string to trim instead of coercing as I suggested above?
@smg6511 here's a real-world example that might not work with the discussed fix maybe 🤔 Check if the user has a name in a custom session variable and output a link based on it:
Every output modifier gets trimmed in line 66. So, with return, you always get the else value, right? |
@jenswittmann - No, I did some tests with |
This solves a PHP fatal error that occurs when using something like this:
[[!#SESSION.user.password:trim]]
PHP Error
System