contact_emails.module in Contact Emails 8
Contains contact_emails.module..
File
contact_emails.moduleView source
<?php
/**
* @file
* Contains contact_emails.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_help().
*/
function contact_emails_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the contact_emails module.
case 'help.page.contact_emails':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Module to handle contact emails') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_mail_alter().
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function contact_emails_mail_alter(&$message) {
// If an email from the contact module, disable sending.
$contact_form_ids = [
'contact_page_autoreply',
'contact_page_mail',
];
if (in_array($message['id'], $contact_form_ids)) {
/** @var \Drupal\contact\MessageInterface $contact_message */
if ($contact_message = $message['params']['contact_message']) {
$contact_form = $contact_message
->getContactForm();
$contact_form_id = $contact_form
->id();
/** @var \Drupal\contact_emails\ContactEmailStorageInterface $storage */
$storage = \Drupal::entityTypeManager()
->getStorage('contact_email');
if ($storage
->hasContactEmails($contact_form_id)) {
// Disable sending of default mails if we have at least one email
// set by contact_emails (regardless of whether or not it is a disabled
// email).
$message['send'] = FALSE;
}
}
else {
// If we don't have a contact_message object, something must have gone
// wrong, let's disable just in case.
$log = t('Unable to load the contact message when attempting to alter the default mailing from contact form for contact form id: @contact_form_id', [
'@contact_form_id' => $message['id'],
]);
\Drupal::logger('contact_emails')
->notice($log);
$message['send'] = FALSE;
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function contact_emails_form_contact_form_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['id']['#default_value'])) {
$contact_form_id = $form['id']['#default_value'];
/** @var \Drupal\contact_emails\ContactEmailStorageInterface $storage */
$storage = \Drupal::entityTypeManager()
->getStorage('contact_email');
// Disable default contact email if we have at least one contact_emails
// email added.
if ($storage
->hasContactEmails($contact_form_id)) {
if (isset($form['recipients'])) {
$form['recipients']['#type'] = 'hidden';
}
if (isset($form['reply'])) {
$form['reply']['#type'] = 'hidden';
}
$form['manage_emails'] = [
'#type' => 'link',
'#title' => t('Emails for this form are managed here.'),
'#url' => Url::fromRoute("entity.contact_email.collection", [
'contact_form' => $contact_form_id,
]),
'#weight' => 100,
];
}
else {
$form['manage_emails'] = [
'#type' => 'link',
'#title' => t('Set up multiple emails for this form.'),
'#url' => Url::fromRoute("entity.contact_email.collection", [
'contact_form' => $contact_form_id,
]),
'#weight' => 100,
];
}
}
}
/**
* Implements hook_entity_operation_alter().
*/
function contact_emails_entity_operation_alter(array &$operations, $entity) {
/** @var EntityInterface $entity */
if ($entity
->getEntityTypeId() == 'contact_form') {
if (\Drupal::service('router.route_provider')
->getRouteByName("entity.contact_email.collection")) {
if ($entity
->id() != 'personal') {
$operations['email_settings'] = [
'title' => t('Manage emails'),
'url' => Url::fromRoute("entity.contact_email.collection", [
'contact_form' => $entity
->id(),
]),
'weight' => 50,
];
}
}
}
}
/**
* Implements hook_mail().
*/
function contact_emails_mail($key, &$message, $params) {
switch ($key) {
case 'contact_emails':
$message['from'] = \Drupal::config('system.site')
->get('mail');
$message['subject'] = $params['subject'];
$message['params']['format'] = $params['format'];
$message['headers']['Content-Type'] = $params['format'];
$message['body'][] = $params['message'];
break;
}
}
/**
* Implements hook_entity_insert().
*/
function contact_emails_entity_insert(EntityInterface $entity) {
if ($entity
->getEntityTypeId() !== 'contact_message') {
return;
}
$emailer = \Drupal::service('contact_emails.emailer');
$emailer
->setContactMessage($entity);
$emailer
->sendEmails();
}
Functions
Name | Description |
---|---|
contact_emails_entity_insert | Implements hook_entity_insert(). |
contact_emails_entity_operation_alter | Implements hook_entity_operation_alter(). |
contact_emails_form_contact_form_edit_form_alter | Implements hook_form_FORM_ID_alter(). |
contact_emails_help | Implements hook_help(). |
contact_emails_mail | Implements hook_mail(). |
contact_emails_mail_alter | Implements hook_mail_alter(). |