0

I have a module that displays an existing signup form in a block.

function my_contact_block_view($delta = '') {
    switch ($delta) {

        // Newsletter signup block
        case 'newsletter_signup':
            $block = my_contact_get_emma_block();
            break;
    }
    return $block;
}

I make some minor alterations to the form. Some markup elements are removed from the form (this works), and I am attaching an #ajax property to the submit element (this doesn't seem to work):

function my_contact_get_emma_block(){
    $form = drupal_get_form('my_contact_emma_form');

    // get wrapping markup
    $header = variable_get('my_contact_emma_block_header', '');
    $footer = variable_get('my_contact_emma_block_footer', '');

    // unset some stuff
    unset($form['heading']);
    unset($form['copy']);
    unset($form['required_blurb']);
    unset($form['privacy_blurb']);

    // alter some stuff
    $form['fname']['#attributes']['placeholder'] = t('First');
    $form['lname']['#attributes']['placeholder'] = t('Last');
    $form['email']['#attributes']['placeholder'] = t('Email');

    // config to submit via ajax
    $form['submit']['#ajax'] = array(
    'callback' => 'my_contact_emma_form_ajax_submit',
    'wrapper' => 'brd-contact-emma-form',
    'method' => 'replace',
    'effect' => 'fade',
    );

    // render the form
    $form = drupal_render($form);
    $markup = $header . $form . $footer; 

    // return renderable array for block
    return array(
        'subject' => t('Event Email Sign-Up?'),
        'content' => array(
            '#type' => 'markup',
            '#markup' => $markup,
        ),
    );
}

Unfortunately, it never hits my ajax submit function. Instead the page submits to the normal submit routine (my_contact_emma_form_submit).

/**
 * Ajax callback function. (Never called)
 */
function my_contact_emma_form_ajax_submit($form, $form_state) {
  // Dummy/dumb validation for demo purpose.
  if (!empty($form_state['input']['email'])) {
    return 'Subscribed !';
  }
  else {
    return $form;
  }
}

What am I doing wrong?

0

3 Answers 3

1

drupal_get_form will return a builded form/ rendered form. i.e Your form has been already prepared based on the specification done in the form callback, like its finalized.

So making changes to the return values of drupal_get_form wont work.

If you want to alter any of the form then you should use any of the following hook

1
  • Ok, I kind of thought that might be the case. But why are other changes working (placeholders, markup elements, etc)?
    – doub1ejack
    Commented Jun 26, 2013 at 18:53
1

The real problem here is that my whole approach was wrong. I should not have been trying to bend the same form to serve multiple purposes.

If all I needed to do was minor alterations the display of the form (which were working above), I probably could have gotten away with it. But when I tried to do things like change the submission method things got sticky. And when I realized that I wanted to show the user a different result for ajax submissions (a simple 'Ok!' instead of a whole confirmation page), things really broke down.

What prevented me from doing what I want is that there is no way to detect the difference between Form A submitted from a block and Form A submitted from a page. Because I couldn't differentiate between them, I could not use hook_form_alter (or similar) to create 2 different use cases.

Ultimately, the solution was to basically duplicate the first form, make all the alterations in my original post in that form, and handle the ajax there as well.

1
  • 1
    You can detect the difference between Form A submitted from a block and Form A submitted from a page. You could call drupal_get_form with params. Like $param1 = 'is_page'; or $is_page = TRUE. Then type drupal_get_form('my_contact_emma_form', $param1, $param2);. And then in form you can use them and set in values and whatever. function my_contact_emma_form($form, &$form_state, $param1, $param2){...}
    – milkovsky
    Commented Jul 23, 2013 at 8:37
1

Try using button instead of submit.

$form['submit']['#type'] = 'button';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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