0

I have created a module that will respond on hook_node_insert() and will send the node information to the users subscribed to that node.

What we need to implement is a tpl file that will have the header, body, and footer of the email that will be sent to the users.

I cant call the template file after hook_node_insert() because I think that is not the main use of a template file in the first place.

I was thinking to something along these lines.

  • The node is inserted
  • I call the template file somehow
  • I format the mail body for that node
  • I return the html in some variable
  • I send the returned variable to the mail function

2 Answers 2

2

As you are using HOOK_node_insert() in your module then you can do anything in that function. As the previous answer, its better to use hook_theme(). So once you call the hook_node_insert() you can call a custom function and that will have the return type also that will send the email. Follow the below mentioned steps,

  1. Create a new custom function as an example,
    example_notify_subscribed($node_information)

    In this function you can send the node informations. So here comes the hook_theme part.
  2. In the hook_theme() mention a new theme.

function your_module_theme($existing, $type, $theme, $path) {
    if($type == 'module') {
        'mail_node_notify' => array(
      'variables' => array(
        'node_information' => NULL,
        'extra_vars' => NULL,
      ),
      'template' => 'notify-mail-subs',
     'path' => drupal_get_path('module', 'your_module').'/template',
      ),
     );
    }
}
  1. In your example_notify_subscribed function call this theme as following,


//Send the email
        $email_text_user = theme('mail_node_notify', array(
          'node_information' => $node, //Your node variable information
          'extra_vars' => $extra, //If you send any more information to the template file
          ),
        ));

        $email_values_user = array(
          'module' => 'your_module',
          'key' => 'mail_key', //Unique hook_mail key
          'to' => $send_to,
          'form' => variable_get('site_mail', '[email protected]'),
          'subject' => t('Your subject'),
          'body' => $email_text_user,
        );

        $sent_to_user = your_module_mail_send($email_values_user); //Custom Function to send email (HTML)

You may need to create some hook_mail_alter() and a new class to extend default DefaultMailSystem Class from Drupal. Because Drupal 7 by default sends text emails.

I hope this idea helps.

0
1

You can always use a template file in any function: Just define a theme function that uses a template file, and then call the theme function from the hook.

That is what happens with theme_book_navigation(), for example. The Book module defines the theme function in book_theme() (an implementation of hook_theme()) using the following code.

  array(
    'book_navigation' => array(
      'variables' => array('book_link' => NULL), 
      'template' => 'book-navigation',
    ),
  )

book_node_view() (an implementation of hook_node_view()) then uses the template file using the following code.

  if ($view_mode == 'full') {
    if (!empty($node->book['bid']) && empty($node->in_preview)) {
      $node->content['book_navigation'] = array(
        '#markup' => theme('book_navigation', array('book_link' => $node->book)), 
        '#weight' => 100,
      );
    }
  }

The content of $node->content['book_navigation']['#markup'] is the content of the template file.

What you do with the value returned from theme() is up to you:

  • You can save it in a Drupal variable
  • You can pass it to a function like drupal_mail()
  • You can store it in a node property you then save in a database table
  • You can print it (from a menu callback, for example)

Remember that a theme function using a template file can use preprocess, and process hooks, if you need them.

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.