3

is there a way to ajaxify simplenews subscribe to newsletter block? I was thinking of hook_form_alter ing it and adding my ajax callback, But its not working.

my_module_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id === 'simplenews_block_form_7182') {
    $ajax = array(
          'callback' => 'simplenews_block_form_validate',
          'wrapper' => 'simplenews_wrapper_block',
          'method' => 'replace',
          'effect' => 'fade',
          'progress' => array('message' => '')
        );
    $form['submit']['#executes_submit_callback'] = TRUE;
    $form['submit']['#ajax'] = $ajax;

but now when I trigger the submit button i get Fatal error: Call to undefined function simplenews_block_form_validate() in /includes/form.inc on line 1460

  • Callback needs to be a valid function. Not just any function, but one that's written to handle your AJAX data. Name your callback in a way that prevents mistaking it with native simplenews functions and post it's code here. – Mołot Jun 27 '13 at 13:17
  • I wanted to call simplenews callback as I dont want to alter anything to the form. I just dont want that page refresh after submitting. Is it possible? – saadlulu Jun 27 '13 at 13:21
  • Simplenews does not provide submitting AJAX callback (or I don't know about one). You need to write your own if you want it. Possible, for sure. But you can't just take validation or submit handler and tell it to be an AJAX callback. – Mołot Jun 27 '13 at 13:25
  • although it would be cool if one can just simply do that, Dont you agree? – saadlulu Jun 27 '13 at 13:35
3

Hope this will help you,

In Drupal 7, you can achieve this using form_load_include OR require_once

Try form_load_include() OR require_once() in your form alter.

function test_form_alter(&$form, &$form_state, $form_id) {
 switch ($form_id) {
    case 'simplenews_block_form_ID'://add your form_id
        require_once(DRUPAL_ROOT . "/modules/contrib/simplenews/includes/simplenews.subscription"); 
        //OR
        form_load_include($form_state, 'inc', 'simplenews', 'includes/simplenews.subscription');
    break;
   } 
}

Reference

| improve this answer | |
  • Works well and what.. Its the Drupal standard. – Jeet Jul 24 '13 at 6:52
  • Great!! enjoy.. – inizio Jul 24 '13 at 6:53

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.