Skip to content
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

toPlaceholders wrong behavior #16370

Open
krava77 opened this issue Jan 30, 2023 · 3 comments
Open

toPlaceholders wrong behavior #16370

krava77 opened this issue Jan 30, 2023 · 3 comments
Labels
bug The issue in the code or project, which should be addressed.

Comments

@krava77
Copy link

krava77 commented Jan 30, 2023

Bug report

Summary

If an array value is empty toPlaceholders function does not clear the value in placeholder and leaves the previous value.

Step to reproduce

here is the result:
изображение

here is my code for the reproducing issue:
$item = $object->toArray(); echo "<pre>Object Data: {$item['ends_at']} <br>"; $modx->toPlaceholders( $object, 'subscription'); echo "placeholder: {$modx->getPlaceholder( 'subscription.ends_at' )}</pre>";

Expected behavior

the function should clear the placeholder if the value is NULL or empty.

Environment

MODX Revolution 3.0.1-pl (traditional)
PHP Version 8.0.25
Ubuntu FPM/FastCGI
paas2.fra.modxcloud.com
10.3.34-MariaDB-0ubuntu0.20.04.1

@krava77 krava77 added the bug The issue in the code or project, which should be addressed. label Jan 30, 2023
@Mark-H
Copy link
Collaborator

Mark-H commented Jan 30, 2023

$modx->toPlaceholders() isn't scoped to any particular row or item, so that's going to remember a previous value if you don't set a new one.

Can you use var_dump() to confirm that $item['ends_at'] is a literal null? Looking at the code, it should still overwrite the previous value if it is provided an empty string '', but null isn't a scalar value so is indeed ignored.

Alternatively, consider rewriting your code to use $modx->getChunk($tpl, $item). That does scope placeholders to only be processed within that specific chunk.

@krava77
Copy link
Author

krava77 commented Jan 31, 2023

Hi Mark!
Thank you for the answer.
Indeed the ["ends_at"]=>NULL . I get these values from the database.
So is there no way to add checking null value in the toPlaceholders function? Just creating my own checking of values?

@krava77
Copy link
Author

krava77 commented Jan 31, 2023

I've resolved this issue by adding the following code before toPlaceholders, but maybe it will be useful to add this checking to the function to avoid unexpected results.

$object = array_map(function($v){
        return (is_null($v)) ? "" : $v;
    }, $object->toArray());

or
$object = array_map('strval', $object->toArray());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue in the code or project, which should be addressed.
Projects
None yet
Development

No branches or pull requests

2 participants