0

I'm testing an external email collection service that appears periodically at certain time intervals on a site.

When a user completes the form, the application can pass the email address as a variable in a POST request to a specific url.

What hook should I use to catch and process the email address at this one specific url?

I thought of using hook_init() something like the following, but I'm not sure if this is a good way to do something like this?

/**
 * Implements hook_init()
 * Check for variable sent via POST and process if found
 */
function mymodule_init() {
  if (drupal_get_path_alias() == "myurl") {
    if (isset($_POST['email_variable'])) {
      <custom processing>;
    }
  }
}

Notes:

  1. The url is a normal node page that already exists on the site.
  2. The form is externally hosted, so a form alter is not an option.
2
  • Form is in iframe ??
    – user33576
    Commented Sep 22, 2014 at 17:53
  • I don't think so. It's the scrollbox widget you see here: sumome.com I think it injects the DIV that contains the form somehow in the body of the page.
    – Dave Bruns
    Commented Sep 22, 2014 at 18:11

2 Answers 2

0

Use mytheme_preprocess_node(&$vars, $hook) in your active theme's template.php file.

function mytheme_preprocess_node(&$vars, $hook) {
  if($vars['nid'] =='node id of that node page'){

 //Add your code to get the data from post using post key
   $data = $_POST['datakey'];// datakey will contain data with which data is posted to this form
 }
}

Please, don't forget to clear drupal cache.

Hope it will help!

2
  • Thanks for this suggestion. Is it possible to implement this approach in a module instead? (I'd rather keep the code in one place). I updated my question to include a possible approach with hook_init, but I don't know is this is a good idea or not.
    – Dave Bruns
    Commented Sep 22, 2014 at 18:53
  • Yes preprocess function works in both template and module. I think it will work with init() also but usually we include files and any condition that we want to execute in begining
    – user33576
    Commented Sep 22, 2014 at 21:12
0

You should use the services module to create an endpoint and use the url where is sent as the path to endpoint.

You don't state which type format the data is being posted to your site but the services module can handle many different types.

Also it's not stated exactly what you want to do with the received data but the services module can be set up to process it in a variety of ways.

You can capture it to a databse table etc or Here's an example of creating a node using services: http://www.drupalwoo.com/content/blog/drupal-services-creating-node-from-external-form

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.