1

Trying to learn how to override forms I've read the instructions on https://www.drupal.org/node/1975230 and see I need to identify the form itself. The form that I need to redirect is located on user/1/invites/invite_by_email, so looking at the HTML code of the form:

<form action="/user/1/invites/invite_by_email" method="post"
 id="invite-form" accept-charset="UTF-8">

So taking its id invite-form I found its function in the invite.admin.inc file of Invite module at line http://cgit.drupalcode.org/invite/tree/includes/invite.admin.inc#n177:

function invite_form($form, &$form_state, $invite) {
  $form['#entity'] = $form_state['invite'] = $invite;

  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $invite->uid,
  );

  field_attach_form('invite', $invite, $form, $form_state);

  $submit = array();
  if (!empty($form['#submit'])) {
    $submit += $form['#submit'];
  }

  $form['actions'] = array(
    '#weight' => 100,
  );

  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save invite'),
    '#submit' => $submit + array('invite_form_submit'),
  );

  // Show Delete button if we edit invite.
  $invite_id = entity_id('invite', $invite);
  if (!empty($invite_id) && invite_access('edit', $invite)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#submit' => array('invite_form_submit_delete'),
    );
  }

  $form['#validate'][] = 'invite_form_validate';

  return $form;
}

Then there is another function invite_by_email_form_invite_form_alter() at http://cgit.drupalcode.org/invite/tree/modules/invite_by_email/invite_by_email.module#n283:

function invite_by_email_form_invite_form_alter(&$form, &$form_state, $form_id) {
  $invite = $form['#entity'];

  if (!empty($invite->type_details()->invite_sending_controller['invite_by_email'])) {
    $language_subject = $form['field_invitation_email_subject']['#language'];
    $invite_by_email_settings = isset($invite->type_details()->data['invite_by_email']) ? $invite->type_details()->data['invite_by_email'] : array('use_default' => TRUE);

    if ($invite_by_email_settings['use_default']) {
      $message_editable = variable_get('invite_message_editable', FALSE);
      $replace_tokens = variable_get('invite_default_replace_tokens', TRUE);

      // Override the default mail subject and body with the global defaults.
      $form['field_invitation_email_subject'][$language_subject][0]['value']['#default_value'] = variable_get('invite_default_mail_subject', '');
      $form['field_invitation_email_body'][$language_subject][0]['value']['#default_value'] = variable_get('invite_default_mail_body', '');
    }
    else {
      $message_editable = $invite_by_email_settings['message_editable'];
      $replace_tokens = isset($invite_by_email_settings['replace_tokens']) ? $invite_by_email_settings['replace_tokens'] : TRUE;
    }

    // Hide if message is not editable.
    if (!$message_editable) {
      $form['field_invitation_email_subject']['#type'] = 'hidden';
      $form['field_invitation_email_body']['#type'] = 'hidden';
    }
    else {
      // Add token help if enabled.
      if (module_exists('token') && $replace_tokens) {
        // Add tokens help.
        $form['token_help'] = array(
          '#title' => t('Replacement patterns'),
          '#type' => 'fieldset',
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#weight' => $form['field_invitation_email_body']['#weight'] + 0.5,
        );

        $form['token_help']['help'] = array(
          '#markup' => theme('token_tree', array('token_types' => array('user', 'profile', 'invite'))),
        );
      }
    }

    $form['#validate'][] = 'invite_by_email_form_invite_form_validate';
    $form['actions']['submit']['#value'] = t('Send Invitation');
  }
}

and it looks like more the one I need to redirect.

However this function is already an alter function itself and I am confused how can I alter it. Can I alter another alter function? If not and if I need to work with the original invite_form function at http://cgit.drupalcode.org/invite/tree/includes/invite.admin.inc#n177, then do I need to copy invite_by_email_form_invite_form_alter() at whole as I don't want to change how it works - I only need to redirect it to another page (to user/1/invites/pending in fact) on form submit.

Bear with me if the question is too simple as I am just learning. I would appreciate if someone could give me a code to try.

1 Answer 1

0

You don't actually alter the alter, but you can create your own custom module that uses hook_form_alter plus a custom function to redirect upon form submittal.

Create a custom module. For this example, let's call it invite_redirect.

If you haven't created a custom module before, these instructions are fairly good but are missing a very important step:

https://www.drupal.org/docs/7/creating-custom-modules/getting-started

The missing step is creating the .info file which is required. Name yours invite_redirect.info. It goes inside the sites/all/modules/custom/invite_redirect along with invite_redirect.module. All that absolutely has to be in the invite_redirect.info file is the following...

name = Invite Redirect
description = Redirects to custom location upon submittal of the invite_by_email form
core = 7.x

In the invite_redirect.module file, add this code...

<?php
function invite_redirect_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'form_invite') {
    $form['#submit'][] = 'invite_form_submit_handler';
  }
}

function invite_form_submit_handler(&$form, &$form_state) {
  $form_state['redirect'] = 'user/1/invites/pending';
}

I would enable this and see if it works (flush caches after enabling it). If it doesn't work, you may have to specify a weight for your custom module (so it gets the last word durring rendering). There are several methods for doing this described here: https://www.drupal.org/docs/7/creating-custom-modules/howtos/how-to-update-a-modules-weight

I prefer the .install file method they discuss.

Hope this helps you.

Anne

6
  • Didn't test this as I've decided to offer a patch to the module's maintainers to address this issue drupal.org/node/2840914#comment-11851423 But based on your answer to my somehow similar question in the other thread I am pretty sure it works, therefore I am accepting your answer. Thank you very much!
    – Nick
    Commented Jan 4, 2017 at 8:29
  • Sorry Anne, but I really want to learn this. I've just used your code and it didn't work. Are you sure the check should be for 'form_invite_form_alter ' and not 'invite_by_email_form_invite_form_alter'?
    – Nick
    Commented Jan 4, 2017 at 8:40
  • Doh! You're right: the form id should be form_invite. I'm correcting my post in case someone else stumbles upon it. sorry about that. Commented Jan 4, 2017 at 18:15
  • Unfortunately, it always ends landing at individual invite page like domain.com/invite/73 and it does not redirect to user/1/invites/pending.
    – Nick
    Commented Jan 4, 2017 at 19:12
  • I suspect this is because of the relative weight of the modules attempting the hook_form_alter on this form. I would try one of the methods they discuss here: drupal.org/docs/7/creating-custom-modules/howtos/… Commented Jan 4, 2017 at 19:18

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.