Mailing list subscription forms in Drupal blocks

Published
2008-11-03 01:00
Written by
The idea of creating a Drupal block containing a subscription form for a specific CiviMail mailing list (or for all of them) comes regularly on the forum, so I decided to try out how much work would it be to create one. It turns out this task is fairly simple, if one knows the proper URLs for CiviMail subscription pages. The URLs are of the form …civicrm/mailing/subscribe?reset=1 (for all mailing lists) and …civicrm/mailing/subscribe?reset=1&gid=X (for a mailing list with group id of X). For example, these two URLs point to subscription pages on our Drupal demo installation: http://drupal.demo.civicrm.org/civicrm/mailing/subscribe?reset=1 (all mailing lists), http://drupal.demo.civicrm.org/civicrm/mailing/subscribe?reset=1&gid=2 (the Newsletter Subscribers list). If having a separate page is not really the best outcome, there’s a possibility of creating similar Drupal blocks. The simplest(?) way of doing this is to look at the sources of the above pages, strip them down as much as necessary and then create an HTML Dupal block from the resulting code. From my quick try, it seems like the below is a nice starting point (for a block listing all groups) – of course, in your case you need to adjust the URL, the names of the lists and their respective ids:
<form action="http://drupal.demo.civicrm.org/civicrm/mailing/subscribe" method="post">
  <p>Email: <input name="email" type="text" id="email" /></p>
  <table>
    <tr>
      <td><input name="mark_x_4" type="checkbox" value="1" /></td>
      <td>Advisory Board</td>
    </tr>
    <tr>
      <td><input name="mark_x_2" type="checkbox" value="1" /></td>
      <td>Newsletter Subscribers</td>
    </tr>
    <tr>
      <td><input name="mark_x_3" type="checkbox" value="1" /></td>
      <td>Summer Program Volunteers</td>
    </tr>
  </table>
  <p>
    <input name="_qf_Subscribe_next" value="Subscribe" type="submit" />
  </p>
</form>
Creating a block for just a single list subscription is even easier – all you need to do is to pass the id as a hidden field:
<form action="http://drupal.demo.civicrm.org/civicrm/mailing/subscribe" method="post">
  <p>
    Email: <input name="email" type="text" id="email" />
    <input type="hidden" name="mark_x_2" value="1" />
    <input name="_qf_Subscribe_next" value="Subscribe" type="submit" />
  </p>
</form>
If you have any comments or questions on the above, please feel free to join this thread on our forum. Update: In v2.2 you can also add a hidden field to redirect the user to a specific page once they have subscribed (it will redirect to referer if not specified). The relevant html snippet to add within the form is:
<input type="hidden" name="destination" value="http://www.yahoo.com/" />
Filed under

Comments

Anonymous (not verified)
2008-11-06 - 05:08

Be aware that by doing it this way (pasting an entire HTML form into a block) you are bypassing Drupal's FormAPI and all the security that goes along with it...

-mike

and civicrm takes care of form validation and escaping charaters etc within its form processing framework (which is different from drupal's form api)

lobo

Anonymous (not verified)
2008-11-21 - 21:55

This is great, thank you for posting this. However, how do you set the "redirect" page (to where you want the person to be redirected to) once the subscriber confirms their subscription from the confirmation email? Also, is it possible to collect additional information, i.e. subscribers zip code?

Best to repost these questions on the community forum - CiviMail section:

http://forum.civicrm.org/index.php/board,17.0.html

This is great and works like a charm.

Adam Shaw (not verified)
2009-06-09 - 06:40

we want to collect first and last name and also birthdate. those other fields should be optional (maybe name mandatory), but i can't figure out a way to do this. the URL for civimail-subscribe only has the email field on there. am i missing something?

I also want to capture additional fields (in fact the same ones that you mention). Has anyone got a way of doing this?

Cheers
Martin Fuggle

this is better discussed on the forums. You can also use a profile to collect additional fields

Anonymous (not verified)
2012-04-04 - 15:55

Due to the way Drupal 7 spawns browser sessions, I found that if anonymous users without initialized sessions submit the block, they will not see the confirmation message.

I've posted some code that fixes this here:

http://forum.civicrm.org/index.php/topic,24165.0.html

Anonymous (not verified)
2014-02-19 - 14:55

I followed this page and created the module and installed it:

It works when I am logged in. However when I try to submit the block form as an anonymous user I get the Error:

Sorry but we are not able to provide this at the moment.
We can't load the requested web page. This page requires cookies to be enabled in your browser settings. Please check this setting and enable cookies (if they are not enabled). Then try again. If this error persists, contact the site adminstrator for assistance.

Site Administrators: This error may indicate that users are accessing this page using a domain or URL other than the configured Base URL. EXAMPLE: Base URL is http://example.org, but some users are accessing the page via http://www.example.org or a domain alias like http://myotherexample.org.

Error type: Could not find a valid session key.

I have enabled the module as per http://forum.civicrm.org/index.php/topic,24165.0.html and per cookbook http://packtlib.packtpub.com/library/civicrm-cookbook/ch05lvl1sec59

I have checked to make sure the Base URL is ok.
I have user 0 in the database
I do not user cache for anonymous user.

Any help would be greatly appreciated