Skip to content
Snippets Groups Projects
Commit 3971a77c authored by Bob Potter's avatar Bob Potter
Browse files

Issue #2954582 by ramonvasconcelos, devender, Neslee Canil Pinto, netsliver: Send test mail

parent a7af727c
No related branches found
Tags 7.x-5.7
1 merge request!25Issue #2951089 by thomscode, Guietc: WSOD - Proxy issue
......@@ -15,6 +15,12 @@ entity.mailchimp_campaign.send:
base_route: entity.mailchimp_campaign.view
weight: 1
entity.mailchimp_campaign.sendtestmail:
title: 'Send Test'
route_name: entity.mailchimp_campaign.sendtestmail
base_route: entity.mailchimp_campaign.view
weight: 1
entity.mailchimp_campaign.edit_form:
title: 'Edit'
route_name: entity.mailchimp_campaign.edit_form
......
......@@ -226,6 +226,38 @@ function mailchimp_campaign_send_campaign(MailchimpCampaign $campaign) {
return FALSE;
}
/**
* Sends a test mail Mailchimp campaign.
*
* @param \Drupal\mailchimp_campaign\Entity\MailchimpCampaign $campaign
* The Mailchimp campaign to send.
*
* @return bool
* TRUE if campaign is sent successfully.
*/
function mailchimp_campaign_sendtestmail_campaign(MailchimpCampaign $campaign, $testemail) {
/* @var \Mailchimp\MailchimpCampaigns $mc_campaign */
$mc_campaign = mailchimp_get_api_object('MailchimpCampaigns');
// Send campaign.
try {
if (!$mc_campaign) {
throw new Exception('Cannot send campaign without Mailchimp API. Check API key has been entered.');
}
$mc_campaign->sendTest($campaign->mc_data->id, explode(',', $testemail), 'html');
return TRUE;
}
catch (\Exception $e) {
\Drupal::messenger()->addError($e->getMessage());
\Drupal::logger('mailchimp_campaign')
->error('An error occurred while sending to this campaign: {message}', [
'message' => $e->getMessage(),
]);
}
return FALSE;
}
/**
* Delete a Mailchimp campaign and the local entity.
*/
......
......@@ -32,6 +32,15 @@ entity.mailchimp_campaign.send:
_permission: 'administer mailchimp campaigns'
_entity_access: 'mailchimp_campaign.send'
entity.mailchimp_campaign.sendtestmail:
path: 'admin/config/services/mailchimp/campaigns/{mailchimp_campaign}/sendtestmail'
defaults:
_entity_form: 'mailchimp_campaign.sendtestmail'
_title: 'Send Test Email Mailchimp Campaign'
requirements:
_permission: 'administer mailchimp campaigns'
_entity_access: 'mailchimp_campaign.sendtestmail'
entity.mailchimp_campaign.stats:
path: 'admin/config/services/mailchimp/campaigns/{mailchimp_campaign}/stats'
defaults:
......
......@@ -151,9 +151,11 @@ class MailchimpCampaignController extends ControllerBase {
$list_url = Url::fromUri('https://admin.mailchimp.com/lists/dashboard/overview?id=' . $campaign->list->id, ['attributes' => ['target' => '_blank']]);
$send_url = Url::fromRoute('entity.mailchimp_campaign.send', ['mailchimp_campaign' => $campaign_id]);
$edit_url = Url::fromRoute('entity.mailchimp_campaign.edit_form', ['mailchimp_campaign' => $campaign_id]);
$send_test_mail = Url::fromRoute('entity.mailchimp_campaign.sendtestmail', ['mailchimp_campaign' => $campaign_id]);
if ($campaign->mc_data->status === "save") {
$send_link = Link::fromTextAndUrl($this->t("Send"), $send_url)->toString();
$send_test_link = Link::fromTextAndUrl(t("Send Test Email"), $send_test_mail)->toString();
$edit_link = Link::fromTextAndUrl($this->t("Edit"), $edit_url)->toString();
}
// "Sent" campaigns were not being cached, so we needed to reload to get
......@@ -173,6 +175,7 @@ class MailchimpCampaignController extends ControllerBase {
Link::fromTextAndUrl(('View Archive'), $archive_url)->toString(),
Link::fromTextAndUrl(('View'), $campaign_url)->toString(),
$send_link,
$send_test_link,
];
if (isset($edit_link)) {
......
......@@ -22,6 +22,7 @@ use Drupal\mailchimp_campaign\MailchimpCampaignInterface;
* "view_builder" = "Drupal\mailchimp_campaign\Entity\MailchimpCampaignViewBuilder",
* "form" = {
* "send" = "Drupal\mailchimp_campaign\Form\MailchimpCampaignSendForm",
* "sendtestmail" = "Drupal\mailchimp_campaign\Form\MailchimpCampaignSendTestMailForm",
* "add" = "Drupal\mailchimp_campaign\Form\MailchimpCampaignForm",
* "edit" = "Drupal\mailchimp_campaign\Form\MailchimpCampaignForm",
* "delete" = "Drupal\mailchimp_campaign\Form\MailchimpCampaignDeleteForm"
......
<?php
namespace Drupal\mailchimp_campaign\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form controller for the MailchimpCampaign send campaign test email form.
*
* @ingroup mailchimp_campaign
*/
class MailchimpCampaignSendTestMailForm extends EntityConfirmFormBase {
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a MailchimpCampaignSendForm object.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('messenger')
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$system_site_config = \Drupal::config('system.site');
$site_email = $system_site_config->get('mail');
$form['testmail'] = [
'#type' => 'email',
'#default_value' => $site_email,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to send a test email for %name?',
['%name' => $this->entity->label()]);
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('This action will send a test email for the campaign through Mailchimp.');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('mailchimp_campaign.overview');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Send campaign test email');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($testemail = $form_state->getValue('testmail')) {
if (mailchimp_campaign_sendtestmail_campaign($this->entity, $testemail)) {
$this->messenger->addStatus($this->t('Mailchimp Campaign %name has been sent a test to %email.',
[
'%name' => $this->entity->label(),
'%email' => $testemail,
]));
}
}
$form_state->setRedirectUrl($this->getCancelUrl());
}
/**
* {@inheritdoc}
*
* // @todo Make sure override afterBuild is the correct solution.
*
* Have to disable EntityForm::afterbuild for this form.
* Drupal was attempting to get a field definition for the submit button
* from the MailchimpCampaign entity, which doesn't (and shouldn't) have it.
*/
public function afterBuild(array $element, FormStateInterface $form_state) {
return $element;
}
}
......@@ -28,6 +28,7 @@ class MailchimpCampaignAccessControlHandler extends EntityAccessControlHandler {
$return = NULL;
switch ($operation) {
case 'send':
case 'sendtestmail':
case 'edit':
case 'delete':
$return = ($status == MAILCHIMP_STATUS_SENT) ? AccessResult::forbidden() : AccessResult::allowed();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment