Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By simonDRPL2183 on
We had someone create some custom modules for us (no longer around) and we're trying to understand them.
For example the module below sends and email when it's a users birthday - we want to be able to edit the actual email that's sent but can't find it anywhere! Can you give us an idea of where we should look?
Thanks!
<?php
/**
* ODL Birthday Notifications
* =================================
*
* Send out an e-mail/pm/sms on a users birthday
* Uses the notification_log to log that a notification has been sent for this year
*/
/**
* Implements hook_notifications().
*/
function odl_notifications_birthday_notifications() {
return array(
'user_birthday' => array(
'title' => 'Birthday Message',
'trigger_type' => NOTIFICATION_TRIGGER_SOURCE_CRON,
'trigger_cron' => array('callback' => 'odl_notifications_birthday_run'),
'handler_config' => array(
'allowed_handlers' => array('odl_mail', 'textanywhere', 'privatemsg'),
),
),
);
}
/**
* Run the cron job that sends notifications for a user's birthday.
* @return array
*/
function odl_notifications_birthday_run() {
$day = (int) date('w');
$current_hour = (int) date('H');
// TODO: Base this on the user's timezone information.
if ($day == 0 || $day == 6) {
// Send between 1pm and 6pm on weekends
if (!($current_hour > 13 && $current_hour < 18)) return;
} else {
// Send between 10am and 6pm on weekdays
if (!($current_hour > 10 && $current_hour < 18)) return;
}
// Find all user's that have a birthday TODAY (if we have missed the notification then don't send one!)
$accounts = _odl_notifications_birthday_fetch_users();
// Filter out those that have had a notification sent today
odl_notifications_birthday_filter_sent_notifications($accounts);
// Return a context for the notifications containing the recipient and node
$notification_contexts = array();
foreach ($accounts as $account) {
$notification_context = array(
'recipient' => $account,
);
$notification_contexts[] = $notification_context;
}
return $notification_contexts;
}
/**
* Look at the notification log and filter out any notifications that have already been
* sent today.
* @param $accounts
*/
function odl_notifications_birthday_filter_sent_notifications(&$accounts) {
$uids = array();
$sent_uids = array();
foreach ($accounts as $account) {
$uids[] = $account->uid;
}
$result = db_query("SELECT recipient_uid FROM {notification_log} date_of_birth
WHERE notification_identifier='user_birthday'
AND FROM_UNIXTIME(date_sent, '%d-%m') = :current_date", array(':current_date' => date('d-m')));
foreach ($result as $record) {
$sent_uids[] = $record->recipient_uid;
}
foreach ($accounts as $index => $account) {
if (array_search($account->uid, $sent_uids) !== FALSE) {
unset($accounts[$index]);
}
}
}
/**
* Return a list of users that have a birthday set for today.
* @return array
*/
function _odl_notifications_birthday_fetch_users() {
$result = db_query("SELECT entity_id FROM {field_revision_field_date_of_birth} date_of_birth
WHERE entity_type='user'
AND DATE_FORMAT(field_date_of_birth_value, '%d-%m') = :current_date", array(':current_date' => date('d-m')));
$uids = array();
foreach ($result as $record) {
$uids[] = $record->entity_id;
}
return user_load_multiple($uids);
}
/**
* Implementation of hook_form_odl_notifications_edit_form_alter().
*/
function odl_notifications_birthday_form_odl_notifications_edit_form_alter(&$form, &$form_state) {
$identifier = $form_state['storage']['identifier'];
// Only add fields to the event reminder form
if ($identifier != "user_birthday") {
return;
}
$form['#submit'][] = 'odl_notifications_birthday_notifications_edit_form_submit';
$form['test'] = array(
'#type' => 'fieldset',
'#title' => 'Test',
);
$form['test'][] = array(
'#type' => 'submit',
'#value' => 'TEST',
);
}
/**
* Submit handler to save the event reminder frequency settings
* @param $form
* @param $form_state
*/
function odl_notifications_birthday_notifications_edit_form_submit($form, $form_state) {
if ($form_state['clicked_button']['#value'] == 'TEST') {
/*db_insert('reminders')->fields(array(
'type' => 'payday_reminder',
'uid' => 1,
'created' => time(),
'reminder_date' => time() - 300
))->execute();*/
odl_notifications_cron();
}
}
Comments
It looks like your site is
It looks like your site is using the Notifications module. Thus it is most likely that the email is actually managed by this module which means it's probably stored in the database. See https://www.drupal.org/project/notifications
Go to your Configuration screen, locate Notifications and have a look. Good luck.
I agree
I agree with dotmundo. It appears the developer used the 'notifications' module. The custom email template it uses should be available in the configuration options.
Thanks both. Yes, we do seem
Thanks both. Yes, we do seem to have the notifications module installed but it looks like a custom/modified version. There's nothing in config/admin :( I've looked through the database too and can't see the email body text in there either.
This is killing me!
So I found this below but
So I found this below but STILL can't see where the actual email body comes from!
Without seeing all of the
Without seeing all of the code for the module, it's going to be hard to give you an exact answer. In looking at the code you provided, the module creates two menu items using hook_menu:
admin/config/odl-notification
and
admin/config/odl-notification/%/edit
The first one is a list view of all the site-wide notifications, and the second one is the edit page for those individual notifications (the % is accepting an argument, most likely the notification id). You should start by viewing the listing page first to see if you can edit them individually from there, to see if editing the message body is possible.
If you want to just disable the notifications, you can do that by commenting out the three calls to $notification->send_notifications($context); in the code you provided.
Also, the body of the message being sent is stored in the $mail['body'] value, which is stored in the $context array. If I believe correctly, the $mail['body'] variable is used with drupals drupal_mail() system, so you could also start by looking at the default mail template for the site to see if the language is in there. If it's not, start looking at any included (usually denoted as .inc file extension) files that came with the module to see if the body is dynamically generated somewhere.
Again, without having all of the code for the module, it's difficult to solve the problem or you so all I can do is offer suggestions to help you find the solution yourself.
Thanks justinfurnas. Where
Thanks justinfurnas. Where might I find the default mail template for the site?
That's a good question. Are
That's a good question. Are you using Drupal's default mail system, or is another one installed?
'Configuration>System>Mail System' will give you the information you need. By default it should say 'DefaultMailSystem', but if you have another mail system installed (such as MimeMail or HTMLMail), then it may be changed. I haven't used the notifications module extensively enough to know if it uses it's own extension of the mail system. If it's still set to 'DefaultSystemMail', then it's probably generating the body of the email somewhere in the module, and not in the actual template for the email. For that, you'll need to dig further into the modules code to find out where it's generating the body.
Do you have the devel module installed? If not, install it. If so, run a dpm($mail['body']); before the sendnotifications functions are called and see if that array actually contains the text you are seeing in the email. From there you can trace back to where it's being set at.
It will give you a very nice krumo style collapsible tree of the array/object structure of the variable you pass to it. Very useful for debugging.
Looking at the code isn't
Looking at the code isn't going to help especially if you are new to Drupal. There is a configuration screen, it just may not be upfront and might be a sub-option. Looking at the Notifications module, it does have a UI screen for configuration and it looks like its part of the Messaging configuration screen. The .info file also shows Messaging is a dependency. Therefore in your Configuration screen look for the Messaging option and from there you might see an option for Notifications.
Thanks both for your help. I
Thanks both for your help. I'll keep trying!