putyourlightson / craft-campaign

Send and manage email campaigns, contacts and mailing lists in Craft CMS.
https://putyourlightson.com/plugins/campaign
Other
63 stars 25 forks source link

Using a mailing lists on different sites #401

Closed mikewink closed 1 year ago

mikewink commented 1 year ago

Hello PutYourLightsOnTeam,

I am using Craft CMS in a multisite configuration. The main site has a mailing list attached to it. I have some other sub sites that should use it in their templates too.

I use the code from the documentation for my subscription form. The code below is from the docs!

{% set mailingList = craft.campaign.mailingLists.id(7).one() %}

...

<form id="subscribe-form" method="post">
    {{ csrfInput() }}
    {{ actionInput('campaign/forms/subscribe') }}
    {{ redirectInput('subscribe-success') }}
    {{ hiddenInput('mailingList', mailingList.slug) }} <-- where the error hits

    {# Specify a site ID – required only if the mailing list does not exist in the current site. #}
    {{ hiddenInput('siteId', 2) }}
...
</form>

When I use this code on a sub site, I get an error message.

Impossible to access an attribute ("slug") on a null variable.

The initial query for the mailing list is already resulting in null. How would the hidden input for the siteId help with that? I tried adding the site ID as part of the mailing list query. I had no success. How would I query for the mailing list from the main site?

Thank you very much for any tips on how to do it.

Best regards,

Mike

bencroker commented 1 year ago

As with entry queries, the site to select from must be specified if not the current site. Here's how are you can do it with a wildcard as the site ID.

{% set mailingList = craft.campaign.mailingLists
    .id(7)
    .siteId('*')
    .one() 
%}