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

[Feature request] Create a template variable to access SubscribeResponse #116

Open
architchandra opened this issue Jun 7, 2021 · 14 comments

Comments

@architchandra
Copy link

architchandra commented Jun 7, 2021

There are times when the mailchimpSubscribe global variable is not accessible, or contains incorrect values (eg. when caching the page). In those cases, it would be great to have an easily accessible template variable to check the correct response status inside a nocache block (can be called something like craft.mailchimpSubscribe.subscribeResponse).

@billmn
Copy link

billmn commented Jan 13, 2022

I have used this plugin inside a Sprig component and I has some problems because after a successful subscription, the mailchimpSubscribe was not accessible.

A template variable would be a great addition!

@outline4
Copy link

hi, any chance this works with sprig already?

@cole007
Copy link

cole007 commented May 23, 2022

Got to the bottom of this (re sprig) but would need a plugin update to resolve I think.
Not sure if this is a sprig thing or a Mailchimp Subscribe thing but a redirect parameter is being assigned by sprig on the submission, even if empty or forced eg s-val:redirect="".

This means in essence it can never complete because the response is being treated as a redirectToPostedUrl() rather than passing back with the relevant variables/parameters.

I imagine that this needs to be set to following or similar:

return $this->redirectToPostedUrl(['mailchimpSubscribe' => $result]);

@bencroker do you know if this is a problem that can be fixed at the Sprig level?

@bencroker
Copy link

@cole007 Why can't the redirect parameter be set to a blank string?

@cole007
Copy link

cole007 commented May 23, 2022

i have done but for some reason it is still being passed through (or at least interpreted) as a hashed string by the time it hits the plugin controller.

@bencroker
Copy link

Get to the bottom of that and you should be able to solve it!

@aelvan
Copy link
Owner

aelvan commented May 23, 2022

Mailchimp Subscribe does not redirect if the posted redirect param is an empty string.

@cole007
Copy link

cole007 commented May 23, 2022

This is why I am confused as to where the redirect variable is coming from (when sent via Sprig) - if there is no redirect specified or a redirect of "" is specified I can see in the plugin controller that it gets a redirect value.

So for example in my sprig form I have added the following parameter:

s-val:redirect=""

Then if in the plugin controller I insert the following:

// if a redirect variable was passed, do redirect
if ($redirect !== '' && $result['success']) {
    Craft::dd($redirect);
    return $this->redirectToPostedUrl();
}

The following is returned:

ce1ef4f0d86c8a8fec5f746393be0c5dfb3f48fe6e962501b5b09b8eae5cb5cbhttps://{id}'

Even though if I inspect the XHR submission the value for redirect is empty.

Not sure if this is happening at a sprig level or a Craft level or in this plugin 🤷‍♂️

@aelvan
Copy link
Owner

aelvan commented May 23, 2022

@bencroker
Copy link

Thanks @aelvan, so we're back to square one, that the plugin should ideally return the result as part of the redirect, as is the convention with Craft controllers.

return $this->redirectToPostedUrl(['mailchimpSubscribe' => $result]);

@mmikkel
Copy link
Collaborator

mmikkel commented May 23, 2022

@bencroker Hm, I don't think that would do anything. An $object passed to redirectToPostedUrl() is only used by Craft in parsing the 'redirect' POST parameter (if there is one) for properties before doing the redirect – it doesn't set the data as route params or anything like that.

I guess a quick fix on MailChimp Subscribe's end could be to make sure that the mailchimpSubscribe route param is set before that conditional redirect. However, since the plugin is retired and this isn't a regression, I'm not sure if it's worth the effort at this point? FWIW I also think the real issue is how Sprig sets a commonly used POST parameter like that. If technically feasible, perhaps it could use a different parameter name, as suggested by putyourlightson/craft-sprig-core#5 (comment)?

@bencroker
Copy link

bencroker commented May 23, 2022

I think you're right @aelvan! What even is the issue @cole007? Is this check provided in putyourlightson/craft-sprig-core#5 (comment) not sufficient?

{% if success is defined and success %}
    Subscribed successfully!
{% else %}
    Do something with the `mailchimpSubscribe` variable.
{% endif %}

@cole007
Copy link

cole007 commented May 23, 2022

Thanks - that partly seems to work (sorry, was expecting these to be accessible via the {{ mailchimpSubscribe }} object, not via the specific attribute.

One oddity though on testing is that success seems to work on a successfully completed form but if the form fails then the {{ mailchimpSubscribe }} object is returned.

This should be enough to get me going though, thanks both.

even if I hit an error the response is showing as successful.
Eg if wrong value sent through to a pre-defined field (i.e. a dropdown list)

@bencroker
Copy link

Yes, given how the plugin controller works, that is the expected behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants