mailjet-8.x-2.7/mailjet.module
mailjet.module
<?php
/*
* @file
* code for Mailjet module
*/
use Drupal\commerce_store\StoreStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
use Mailjet\Resources;
/**
* Redirects to external links.
*/
function mailjetExternalLinkRedirect($url)
{
$moduleHandler = Drupal::service('module_handler');
if ($moduleHandler->moduleExists('overlay') && overlay_get_mode() == 'child') {
unset($_GET['destination']);
overlay_close_dialog($url, ['external' => TRUE]);
return FALSE;
}
$response = new \Symfony\Component\HttpFoundation\RedirectResponse($url);
$response->send();
}
/**
* Registration alter callback.
*/
function _mailjet_register_alter_callback()
{
$config_mailjet = Drupal::service('config.factory')->getEditable('mailjet.settings');
if (isset($_POST['data']['apikey']) && !empty($_POST['data']['apikey']) && isset($_POST['data']['secretkey']) && !empty($_POST['data']['secretkey'])) {
$config_mailjet->set('mailjet_active', TRUE);
$config_mailjet->set('mailjet_username', $_POST['data']['apikey']);
$config_mailjet->set('mailjet_password', $_POST['data']['secretkey']);
$config_mailjet->save();
$mailjetApiClient = \Drupal\mailjet\MailjetApi::getApiClient($_POST['data']['apikey'], $_POST['data']['secretkey']);
$params = [
'AllowedAccess' => 'campaigns,contacts,stats,pricing,account,reports',
'APIKeyALT' => $_POST['data']['apikey'],
'TokenType' => 'iframe',
'IsActive' => TRUE,
'SentData' => serialize(['plugin' => 'drupal-3.0']),
];
$response = \Drupal\mailjet\MailjetApi::createApiToken($params);
if (FALSE != $response) {
$config_mailjet->set('APItoken', $response[0]['Token']);
$config_mailjet->save();
mailjet_first_sync(mailjet_get_default_list_id(mailjet_new()));
} else {
Drupal::messenger()->addMessage(t('Token was not generated! Please try again.'), 'error');
}
}
if (strpos($_POST['data']['next_step_url'], 'welcome') !== FALSE) {
$res = [
"code" => 1, // necessary for Mailjet's system to proceed
"continue" => TRUE,
"exit_url" => 'admin/config/system/mailjet/api',
];
echo json_encode($res);
drupal_exit();
}
$res = [
"code" => 1, // necessary for Mailjet's system to proceed
"continue" => TRUE,
"continue_address" => $_POST['data']['next_step_url'],
];
echo json_encode($res);
drupal_exit();
}
/**
* Creates a new MailJet Object
*/
function mailjet_new()
{
$config_mailjet = Drupal::config('mailjet.settings');
$key = '';
$secret_key = '';
if (!empty($config_mailjet->get('mailjet_active')) && !empty($config_mailjet->get('mailjet_username'))
&& !empty($config_mailjet->get('mailjet_password'))) {
// include mailje.api.inc
$key = trim($config_mailjet->get('mailjet_username'));
$secret_key = trim($config_mailjet->get('mailjet_password'));
return \Drupal\mailjet\MailjetApi::getApiClient($key, $secret_key);
}
if (!($key) && !($secret_key)) {
Drupal::messenger()->addMessage(t('You need to add your MailJet API details before you can continue!'));
}
return FALSE;
}
/**
* Get the user infos.
* @return boolean
*/
function mailjet_user_infos()
{
$mailjetApiClient = mailjet_new();
$responseProfile = $mailjetApiClient->get(Resources::$Myprofile);
$responseUser = $mailjetApiClient->get(Resources::$User);
if ($responseProfile->success()) {
return array_merge($responseProfile->getData()[0], $responseUser->getData()[0]);
}
return FALSE;
}
/**
* Update the user infos.
* @param unknown $infos
* @return boolean
*/
function mailjet_mjuser_update($infos)
{
Drupal::logger('my_module')->notice(print_r($infos, TRUE));
$mailjetApiClient = mailjet_new();
$response = $mailjetApiClient->put(Resources::$Myprofile, ['body' => $infos]);
return ($response->success()) ? TRUE : FALSE;
}
/**
* User tracking check.
*/
function mailjet_user_trackingcheck()
{
$mailjetApiClient = mailjet_new();
$filters = [
'APIKey' => \Drupal\mailjet\MailjetApi::$mjApiKey,
];
$response = $mailjetApiClient->get(Resources::$Eventcallbackurl, ['filters' => $filters]);
if ($response->success()) {
return $response->getData();
}
return FALSE;
}
/**
* Mailjet traging update.
*/
function mailjet_user_trackingupdate($tracking, $current_events)
{
$mailjetApiClient = mailjet_new();
try {
$check = ["open", "click", "bounce", "spam", "blocked", "unsub"];
foreach ($tracking as $key => $value) {
if (in_array($key, $check)) {
if ($value == 1 && !array_key_exists($key, $current_events)) {
$body = [
'APIKeyALT' => \Drupal\mailjet\MailjetApi::$mjApiKey,
'EventType' => $key,
'Url' => $tracking['Url'] ?? '',
];
$new_response = $mailjetApiClient->post(Resources::$Eventcallbackurl, ['body' => $body]);
unset($body);
unset($new_response);
}
if ($value == 0 && array_key_exists($key, $current_events)) {
$del_response = $mailjetApiClient->delete(Resources::$Eventcallbackurl, ['id' => $current_events[$key]]);
unset($del_response);
}
}
}
return TRUE;
} catch (Exception $e) {
return FALSE;
}
}
/**
* Returns a list of trusted domains.
*/
function mailjet_user_domain_list()
{
$mailjetApiClient = mailjet_new();
$filters = [
'style' => 'full',
'limit' => 0,
];
$response = $mailjetApiClient->get(Resources::$Sender, ['filters' => $filters]);
if ($response->success()) {
$senders = $response->getData();
$domains = [];
foreach ($senders as $sender) {
if (!empty($sender['DNS']) and !array_key_exists($sender['DNS']['Domain'], $domains) && strpos($sender['Email']['Email'], "*@") !== FALSE) {
$domains[$sender['DNS']['Domain']] = $sender;
}
}
return $domains;
}
return [];
}
/**
* Gets trusted email domains.
*/
function mailjet_get_a_trusted_email()
{
$mailjetApiClient = mailjet_new();
$system_site_config = Drupal::config('system.site');
$filters = [
'style' => 'full',
];
$response = $mailjetApiClient->get(Resources::$Sender, ['filters' => $filters]);
if ($response->success()) {
$senders = $response->getData();
foreach ($senders as $emailObject) {
if ($emailObject['Status'] == 'Active') {
return $emailObject['Email']['Email'];
}
}
}
return $system_site_config->get('site_mail');
}
/**
* Add a trusted domain
* @param unknown $domain
* @return boolean
*/
function mailjet_user_domain_add($domain)
{
$mailjetApiClient = mailjet_new();
if (strpos($domain, '@') === FALSE) {
$domain = '*@' . $domain;
}
$body = [
'Email' => $domain,
];
$response = $mailjetApiClient->post(Resources::$Sender, ['body' => $body]);
if ($response->success()) {
return TRUE;
}
Drupal::messenger()->addMessage(t($response->getReasonPhrase()), 'error');
return FALSE;
}
/**
* Check a domain
* @param unknown $domain
* @return boolean
*/
function mailjet_user_domain_status($domain)
{
$mailjetApiClient = mailjet_new();
if (strpos($domain, '@') === FALSE) {
$domain = '*@' . $domain;
}
$body = [
'Email' => $domain,
];
$response = $mailjetApiClient->post(Resources::$Sender, ['body' => $body]);
if ($response->success()) {
return TRUE;
}
return FALSE;
}
/**
* Checks for same names.
*/
function _has_drupal_property($mailjet_property, $drupal_fields)
{
foreach ($drupal_fields as $key => $value) {
if ($value['Name'] == $mailjet_property['Name']) {
return TRUE;
}
}
return FALSE;
}
function mailjet_properties_sync()
{
$config_mailjet = Drupal::config('mailjet.settings');
//get all user fields from Drupal system
$user_fields = [];
//add this property by default
$user_fields[] = ["Name" => 'name', "Datatype" => 'str'];
$user_info = \Drupal\user\Entity\User::load(Drupal::currentUser()->id());
foreach ($user_info as $propertyName => $propertyValue) {
if (strpos($propertyName, "field_") !== FALSE) {
$currentField = $user_info->getFieldDefinitions()[$propertyName];
$currentFieldType = $currentField->getType();
$field_type = 'str';
if ($currentFieldType == "integer" || $currentFieldType == "number_integer" || $currentFieldType == 'timestamp') {
$field_type = 'int';
} else if ($currentFieldType == "float" || $currentFieldType == "number_float" || $currentFieldType == "decimal" || $currentFieldType == "number_decimal" || $currentFieldType == "commerce_price") {
$field_type = 'float';
} else if ($currentFieldType == "boolean" || $currentFieldType == "list_boolean") {
$field_type = 'bool';
} else if ($currentFieldType == "datetime") {
$field_type = 'datetime';
}
$user_fields[] = [
"Name" => str_replace('field_', '', $propertyName),
"Datatype" => $field_type,
];
}
}
//get existing contact properties from MailJet
$properties = [];
$mailjetApiClient = mailjet_new();
$response = \Drupal\mailjet\MailjetApi::getContactProperties();
if (FALSE !== $response) {
foreach ($response as $property) {
$properties[$property['Name']] = (array) $property;
}
}
//sync Drupal fields to MJ properties
foreach ($user_fields as $field) {
if (array_key_exists($field['Name'], $properties)) {
if ($field['Datatype'] == $properties[$field['Name']]['Datatype']) {
// \Drupal::messenger()->addMessage(t('Field '.$field['Name'].' is already in your MailJet account.'),'status'); // no need of this message - too much spam :)
} else {
$updateResponse = \Drupal\mailjet\MailjetApi::updateMailjetContactProperty($properties[$field['Name']]['ID'], $field['Name'], $field['Datatype']);
if (FALSE != $updateResponse) {
$properties[$field['Name']] = $field; // Add it here, so the next loop it wont be handled again
}
}
} else {
$insertResponse = \Drupal\mailjet\MailjetApi::createMailjetContactProperty($field['Name'], $field['Datatype']);
if (FALSE != $insertResponse) {
$properties[$field['Name']] = $field; // Add it here, so the next loop it wont be handled again
}
}
}
}
function processCustomFields($user)
{
$properties = [];
foreach ($user as $propertyName => $propertyValue) {
// Sync only the `name` from `user` entity
if ('name' == $propertyName) {
$properties[$propertyName] = $propertyValue->getString();
}
if (strpos($propertyName, "field_") !== FALSE) {
// Get property types and use it to cast property values
$currentField = $user->getFieldDefinitions()[$propertyName];
$currentFieldType = $currentField->getType();
if (!empty($propertyValue)) {
$castedPropertyValue = $propertyValue->getString();
if ($currentFieldType == "integer" || $currentFieldType == "number_integer" || $currentFieldType == "timestamp") {
$castedPropertyValue = intval($castedPropertyValue);
} else if ($currentFieldType == "float" || $currentFieldType == "number_float" || $currentFieldType == "decimal" || $currentFieldType == "number_decimal") {
$castedPropertyValue = floatval($castedPropertyValue);
} else if ($currentFieldType == "boolean" || $currentFieldType == "list_boolean") {
$castedPropertyValue = boolval($castedPropertyValue);
} else if ($currentFieldType == "datetime") {
$castedPropertyValue = date("c", strtotime($castedPropertyValue)); // RFC 3339 format
} else if ($currentFieldType == "commerce_price") {
$castedPropertyValue = floatval($castedPropertyValue);
}
$properties[str_replace('field_', '', $propertyName)] = $castedPropertyValue;
}
}
}
return $properties;
}
/**
* Add a Mailjet user.
* @param \Drupal\user\Entity\User $user
* The User object.
*/
function mailjet_user_insert(User $user)
{
if ($user->get('status')->value > 0) {
mailjet_sync_single_user($user, 'add');
}
}
/**
* Update a Mailjet user.
* @param \Drupal\user\Entity\User $user
* The User object.
*/
function mailjet_user_update(User $user)
{
// Only trigger updates when the account status has changed.
if (isset($user->get('status')->value)) {
if ($user->get('status')->value > 0) {
mailjet_sync_single_user($user, 'update');
} else {
mailjet_sync_single_user($user, 'remove');
}
}
}
/**
* Delete a Mailjet user.
* @param \Drupal\user\Entity\User $user
* The User object.
*/
function mailjet_user_delete(User $user)
{
mailjet_sync_single_user($user, 'remove');
}
/**
* Sync a single Mailjet user.
* @param \Drupal\user\Entity\User $user
* The User object.
* @param $action
* The action to send to Mailjet.
*/
function mailjet_sync_single_user(User $user, $action)
{
$config_mailjet = Drupal::config('mailjet.settings');
// Check sync status.
$mailjet_sync_on = $config_mailjet->get('mailjet_sync') ?? 0;
if (!$mailjet_sync_on || empty($user->get('mail')->value)) {
return;
}
$contact = [
'Email' => $user->get('mail')->value,
];
$mailjetApiClient = mailjet_new();
$mj_lists = [];
$mj_lists[] = mailjet_get_default_list_id($mailjetApiClient);
$properties = [];
switch ($action) {
case 'add':
case 'update':
foreach ($mj_lists as $listId) {
$properties = processCustomFields($user);
if (!empty($properties)) {
$contact['Properties'] = $properties;
}
mailjet_properties_sync();
// Add new contact.
$response = \Drupal\mailjet\MailjetApi::syncMailjetContact($listId, $contact);
if ($response) {
Drupal::logger('mailjet_messages')
->notice(t('The new contact was added to list #@list.', ['@list' => $listId]));
} else {
Drupal::logger('mailjet_messages')
->notice(t('The new contact was not added to list #@list.', ['@list' => $listId]));
}
}
break;
case 'remove':
foreach ($mj_lists as $listId) {
$contact = [
'Email' => $user->get('mail')->value,
];
//add new email
$response = \Drupal\mailjet\MailjetApi::syncMailjetContact($listId, $contact, $action);
if (FALSE == $response) {
Drupal::logger('mailjet_messages')
->notice(t('The contact was unsubscribed from list #@list.', ['@list' => $listId]));
} else {
Drupal::logger('mailjet_messages')
->notice(t('The contact was not unsubscribed from list #@list.', ['@list' => $listId]));
}
}
break;
default:
$messenger = Drupal::messenger();
$messenger->addMessage(t('No user action'), 'warning');
break;
}
}
/**
* Checks if batch process is running.
*/
function is_batchjob_running($bjID)
{
$config_mailjet = Drupal::config('mailjet.settings');
$mailjetApiClient = mailjet_new();
$filters = [
'APIKeyALT' => $config_mailjet->get('mailjet_username'),
];
$responseBatchJobCheck = $mailjetApiClient->get(Resources::$Batchjob, ['id' => $bjID, 'filters' => $filters]);
if ($responseBatchJobCheck->success()) {
$responseBatchJobCheckData = $responseBatchJobCheck->getData();
}
return !(!empty($responseBatchJobCheckData[0]['Status']) && $responseBatchJobCheckData[0]['Status'] == "Completed");
}
/**
* Gets Mailjet list id.
*/
function mailjet_get_default_list_id($mailjetApiClient)
{
$mailjetApiClient = mailjet_new();
// Gets Mailjet contact list by name
$contactsList = \Drupal\mailjet\MailjetApi::getMailjetContactListByName(mailjet_get_default_list_name());
if (FALSE != $contactsList) {
$def_list_id = $contactsList[0]['ID'];
}
return empty($def_list_id) ? mailjet_create_default_list() : $def_list_id;
}
function mailjet_get_default_list_name()
{
return 'Drupal contact list';
}
/**
* Create the default Drupal contact list if not existing.
*/
function mailjet_create_default_list()
{
$mailjetApiClient = mailjet_new();
$responseNewContactsList = \Drupal\mailjet\MailjetApi::createMailjetContactList(mailjet_get_default_list_name());
if (FALSE != $responseNewContactsList) {
$def_list_id = $responseNewContactsList[0]['ID'];
Drupal::messenger()->addMessage(t('Default list was created!'), 'status');
} else {
Drupal::messenger()->addMessage(t('Default list was not created! Please try again.'), 'error');
}
return empty($def_list_id) ? 0 : $def_list_id;
}
/**
* Sync lists.
*/
function mailjet_first_sync($list_id)
{
$config_mailjet = Drupal::service('config.factory')->getEditable('mailjet.settings');
$user_storage = Drupal::entityTypeManager()->getStorage('user');
$ids = $user_storage->getQuery()->accessCheck(FALSE)->execute();
$users = $user_storage->loadMultiple($ids);
$contacts = [];
foreach ($users as $user_id => $user) {
if ($user_id > 0 && $user->get('status')->value > 0) {
$contact = [];
$properties = processCustomFields($user);
$contact['Email'] = $user->getEmail();
if (!empty($properties)) {
$contact['Properties'] = $properties;
}
$contacts[] = $contact;
}
}
mailjet_properties_sync();
$responseInitialSync = \Drupal\mailjet\MailjetApi::syncMailjetContacts($list_id, $contacts);
if (!$responseInitialSync) {
Drupal::messenger()->addMessage(t('Could not create the contact list'));
}
Drupal::messenger()->addMessage(t('Sync in progress.'), 'status');
}
function get_mailjet_get_lists_limit()
{
return 500;
}
// Getter functions - LISTS and Properties
function mailjet_get_lists()
{
$contact_lists = [];
$mailjetApiClient = mailjet_new();
$contactsListResponse = \Drupal\mailjet\MailjetApi::getMailjetContactLists(get_mailjet_get_lists_limit());
if (FALSE != $contactsListResponse) {
foreach ($contactsListResponse as $list) {
$contact_lists[$list['ID']] = $list['Name'];
}
}
return $contact_lists;
}
function mailjet_get_properties()
{
//get existing contact properties from MailJet
$properties = [];
$mailjetApiClient = mailjet_new();
$response = \Drupal\mailjet\MailjetApi::getMailjetContactProperties();
if (FALSE != $response) {
foreach ($response as $property) {
$properties[$property['Name']] = $property['Name'];
}
}
return $properties;
}
function mailjet_help() {
$path = Drupal::service('extension.list.module')->getPath('mailjet');
if ($path === 'admin/help#mailjet') {
return t('Send your emails by your Mailjet API.');
}
}
/**
* Implements hook_mail().
*/
function mailjet_mail($key, &$message, $params)
{
$options = [
'langcode' => $message['langcode'],
];
switch ($key) {
case 'test_mail':
$message['subject'] = t('Your test mail from Mailjet', $options);
$message['body'][] = Drupal\Component\Utility\Html::escape($params['message']);
break;
case 'activation_mail':
$message['subject'] = t('Activation mail', $options);
$message['body'][] = $params['message'];
break;
}
}
/**
* Implements function for find a contact in some contact list
*/
function mailjet_find_conctact($email, $list_id)
{
$mailjetApiClient = mailjet_new();
$response = TRUE;
$filters = [
'ContactEmail' => $email,
'ContactsList' => $list_id,
];
$result = $mailjetApiClient->get(Resources::$Listrecipient, ['filters' => $filters]);
if ($result->success() && $result->getCount() > 0) {
$data = $result->getData();
if (isset($data[0]['IsUnsubscribed']) && TRUE == $data[0]['IsUnsubscribed']) {
$response = FALSE;
}
} else {
$response = FALSE;
}
return $response;
}
function prepare_mail_template($email_header, $email_text_button, $email_description, $email_footer, $email_owner, $url, $email_description_footer)
{
global $base_url;
$tempalate = '<div style="width: 100%; background-color: white; box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.35);">
<table class="mockup-content paint-area" style="background-color: white; font-family: Ubuntu, Helvetica; border-collapse: collapse; width: 100%;">
<tbody>
<tr style="text-align: center; padding: 0 0 20px 0;">
<td style="height: 75px; line-height: 75px; width: 100%; vertical-align: middle;">
<span id="opt-in-subscribe-title" class="paint-area paint-area--text" contenteditable="true" style="font-family: Ubuntu, Helvetica; font-size: 18px; color: #333333; line-height: normal;"><b class="medium-b">' . $email_header . '</b></span>
</td>
</tr>
<tr>
<td style="height: 75px; line-height: 75px; width: 100%; vertical-align: middle; padding: 0 60px;">
<div id="opt-in-display-text" class="paint-area paint-area--text" style="text-align: center; color: grey; margin-bottom: 25px; line-height: normal;" contenteditable="true">' . $base_url . '</div></td></tr>
<tr>
<td><a rel="notrack" class="subscribe" href="' . $url . '" style="text-align: center;display: block;color: white;font-weight: 500;
font-size: 15px;background-color: #3da7ea;text-transform: uppercase;text-decoration: none;width: 260px;margin:
auto;border-radius: 5px;padding: 16px;">' . $email_text_button . '</a></td>
</tr>
<tr>
<td style="padding: 30px;" >
<span style="text-align: center;display: block; font-weight:bold; font-size:14px; text-decoration: none;">' . $email_description . '</span> <br />
<a rel="notrack" class="subscribe" href="' . $url . '" style="text-align: center;display: block;color: black; font-size: 12px; text-decoration: underline; margin: auto;">
' . $url . '
</a>
</td>
</tr>
<tr><td style="text-align: left; height: 75px; line-height: 75px; width: 100%; vertical-align: middle; padding: 0 60px;"><div id="opt-in-footer" class="paint-area paint-area--text" contenteditable="true" style="display: inline-block; font-family: Ubuntu, Helvetica; text-align: left; margin-bottom: 24px; color: grey; line-height: normal;"><i class="medium-i">' . $email_description_footer . '</i></div></td></tr>
<tr><td style="text-align: left; padding: 0 60px; height: 30px; line-height: 30px;"><div id="opt-in-signature-top" class="paint-area paint-area--text" contenteditable="true" style="font-family: Ubuntu, Helvetica; display: inline-block; text-align: left; color: grey; line-height: normal;">' . $email_footer . '</div></td></tr>
<tr><td style="text-align: left; padding: 0 60px; height: 30px; line-height: 30px;"><div id="opt-in-signature-bottom" class="paint-area paint-area--text" contenteditable="true" style="font-family: Ubuntu, Helvetica; display: inline-block; text-align: left; color: grey; line-height: normal;"><b class="medium-b">' . $email_owner . '</b></div></td></tr>
</tbody>
</table>
</div>';
return $tempalate;
}
function mailjet_validate_entity_form(array &$form, FormStateInterface $form_state)
{
$form_values = $form_state->getValues();
if (count($form_values['fields_mailjet']) > 3) {
$form_state->setErrorByName('fields_mailjet', t('Please note that currently you may have a maximum of 3 contact properties in your subscribe form block. Reduce your selected contact properties.'));
}
}
function mailjet_get_propertiy_type($property_name)
{
//get existing contact properties from MailJet
$properties = [];
$mailjetApiClient = mailjet_new();
$response = \Drupal\mailjet\MailjetApi::getMailjetContactProperties();
if (FALSE != $response) {
foreach ($response as $property) {
if ($property['Name'] == $property_name) {
return $property['Datatype'];
}
}
}
}
function mailjet_get_property_name($id)
{
$mailjet_properties = mailjet_get_properties();
$counter_prop = 0;
foreach ($mailjet_properties as $key => $prop) {
if (strpos($prop, 'message') !== FALSE) {
unset($mailjet_properties[$key]);
} elseif (strpos($prop, 'commerce') !== FALSE) {
unset($mailjet_properties[$key]);
} elseif (strpos($prop, 'mailjet') !== FALSE) {
unset($mailjet_properties[$key]);
} elseif (strpos($prop, 'signup') !== FALSE) {
unset($mailjet_properties[$key]);
}
}
$conctact_prop = [];
foreach ($mailjet_properties as $key => $prop) {
$contact_prop[$counter_prop] = $prop;
$counter_prop++;
}
return $contact_prop[$id];
}
function mailjet_menu_local_actions_alter(&$local_actions)
{
$menu_action_links = [];
$counter = 0;
foreach ($local_actions as $action_link) {
switch ($action_link['id']) {
case 'view.order.page_1':
$menu_action_links[5]['route_name'] = $action_link['id'];
$menu_action_links[5]['title'] = accessProtected($action_link['title'], 'string');
$menu_action_links[5]['description'] = t('View the financial impact (total revenue and total number of sales) generated by each of your marketing campaigns');
break;
case 'mailjet_upgrade.page':
$menu_action_links[7]['route_name'] = $action_link['id'];
$menu_action_links[7]['title'] = accessProtected($action_link['title'], 'string');
$menu_action_links[7]['description'] = t('Click here to change/upgrade your current plan');
break;
case 'mailjet_my_profile.page':
$menu_action_links[6]['route_name'] = $action_link['id'];
$menu_action_links[6]['title'] = accessProtected($action_link['title'], 'string');
$menu_action_links[6]['description'] = t('Click here to change account settings, configure SPF and DKIM for optimized deliverability, etc.');
break;
case 'campaign.content':
$menu_action_links[2]['route_name'] = $action_link['id'];
$menu_action_links[2]['title'] = accessProtected($action_link['title'], 'string');
$menu_action_links[2]['description'] = t('Click here to access the Mailjet drag-and-drop newsletter tool, templates and more for your marketing campaigns');
break;
case 'list.content':
$menu_action_links[1]['route_name'] = $action_link['id'];
$menu_action_links[1]['title'] = accessProtected($action_link['title'], 'string');
$menu_action_links[1]['description'] = t('Click here to access the Mailjet contact lists');
break;
case 'stats.content':
$menu_action_links[4]['route_name'] = $action_link['id'];
$menu_action_links[4]['title'] = accessProtected($action_link['title'], 'string');
$menu_action_links[4]['description'] = t('Click here to gain insight into your user engagement (Clicks, Opens, Geographies, User Agents, Unsubs, etc.)');
break;
case 'trigger_examples.content':
$menu_action_links[3]['route_name'] = $action_link['id'];
$menu_action_links[3]['title'] = accessProtected($action_link['title'], 'string');
$menu_action_links[3]['description'] = t('Click here to define and activate different email marketing triggers (order reminder, purchase anniversary, etc.)');
break;
}
$counter++;
}
ksort($menu_action_links);
Drupal::state()->set('mailjet_action_links', $menu_action_links);
}
/**
* Implements hook_preprocess_HOOK() for menu-local-action templates.
*/
function mailjet_preprocess_block(&$variables)
{
$variables['#cache']['contexts'][] = 'url';
if ($_SERVER['REQUEST_URI'] === '/admin/config/system/mailjet/mailjet-panel') {
$action_links = Drupal::state()->get('mailjet_action_links');
$output_html = '<br/><ul class="admin-list">';
foreach ($action_links as $link) {
$url = Url::fromRoute($link['route_name']);
$internal_link = \Drupal\Core\Link::fromTextAndUrl($link['title'], $url);
$output_html .= '<li><span class="label">' . accessProtected($internal_link, 'generatedLink') . '</span><span class="description">' . accessProtected($link['description'], 'string') . '</span></li> ';
}
$output_html .= '</ul>';
$variables['content_menu']['#markup'] = $output_html;
} else {
$variables['content_menu'] = NULL;
}
}
/**
* @throws ReflectionException
*/
function accessProtected($obj, $prop)
{
$reflection = new ReflectionClass($obj);
if ($reflection->hasProperty($prop)) {
$property = $reflection->getProperty($prop);
$property->setAccessible(TRUE);
return $property->getValue($obj);
}
return '';
}
/**
* Implements hook_theme_registry_alter
*/
function mailjet_theme_registry_alter(&$theme_registry)
{
$theme_registry['block__local_actions_block']['path'] = Drupal::service('extension.list.module')->getPath('mailjet') . '/templates';
}
function mailjet_theme()
{
return [
'block__local_actions_block' => [
'template' => 'block--local-actions-block',
],
];
}
