Alternative notifications for Entityforms in Drupal 7

Entityform already offers a way to send notifications: by integrating with Rules module you can configure your notifications for administrators or other recipients, some of those may be inputs in the form itself, so this is perfect for most of cases, ah and if you need to migrate the form from one environment to another (let’s say from development to production), you can create a feature containing the form and that way you won’t have to redo the work manually

But what if you are a control freak or simply want to do more, like including some data belonging to an existing entity, get your recipients from related entities or by other means of association, etc. Well good news, you can complement the functionality using hooks and taking advantage of the features module to be able to take your code to any other environment

So, let’s begin by creating an Entityform and exporting it using a feature, let’s say that our form will be used to register events and that the form was created using the Entityform interface, here we just have 4 dummy fields

Then, we can create the feature

 

The feature created includes a main .module file and a bunch of other files depending on the type of resources used, e.g. taxonomy, fields, content types, etc., also, if we move the feature to another Drupal instance, we need to make sure that the required modules are installed there

One of the greatest things Drupal allows is to customize the feature by adding code in the .module file, even if we change the feature and eventually have to recreate it in the future, the changes we define in the .module file are kept, so going back to our original situation, let’s say that we want to:

  • Have a simple interface to define the email address of a person to notify
  • Make the form send a message with every submission data to that person

To achieve the first goal, we will use hook_menu and system_settings_form to create a basic page where we can add the recipient of the message and save it to Drupal’s variables table

/**
 * Implements hook_menu
 */ 
function my_module_menu() {   

  $items['admin/config/dummy-event-registrtion-form'] = array(  
    'title' => 'Event Registration form Settings',
    'description' => 'Allows to define a recipient for notifications',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_module_admin_form'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}
/**
 * Menu callback: xPath queries.
 */
function my_module_admin_form() {
  $form = array();

  $form['my_module_notification_email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email address for notifications'),
    '#default_value' => variable_get('my_module_notification_email', '[email protected]'),
  );

  return system_settings_form($form);
}

For the second goal, we need to add code to alter the Entityform (hook_form_alter) in order to add a callback for its submit event, also, we will need to implement hook_mail and of course we need to implement the callback code, so we will have something like this:

/**
* Implements hook_form_alter().
*/
function my_module_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == "feature_entityform_edit_form") {
    $form['actions']['submit']['#submit'][] = '_my_module_send_email_notification';
  }
}

/**
* Implements hook_mail().
*/
function my_module_mail($key, &$message, $params) {
  if (strpos($key, 'my_module_') !== FALSE) {
    $message['subject'] = $params['subject'];
    $message['body'][] = $params['message'];
  }
}

function _my_module_send_email_notification($form, &$form_state) {

  $form_state['redirect'] = $_GET['q'];
  $email_to = variable_get('my_module_notification_email', '[email protected]');

  if ($email_to) {
    $body = 'Add your own message';
    $email_from = variable_get('site_mail', '');
    $key = 'my_module_' . time();
    $language = language_default();

    $params = array(
      'subject' => 'New Event registration',
      'message' => $body,
      'langcode' => LANGUAGE_NONE,
    );

    drupal_mail('my_module', $key, $email_to, $language, $params, $email_from);
  }
}

You may need to adjust the code according to your needs


Do you need some help?

Let's get started. Tell us a little about yourself and your organization
and we can start a conversation about your needs and our capabilities.
Tagged: ,

Related Posts