campaignmonitor_campaign.module in Campaign Monitor 8
Module file for campaignmonitor_campaign.
File
modules/campaignmonitor_campaign/campaignmonitor_campaign.moduleView source
<?php
/**
* @file
* @file
* Module file for campaignmonitor_campaign.
*/
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\campaignmonitor\CampaignMonitor;
use Symfony\Component\HttpFoundation\Request;
/**
* .
*
* Helper function to get all campaigns from Campaign Manager.
*
* @return mixed
*/
/**
*
*/
function campaignmonitor_campaign_get_campaigns() {
$cm = CampaignMonitor::getConnector();
return $cm
->getCampaigns();
}
/**
*
*/
function campaignmonitor_campaign_get_configured_node_types() {
$config = \Drupal::config('campaignmonitor_campaign.settings');
return $config
->get('node_types') != NULL ? $config
->get('node_types') : [];
}
/**
*
*/
function campaignmonitor_campaign_type_is_configured($node_type) {
$types = campaignmonitor_campaign_get_configured_node_types();
if (in_array($node_type, $types, TRUE)) {
return TRUE;
}
return FALSE;
}
/**
* Return campaignmonitor_campaign' settings.
*
* @param $setting
* One of the content_access_available_settings(), e.g. 'view' or 'per_node'.
* If 'all' is passed, all available settings are returned.
* @param $type_name
* The name of the content type to return settings for.
*
* @return
* The value of the given setting or an array of all settings.
*/
function campaignmonitor_campaign_get_node_settings($setting, $type_name) {
$config = \Drupal::configFactory()
->getEditable('campaignmonitor_campaign.node_settings');
$settings = unserialize($config
->get('campaignmonitor_campaign_node_type.' . $type_name));
if (empty($settings)) {
$settings = [];
}
if (empty($settings)) {
$settings = [];
}
if ($setting == 'all') {
return $settings;
}
return isset($settings[$setting]) ? $settings[$setting] : [];
}
/**
* Save campaignmonitor_campaign settings of a content type.
*/
function campaignmonitor_campaign_set_node_settings($settings, $type_name) {
$config = \Drupal::configFactory()
->getEditable('campaignmonitor_campaign.node_settings');
$config
->set('campaignmonitor_campaign_node_type.' . $type_name, serialize($settings));
$config
->save();
}
/**
* Return an array containing all available campaignmonitor_campaign settings.
*/
function campaignmonitor_campaign_available_settings() {
return [
'lists',
'view_mode',
];
}
/**
* Helper function to get Draft campaigns (not sent yet)
*
* @return mixed
*/
function campaignmonitor_campaign_get_drafts() {
$cm = CampaignMonitor::getConnector();
return $cm
->getDrafts();
}
/**
* Implements hook_css_alter().
* See AssetResolver.
*
* @param $css
* @param \Drupal\Core\Asset\AttachedAssetsInterface $assets
*/
function campaignmonitor_campaign_css_alter(&$css, AttachedAssetsInterface $assets) {
return;
$base_url = Request::createFromGlobals()
->getSchemeAndHttpHost();
$settings = $assets
->getSettings();
$newcss = [];
// This is the only method to identify context.
// Presumably when we generate the css for the campaign there are no settings because we haven't applied any.
if (empty($settings)) {
foreach ($css as $path => $values) {
$newpath = $base_url . '/' . $path;
$values['data'] = $newpath;
// In order to display absolute path we need to identify the type as external.
// See CssCollectionRenderer.
$values['type'] = 'external';
$newcss[$newpath] = $values;
}
$css = $newcss;
}
}
/**
* Implements hook_entity_storage_load()
*/
function campaignmonitor_campaign_entity_storage_load(array $entities, $entity_type) {
if ($entity_type != 'campaignmonitor_campaign') {
return;
}
$ids = [];
if (!empty($entities)) {
foreach ($entities as $campaign) {
$ids[] = $campaign
->getMcCampaignId();
}
}
$mc_campaigns = campaignmonitor_campaign_get_campaigns($ids);
foreach ($entities as $mc_campaign_id => $campaign) {
$campaign->mc_data = $mc_campaigns[$mc_campaign_id];
// Lists are cached separately, but we want to load them here.
if (isset($campaign->mc_data->recipients->list_id) && $campaign->mc_data->recipients->list_id) {
$campaign->list = campaignmonitor_get_list($campaign->mc_data->recipients->list_id);
}
if (isset($campaign->mc_data->settings->template_id) && $campaign->mc_data->settings->template_id) {
$campaign->mc_template = campaignmonitor_campaign_get_template($campaign->mc_data->settings->template_id);
}
}
}
/**
* Implements hook_theme().
*/
function campaignmonitor_campaign_theme($existing, $type, $theme, $path) {
return [
'campaignmonitor_campaign_html' => [
'variables' => [
'html' => '',
],
'template' => 'campaignmonitor-campaign-html',
],
'campaignmonitor_archive' => [
'variables' => [
'campaigns' => NULL,
],
'template' => 'block--campaignmonitor-archive',
],
];
}
/**
* Helper function to return the real or http path for campaign html.
*
* @param $node
* @param string $type
*
* @return string
*/
function campaignmonitor_campaign_get_filepath($node, $type = 'realpath') {
$path = file_create_url("public://");
$realpath = \Drupal::service('file_system')
->realpath("public://");
$dir = 'campaignmonitor';
if (!file_exists($realpath . '/' . $dir)) {
mkdir($realpath . '/' . $dir, 0777);
}
// Create a filename using the node id.
$filename = 'campaignmonitor-campaign-' . $node
->id() . '.html';
switch ($type) {
case 'realpath':
return $realpath . '/' . $dir . '/' . $filename;
default:
return $path . $dir . '/' . $filename;
}
}
Functions
Name | Description |
---|---|
campaignmonitor_campaign_available_settings | Return an array containing all available campaignmonitor_campaign settings. |
campaignmonitor_campaign_css_alter | Implements hook_css_alter(). See AssetResolver. |
campaignmonitor_campaign_entity_storage_load | Implements hook_entity_storage_load() |
campaignmonitor_campaign_get_campaigns | |
campaignmonitor_campaign_get_configured_node_types | |
campaignmonitor_campaign_get_drafts | Helper function to get Draft campaigns (not sent yet) |
campaignmonitor_campaign_get_filepath | Helper function to return the real or http path for campaign html. |
campaignmonitor_campaign_get_node_settings | Return campaignmonitor_campaign' settings. |
campaignmonitor_campaign_set_node_settings | Save campaignmonitor_campaign settings of a content type. |
campaignmonitor_campaign_theme | Implements hook_theme(). |
campaignmonitor_campaign_type_is_configured |