Hi! What I've done is copy the html source code into a custom block as follows:

><form accept-charset="UTF-8" action="/" id="simplenews-subscriptions-page-form" id="simplenews-block-form-32" method="post">
<div><div class="form-type-textfield form-item-realname form-item form-group">
  <label for="edit-realname">Name <span class="form-required" title="This field is required.">*</span></label>
 <input type="text" id="edit-realname" name="realname" value="" size="20" maxlength="255" class="form-text required" />
</div>
<button class="btn btn-default form-submit" id="edit-submit" name="op" value="Subscribe" type="submit">Subscribe</button>
<input type="hidden" name="form_build_id" value="form-d1sWPN54e_cfYPe1hXkOpQQsH2ibEuW7bfTUTdie3S4" />
<input type="hidden" name="form_token" value="Vht2yfEoSBhRS7fnw08bBED_z6jvc0v-xZ5b3u4tJUM" />
<input type="hidden" name="form_id" value="simplenews_block_form_32" />
</div></form>  

which works perfectly. But what I would like is that on submit the person gets redirected to a specific thank you page. When I change action="/" to example

action="/thank-you"

the user does not get added to the list.

Would most most appreciate any suggestions or guidance :)

Comments

Liliplanet’s picture

Title: Custom subscribe block with redirect » Custom simplenews subscribe block with redirect
Liliplanet’s picture

Issue summary: View changes
Liliplanet’s picture

Sorted with Rules! If user has subscribed, select data comparison, selected data is tid equals your newsletter taxonomy, add action.

Would have been easier if it was possible in the form, but super happy.

Proteo’s picture

A bit late, but I just had to do something very similar and wanted to share my solution. I've placed a Simplenews block in the footer (globally), and by default the module will reload the same page after the form has been submitted. This has to drawbacks:

a) All the feedback the user will see after submitting the form is a message at the top of the page (or wherever you placed messages in your template), so it may pass unnoticed.

b) We use Varnish for page caching, so these messages simply won't appear when the page is already cached (which is true 90% of the times). The registration itself works fine, but the user just won't have any feedback, so many users will submit the form repeatedly and will receive multiple confirmation messages.

So I wanted to:

a) Be able to craft a page specifically to inform users about the result of the submission –if it was valid, they would see a page, and a different one if not–.

b) Redirect all form submissions to a custom page with a fixed URL that can be excluded from the cache.

This is what I did:

1. Create two pages, one for successful submissions and other for the failed ones. How to do that and what to put in them, is up to you. You can create two nodes, or create to Panels pages, or whatever works for you. Just make sure you have two working URLs.

2. Create a custom module and put these functions in it. Don't forget to change the relevant bits accordingly:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, $form_state, $form_id) {
	switch ($form_id) {
		// This will match all simplenews blocks. If you want to restric to a specific one,
		// change this line to: case 'simplenews_block_form_xx' :
		case strstr($form_id, 'simplenews_block_form_') :
			$form['#action'] = '/success_page_url'; // override action URL
			$form['#validate'] = array('_mymodule_simplenews_validate'); // override validate handler
			$form['#submit'] = array('_mymodule_simplenews_submit'); // override submit handler
			break;
	}
}

/**
 * Validate Simplenews block forms.
 */
function _mymodule_simplenews_validate($form, &$form_state) {
	// If the validation fails, insted of setting the form as unvalid we'll set a flag so the form
	// can reach the submit handler
	$form_state['#is_valid']= FALSE;
	if (valid_email_address($form_state['values']['mail'])){
		$form_state['#is_valid']= TRUE;
	}
}

/**
 * Simplenews block form submit handler. It evaluates the flag set in _mymodule_simplenews_validate()
 * and redirects the user if necessary.
 */
function _mymodule_simplenews_submit($form, &$form_state) {
	if ($form_state['#is_valid']) {
		// Validation successful, fallback to the default submit handler
		simplenews_block_form_submit($form, $form_state);
	} else {
		// Validation failed, redirect user to the error page
		drupal_set_message(t("The e-mail address you supplied is not valid."));
		$form_state['redirect']= '/error_page_url';
	}
}

vrwired’s picture

I had to bring some code more up to date but the above was useful in figuring it out. Here's another basic example:

<?php
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;

function MYMODULE_form_alter(&$form, $form_state, $form_id) {

  switch ($form_id) {
    // Redirect to blog view.
    case strstr($form_id, 'simplenews_subscriptions_block_') :

      foreach (array_keys($form['actions']) as $action) {
        if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
          $form['actions'][$action]['#submit'][] = 'MYMODULE_form_submit';
        }
      }
  }
}

function MYMODULE_form_submit($form, FormStateInterface $form_state) {

  $response = Url::fromUserInput('/blog');
  $form_state->setRedirectUrl($response);

  //\Drupal::logger(' MYMODULE')->notice('MYMODULE submission has occurred ') ;
  simplenews_user_profile_form_submit($form, $form_state);
}