Simplenews Subscription Block Override - Tutorial 1 of 2
In this Drupal Video Tutorial, we look at the Simplenews module and create a custom module to override the Simplenews subscription block. Using hook_form_alter, we rename the Simplenews save button and remove the Simplenews radio buttons for subscribe and unsubscribe.
This tutorial could also be considered a basic Drupal module development tutorial as we call hook_form_alter via a custom module. When viewing this tutorial, I assume you familiar with Simplenews and have the module already installed and Simplenews subscription block enabled.
In tutorial 2 for Simplenews Subscription Block Override, we replace the "save" button with an image button and introduce some jquery to our custom module so that the Simplenews subscription form fields is prepopulated with "Subscribe..." which disappears when a user clicks on the form field.

Comments
Thank you so much!!! What a
Thank you so much!!! What a clear and great tutorial!
It's very rare to see any simplenews tutorials so this was
refreshing! Again - thank you!!
Hey, thanks onearmfrog, I
Hey, thanks onearmfrog, I appreciate the feedback! I'm glad the tutorial helped.
pete
Great tutorial. Thank you so
Great tutorial. Thank you so much.
The quality is outstanding... plus, perfect release timing for me! How did you know... ;-)
I got a feeling about those
I got a feeling about those kinds of things krabbe. Thanks for the feedback, I really appreciate it! Good to hear about the quality too, I've been wondering what people have been thinking about them... I've started doing them in HD - of course, Blip changes that but still...
pete
i thank you so much,now i can
i thank you so much,now i can finish this task ;)
very cool tutorial, short & simple,exactly what i searched
best regards from austria
Also great release timing for
Also great release timing for me
just what I needed at that time and a nice shortcut into custom modules!
thanks,
Matthijs
Thanks Pete, nice tutorial.
Thanks Pete, nice tutorial. Like the straight to it approach.
Happy new year (nearly!).
Wow.....great stuff. Been
Wow.....great stuff. Been tired of searching around on simplenews tuts. Keep up the good work
Thanks Dan, I really
Thanks Dan, I really appreciate the positive feedback!
pete
Good work Peter ma' boy!
Good work Peter ma' boy! Thanks to your very clear and easy to follow video tut, I'm now able to make my newsletter block actually look nice.
I do have a question though - hope you don't mind, but when I use 'theme developer' and hover over the simplenews block I dont get:
simplenews-block.tpl.php
I get information like:
Theme hook called:
File used:
sites/all/themes/newagegiftshops/templates/region-sidebar.tpl.php
Candidate template files:
region-sidebar-first.tpl.php < region-sidebar.tpl.php < region.tpl.php
My site is themed with my own theme, but I don't seem to get useful information like 'simplenews-block.tpl.php like you do. I don't really understand what's going on - do you have any idea?
Many thanks
Sci-Fi Si
Hey Sci-Fi Si, Thanks for
Hey Sci-Fi Si,
Thanks for the feedback! As for your question about the theme developer, I'm not 100% sure but it sounds like you may not be hovering over the Simplenews block itself but picking up the sidebar... However, if you can take a screenshot, email it over to me at peter@torontowebsitedeveloper.com so I can get a better idea of what's happening.
Pete
Great tutorial...i was trying
Great tutorial...i was trying to change the subscription for 2 days and now found this video. Congratulations for the extra mile of the second video by putting the image button and javascript onblur/onfocus
Thanks for the tutorial. I'm
Thanks for the tutorial. I'm curious as to how you would add a custom id to the simplenews block based on whether or not the user is subscribed. Thanks for any insight!
Hi Eric, I think you can use
Hi Eric,
I think you can use a theme override for simple news to do that and add an id. Alternatively, depending on the theme you are using (i'm using a zen subtheme), you may have a body class "logged-in" added if your users are logged in. You could just use .logged-in #block-simplenews-1 to identify the block if so...
Pete
Hi Eric, The folowing did not
Hi Eric,
The folowing did not work for me:
$form['submit']['value'] = 'Subscribe';
Ik had to change this to:
$form['submit'] = array('#type' => 'submit', '#value' => t('Subscribe'));
Ik hope this is usefull.
greetings Wim
Pretty serious issue with
Pretty serious issue with this... the authenticated user can never unsubscribe... the buttons always says "Subscribe".
Thanks for bringing that up.
Thanks for bringing that up. You're right that it does however, the authenticated user can still unsubscribe through, my account -> manage my subscriptions and when they receive a newsletter, simplenews includes an unsubscribe link -- an antispam best practice.
Pete
Thanks very much, really
Thanks very much, really helpful.
How would I go about changing the text in the form? It takes the site's tagline, but I can't see where to override it in the form.
Hi Tez, sorry, I may be
Hi Tez,
sorry, I may be missing something, but what do you mean change the text in the form? It was a while ago I did this tutorial...
Pete
Wow!! Very helpful tutorial..
Wow!! Very helpful tutorial.. This vide tutorial beats everything I've seen before concerning this topic! Thank you very much!
Thanks. Your videos helped me
Thanks. Your videos helped me understand the fundamental concept of the module development, something the book 'Pro Drupal Development' was missing.
Hey Thupten, Thanks! That's
Hey Thupten,
Thanks! That's a huge complement, I probably refer back to Pro Drupal Development 2 or 3 times a week. Glad the tutorial has helped. Another great book on module develompent is 'Drupal Module Development', also published by Apress.
-Pete
Hey Pete, Thanks for a great
Hey Pete,
Thanks for a great tutorial. it really taught me quite a bit and feel like a just beat a plateau I've been stuck on for quite a while. The Drupal community needs more people like you, Thank you!
A quick question, if I only want to display the block if someone is not subscribed, is that possible? Or even just a message in the block like "Thank you for subscribing" and maybe a to unsubscribe click here link?
I understand you are probably a super busy guy but if you get a chance to respond I really appreciate it, thanks again.
Ivan
Hey Ivan, Thanks for the
Hey Ivan,
Thanks for the kind words, I'm glad I could help out. With regards to your question about displaying the block only for someone not subscribed or changing the message, it is possible, with a couple things to consider:
1. You'd only be able to do it for registered users - if an anonymous user subscribes, there is no way for the simplenews module to know when that user has returned (unless you do some custom work with the session or cookies but even that is temporary).
2. For registered users, you could use some php to define when the block is visible and query the database to see if the users uid is in the subscriptions table and if so, hide the block. it would look like:
global $user;$result = db_fetch_object(db_query("SELECT COUNT (*) FROM {simplenews_subscriptions} WHERE uid = %d", $user->uid));
if ($result) {
return false;
} else {
return true;
}
3. In terms of adding a different message if the user is subscribed, that's possible, you'd use the same hook_form_alter as in the tutorial but would have to query the db (like the code above) to see if the user is already subscribed. you would then use an if statement to provide the different messages for each scenario.
Hope that helps,
pete
Hey Pete, Thanks for the
Hey Pete,
Thanks for the speedy response time! So, unfortunately my newsletter will have to be for non-registered users as well, so I will take your response as use it as reference and knowledge for future projects. Thanks so much again it is very useful.
As an alternative, what about the way you have your form, so that it remains in the same state pre/post subscription?
That is the default if you
That is the default if you aren't a registered user on the site... whenever I log in (as I'm registered for the newsletter), the input field is replaced with my username...
Pete
I LIKE it ! THX
I LIKE it ! THX
Very clear and interesting
Very clear and interesting
Actually the tutorial is
Actually the tutorial is great. But the newsletter subscription form does not appear on the page of admin and also dsm($form_id) does not execute in my snoverride.module (does not show the form id). I am using newsletter 6.x 1.3. Anything wrong? Have done just the same way as explained in this tutorial. thanks
Thank you for the tutorial.
Thank you for the tutorial. It was very useful. If I wanted to change the submit button for authenticated users (based on their subscription status), how would I use the form callback to update which button to use?
This is definitely a great
This is definitely a great hands-on show. Thanks for that. Being encouraged by what you say, may I suggest that you make part 3 of the series showing how to tweak Simplenews subscription block into an ajax-powered one, so that any error messages (such as invalid mail) are displayed within the block, rather than in the content region.
Post new comment