function registration_send_broadcast in Entity Registration 8
Same name and namespace in other branches
- 8.2 registration.module \registration_send_broadcast()
- 7.2 registration.module \registration_send_broadcast()
- 7 registration.module \registration_send_broadcast()
Send an email to all registrations for a host entity.
Parameters
string $entity_type: The host entity type.
int $entity_id: The host entity ID.
string $subject: Subject of email.
string $message: Message body of email.
2 calls to registration_send_broadcast()
- registration_cron in ./
registration.module - Implements hook_cron().
- registration_registrations_broadcast_form_submit in includes/
registration.forms.inc - Submit handler for registration_registrations_broadcast_form.
File
- ./
registration.module, line 1000
Code
function registration_send_broadcast($entity_type, $entity_id, $subject, $message) {
$language = \Drupal::languageManager()
->getCurrentLanguage();
// Return early if there are no active registration states configured.
$active_states = registration_get_active_states();
if (empty($active_states)) {
$error_message = t('There are no active registration states configured. For email to be sent to registrants, an active registration state must be specified at "admin/structure/registration/registration_states".');
drupal_set_message($error_message, 'error');
\Drupal::logger('registration')
->notice($error_message, []);
return;
}
// grab registration entity settings
$settings = registration_entity_settings($entity_type, $entity_id);
$from = $settings['settings']['from_address'];
// grab all registrations
$query = new EntityFieldQuery();
$entities = $query
->entityCondition('entity_type', 'registration')
->propertyCondition('entity_id', $entity_id)
->propertyCondition('entity_type', $entity_type)
->propertyCondition('state', $active_states, 'IN')
->execute();
if (!empty($entities)) {
$recipients = array();
$message_template = $message;
$params = array(
'subject' => $subject,
'message' => $message,
);
// load registrations and build an array of recipients
$registrations = registration_load_multiple(array_keys($entities['registration']));
// Give other modules a chance to alter registrations (for example, did
// someone opt-out of the reminder?
// Invoke hook_registration_send_broadcast_alter(),
// hook_registration_send_broadcast_ENTITY_TYPE_alter() and
// hook_registration_send_broadcast_ENTITY_TYPE_ENTITY_ID_alter()
$hooks = array(
'registration_send_broadcast',
);
$hooks[] = 'registration_send_broadcast_' . $entity_type;
$hooks[] = 'registration_send_broadcast_' . $entity_type . '_' . $entity_id;
$context = array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
);
\Drupal::moduleHandler()
->alter($hooks, $registrations, $context);
// send the email to each registrant and communicate results
$success_count = 0;
foreach ($registrations as $registration) {
$wrapper = entity_metadata_wrapper('registration', $registration);
$recipients[] = $wrapper->mail
->value();
$entity = entity_load_single($entity_type, $entity_id);
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$message = token_replace($message_template, array(
$entity_type => $entity,
'registration' => $registration,
), array(
'clear' => TRUE,
));
}
$params['message'] = $message;
$result = drupal_mail('registration', 'broadcast', $wrapper->mail
->value(), $language, $params, $from);
if ($result['result']) {
$success_count++;
}
else {
\Drupal::logger('registration')
->error('Failed to send registration broadcast email to %email.', array(
'%email' => $wrapper->mail
->value(),
));
}
}
if ($success_count) {
drupal_set_message(t('Registration broadcast sent to @count registrants.', array(
'@count' => $success_count,
)));
\Drupal::logger('registration')
->notice('Registration broadcast sent to @count registrants.', array(
'@count' => $success_count,
));
}
}
else {
drupal_set_message(t('There are no participants registered for this %type.', array(
'%type' => $entity_type,
)), 'warning');
}
}