2

I have a content type Testimonials and like any other type, it gets displayed after you save it. Is it possible to theme that page into something that says Your Testimonial has been saved! instead of displaying the data? I only need this for this specific content type.

EDIT: @googletorp Am I doing this correctly? I placed this code in my custom module enchance.module (activated) but I'm still getting the same page: the full version of the published testimonial instead of being redirected to <front>.

function enchance_form_alter(&$form, &$form_state, $form_id) {
    //dpm($form);
    if ($form_id == 'edit-testimonials-node-form') {
        $form['#submit'][] = 'testimonials_redirect_success';
    }
}

function testimonials_redirect_success(&$form, &$form_state) {
  // Use the drupal_set_message solution.
  drupal_set_message(t('Your Testimonial has been saved!'));
  // Redirect to custom page solution
  $form_state['redirect'] = '<front>';
}

// I'm not being redirected to <front>

2 Answers 2

3

with hook_form_alter you could for the form for the node type add a submit handler that will redirect to a custom page. Alternatively you could use drupal_set_message.

function module_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'the form id for the node form') {
    $form['#submit'][] = 'my_custom_submit_handler';
  }
}

function my_custom_submit_handler(&$form, &$form_state) {
  // Use the drupal_set_message solution.
  drupal_set_message(t('Your Testimonial has been saved!'));
  // Redirect to custom page solution
  $form_state['redirect'] = 'url/to/page';
}
4
  • 1
    +1 Of course you can also combine those two methods, for instance, you can redirect to the front page and have your drupal_set_message show up there. Commented Jun 22, 2011 at 14:04
  • Wow, amazing! I'll try it out.
    – enchance
    Commented Jun 22, 2011 at 17:00
  • Stupid question but how do I use the code you just posted?
    – enchance
    Commented Jun 23, 2011 at 13:18
  • @enchance You put it in a custom module, rename the functions so that 'module' is replaced with the name of the module and enter the actual name of the form_id.
    – googletorp
    Commented Jun 23, 2011 at 18:27
0

The problem with your code is that you are using the wrong form ID. The form ID is normally the name of the function that build the form; it cannot contain characters that a function name cannot contain, such as hyphens.

With Drupal 7, you can implement hook_form_BASE_FORM_ID_alter(), and your code would become similar to the following one:

function enchance_form_node_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#node']->type == 'custom') { // Replace 'custom' with the content type ID.
    $form['#submit'][] = 'enhance_redirect_success';
  }
}

function enhance_redirect_success(&$form, &$form_state) {
  drupal_set_message(t('Your Testimonial has been saved!'));
  $form_state['redirect'] = '<front>';
}

Taking the assumption that the submission handler was implemented in enhance.module, I used enhance_redirect_success(), as the function of a module needs to be prefixed by the module name, with the exception of "private" functions that are prefixed by an underscore and the module name, and the theme functions that are prefixed by "theme_" and the module name.
If there is a module that implements testimonials_redirect_success(), and you want to use that function, then you replace "enhance_redirect_success" with "testimonials_redirect_success."

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.