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

Field not normalized on action request #108

Open
jerome2710 opened this issue Dec 3, 2020 · 12 comments
Open

Field not normalized on action request #108

jerome2710 opened this issue Dec 3, 2020 · 12 comments
Labels
bug Something isn't working

Comments

@jerome2710
Copy link

Hi!

May I ask why the field content is not normalized when the request is an action request (https://github.com/chasegiunta/craft-jason/blob/master/src/fields/JasonField.php#L126)?

We are displaying Jason contents on a page which also includes a MailChimp subscribe form. After posting the subscribe form and returning to the page, the Jason content is not decoded and is causing errors now.

@chasegiunta
Copy link
Owner

#73 ..?

@jerome2710
Copy link
Author

#73 seems to be the cause of this bug indeed. Turns out !Craft::$app->request->getIsActionRequest() is not the best way to go, as it breaks other functionality. Could you fix this with an alternative?

@chasegiunta
Copy link
Owner

I'd have to look into figuring out test cases for both instances.

Open to PR's if you have the environments set up (only asking since you were on both issues).

I agree, looks like #73 was the wrong bandaid.

@chasegiunta chasegiunta added the bug Something isn't working label Dec 3, 2020
@jerome2710
Copy link
Author

Thank you. I'm not really familiar with Crafts normalizeValue() and need to look into it as well, but I'm now temporarily overcoming this in the template by checking if the value is_array() or not and then performing a json_decode() if needed.

@jerome2710
Copy link
Author

Any updates on this matter? This bug seem to be coming back every once in a while in one of our projects.

@philipzaengle
Copy link

This keeps biting us too, any progress?

@chasegiunta
Copy link
Owner

chasegiunta commented Sep 3, 2021

@philipzaengle I really need to know exactly what cases folks are running into this error so I can tailor the response for each. @jerome2710 was your workaround working (at least in your one project) for all known cases?

@jerome2710
Copy link
Author

@chasegiunta @philipzaengle I have not narrowed it down to specific cases / errors, but instead just wrote a helper-function to retrieve the JSON-data:

public static function getData(Element $element): array
{
    $data = $element->json ?? [];
    if (!empty($data) && is_string($data)) {
        $data = json_decode($data, true) ?? [];
    }

    return $data;
}

So looking up the $element->json does not consistently consists of decoded JSON. If it doesn't and consists of a string, we manually decode it.

@stevecomrie
Copy link

So .. I've been running into this issue and another issue that I think is related to #73 .. and I think the fix is relatively simple.

On this line, changing:

return json_decode($value, true);

to:

return is_array( $value ) ? $value : json_decode($value, true);

Should help with not trying to normalize input that's already formatted properly. Which should make it easier to save data to as Jason field programmatically without experiencing errors.

@zadro
Copy link

zadro commented Mar 9, 2024

Has the author abandoned this plugin? Would be nice to have an update on this or some communication at a minimum.

@chasegiunta
Copy link
Owner

I just unfortunately don't use Craft for my day job, and my one volunteer project with Craft does not use this plugin. I just haven't been able to dedicate the time to test all the use-cases, because apparently there are several scenarios where one change could break another, so I'm a bit fearful to make any changes.

I might hand this plugin over to someone else, but in the mean-time, forking and making the changes necessary for your use-case might be the way to go.

@zadro
Copy link

zadro commented Mar 9, 2024

Thanks, @chasegiunta -- appreciate your message!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants