remp-8.x-1.x-dev/remp.module
remp.module
<?php
/**
* @file
* Contains remp.module.
*/
use Drupal\Component\Serialization\Json;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
/**
* Implements hook_help().
*/
function remp_help($route_name,
RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the remp module.
case 'help.page.remp':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The REMP CRM integration module.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_preprocess_node().
*/
function remp_preprocess_node(&$variables) {
if ($variables['view_mode'] === 'anonym') {
unset($variables['label']);
}
if ($variables['view_mode'] === 'member') {
unset($variables['label']);
$variables['display_submitted'] = FALSE;
}
}
/**
* Implements hook_ENTITY_TYPE_view_alter().
*/
function remp_node_view_alter(array &$build,
EntityInterface $entity,
EntityViewDisplayInterface $display) {
/** @var \Drupal\node\Entity\Node $entity */
if (
isset($build['#entity_type'])
&& $build['#entity_type'] === 'node'
&& $build['#view_mode'] === 'full'
&& !empty($entity->remp_member_only->value)
) {
$config = \Drupal::config('remp.config');
if (empty($config->get('custom'))) {
/** @var \Drupal\Core\Entity\EntityViewBuilderInterface $view_builder */
$view_builder = \Drupal::service('entity_type.manager')
->getViewBuilder('node');
foreach (Element::children($build) as $child) {
unset($build[$child]);
}
$build['anonym'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'remp-anonym',
'class' => ['only-anonym'],
],
'content' => $view_builder->view($entity, 'anonym'),
];
$build['member'] = [
'#type' => 'container',
'#attributes' => [
// TODO: make this ID unique for every content.
'id' => 'remp-member',
'class' => ['only-member'],
'data-remp-id' => $entity->getEntityTypeId() . ':' . $entity->id(),
],
];
}
}
if (
isset($build['#entity_type'])
&& $build['#entity_type'] === 'node'
&& $build['#view_mode'] === 'full'
) {
if (empty($entity->remp_member_only->value)) {
$build['#attached']['drupalSettings']['remp']['beam']['article'] = [
'id' => 'node/' . $entity->id(),
'author' => $entity->getOwner()->id(),
'locked' => !empty($entity->remp_member_only->value),
];
}
else {
$build['#attached']['drupalSettings']['remp']['beam']['wait'] = TRUE;
}
}
}
/**
* Implements hook_page_attachments().
*/
function remp_page_attachments(array &$attachments) {
$config = \Drupal::config('remp.config');
if (!empty($config->get('host'))) {
$attachments['#attached']['library'][] = 'remp/remp';
$attachments['#attached']['drupalSettings']['remp'] = [
'host' => $config->get('host'),
'custom' => $config->get('custom'),
];
}
if (!empty($campaign_host = $config->get('campaign_host'))) {
$attachments['#attached']['library'][] = 'remp/campaign';
$attachments['#attached']['drupalSettings']['remp']['campaign_host'] = $campaign_host;
}
if (!empty($beam_host = $config->get('beam_host'))) {
$attachments['#attached']['library'][] = 'remp/beam';
$beam_settings = [
'token' => $config->get('beam_property'),
'tracker' => [
'url' => $config->get('beam_tracker'),
],
];
$beam_settings['tracker']['timeSpent'] = [
'enabled' => TRUE,
'interval' => 5,
];
$beam_settings['tracker']['readingProgress'] = [
'enabled' => TRUE,
'interval' => 5,
];
$attachments['#attached']['drupalSettings']['remp']['beam'] = $beam_settings;
$attachments['#attached']['drupalSettings']['remp']['beam_host'] = $beam_host;
}
}
/**
* Implements hook_theme().
*/
function remp_theme($existing, $type, $theme, $path) {
return [
'remp_footer' => [
'variables' => [
'remaining_articles' => 0,
'subscribe' => '',
],
],
'remp_user_links' => [
'variables' => [
'links' => NULL,
'logged_in' => NULL,
],
],
];
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function remp_node_insert(NodeInterface $node) {
remp_beam_article_upsert($node);
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function remp_node_update(NodeInterface $node) {
remp_beam_article_upsert($node);
}
/**
* Upserts article data to remp beam.
*/
function remp_beam_article_upsert(NodeInterface $node) {
$config = \Drupal::config('remp.config');
$host = $config->get('beam_host');
$property_uuid = $config->get('beam_property');
$sso_token = $config->get('beam_sso_token');
if (!empty($host) && !empty($property_uuid)) {
$api_url = $host . '/api/v2/articles/upsert';
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $sso_token,
];
$author = $node->getOwner();
$body = [
'articles' => [
$node->uuid() => [
'external_id' => $node->uuid(),
'property_uuid' => $property_uuid,
'title' => $node->getTitle(),
'url' => Url::fromRoute('entity.node.canonical', ['node' => $node->id()], ['absolute' => TRUE])->toString(),
'authors' => [
[
'external_id' => $author->id(),
'name' => $author->getAccountName(),
],
],
],
],
];
if ($node->isPublished()) {
$body['articles'][$node->uuid()]['published_at'] = date(DATE_RFC3339, $node->getChangedTime());
}
try {
$httpClient = \Drupal::service('http_client');
$httpClient->post(
$api_url, [
'headers' => $headers,
'body' => Json::encode($body),
]
);
}
catch (\Exception $e) {
}
}
}
