0

I use the Simplenews module for newsletters of my site. But I don't send out mails, rather use the module for subscribing and unsubscribing. Now for the anonymous user it gets difficult to unsubscribe since the user doesn't get any mail other than the subscribe mail. I read the comment in https://drupal.org/node/337628 #6, but I don't think I have an option here.

So I want to add an unsubscribe button to my block. How can I do that?

Drupal Version - 7.22 Simplenews Version - 7.x-1.0

1

I needed the same thing, so I thought I'd share my solution.

In hook_form_alter():

  // Check that this is a simplenews form and the user is anonymous
  // If so, add an Unsubscribe button with a custom submit handler before the
  // default simplenews form submit handler is called
  if (strpos($form_id, 'simplenews_block_form') !== FALSE && $user->uid == 0) {
    $form['unsubscribe'] = array(
      '#type' => 'submit',
      '#value' => t('Unsubscribe'),
    );

    $form['unsubscribe']['#submit'] = $form['#submit'];
    array_unshift($form['unsubscribe']['#submit'], 'custom_simplenews_form_unsubscribe');
  }

Then the custom submit handler will be something like this:

function custom_simplenews_form_unsubscribe(&$form, &$form_state) {
  // Change action to unsubscribe and exit so the simplenews handler will take over
  if ($form_state['clicked_button']['#value'] == 'Unsubscribe') {
    $form_state['values']['action'] = 'unsubscribe';
    $form_state['values']['submit'] = 'Unsubscribe';
  }
}

So, when control is passed to simplenews_block_form_submit() in simplenews.subscription.inc the user will be unsubscribed using the module's workflow.

  • in your solution, where go these codes? Which files? – user25838 Jan 29 '14 at 13:33
  • If you have a custom module, let's say you've named it my_custom_module, these will go into my_custom_module.module and the first piece of code would go into a function named my_custom_module_form_alter(&$form, &$form_state). – lnah Jan 29 '14 at 14:33
  • Small additions: The $user variable must be made available in hook_form_alter using global $user;. To make this work in localized environments if ($form_state['clicked_button']['#value'] == $form['unsubscribe']['#value']) must be used in the submit handler. – berliner Feb 14 '14 at 0:45
1

Create a block that augments the standard subscription block with a link to the unsubscribe user. The content of the block is just

<p><a href="/newsletter/subscriptions">Unsubscribe</a></p>

The block is filtered on unauthenticated users.

0

Here is the compete code with the modifications mentioned above, that works well for me. Just change the "mythemename" in the first line and put the code in the template.php (in your theme directory).

function mythemename_form_alter(&$form, &$form_state, $form_id) {
 global $user;
 if (strpos($form_id, 'simplenews_block_form') !== FALSE && $user->uid == 0) {
  $form['unsubscribe'] = array(
   '#type' => 'submit',
   '#value' => t('Unsubscribe'),
  );

  $form['unsubscribe']['#submit'] = $form['#submit'];
  array_unshift($form['unsubscribe']['#submit'], 'custom_simplenews_form_unsubscribe');
 }


}

function custom_simplenews_form_unsubscribe(&$form, &$form_state) {
    if ($form_state['clicked_button']['#value'] == $form['unsubscribe']['#value']) {
     $form_state['values']['action'] = 'unsubscribe';
     $form_state['values']['submit'] = 'Unsubscribe';
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.