manifest/web/modules/custom/yufu_admin/yufu_admin.module

151 lines
4.7 KiB
PHP

<?php
/**
* @file
* Primary module hooks for Yufu Admin module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
/**
* Implements hook_entity_operation().
*/
function yufu_admin_entity_operation(EntityInterface $entity) {
$operations = [];
$entityType = $entity->getEntityType();
// @todo Only for entity node - should we add other entities as well?
if ($entityType->id() === 'node' && \Drupal::currentUser()->hasPermission('use jsonapi operation link')) {
// Build the url.
$url = Url::fromRoute(sprintf('jsonapi.%s--%s.individual', $entityType->id(), $entity->bundle()),
['entity' => $entity->uuid()]
);
$operations['view-jsonapi-output'] = [
'title' => t('jsonapi Output'),
'weight' => 50,
'url' => $url,
];
}
return $operations;
}
/**
* Implements hook_mail().
*/
function yufu_admin_mail($key, &$message, $params) {
$options = ['langcode' => $message['langcode']];
switch ($key) {
case 'concept_drafted':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('YUFU: New concept draft: @title', ['@title' => $params['node_title']], $options);
$message['body'][] = $params['message'];
break;
case 'concept_approved':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('YUFU: Your concept (changes) were approved: @title', ['@title' => $params['node_title']], $options);
$message['body'][] = $params['message'];
break;
case 'concept_rejected':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('YUFU: Your concept (changes) were rejected: @title', ['@title' => $params['node_title']], $options);
$message['body'][] = $params['message'];
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function yufu_admin_node_presave(EntityInterface $entity) {
if ($entity instanceOf NodeInterface && $entity->bundle() == 'concept') {
// Get transition: concept_drafted, concept_approved, concept_rejected.
if ($entity->isNew()) {
$transition = 'concept_drafted';
}
else {
$transition = _yufu_admin_get_node_transition($entity);
}
switch ($transition) {
case 'concept_drafted':
// Send email to editors.
_yufu_admin_concept_drafted_mail($entity);
break;
case 'concept_approved':
// Send email to revision creator (user).
break;
case 'concept_rejectd':
// Send email to revision creator (user).
// This transition happens on revision delete.
// Curently it is dead.
break;
}
}
}
/**
* Compare original node moderation state with current state.
*
* @param \Drupal\node\NodeInterface $node
* Node to check transition on.
*
* @return string|null
* Name of the transition.
*/
function _yufu_admin_get_node_transition(NodeInterface $node) {
$original = $node->original;
$moderation_state = $node->moderation_state->value;
$previous_state = $original->moderation_state->value;
if ($moderation_state == 'draft' && $previous_state == 'published') {
return 'concept_drafted';
}
else if ($moderation_state == 'published' && $previous_state == 'draft') {
return 'concept_approved';
}
else if ($moderation_state == 'published' && $previous_state == 'published') {
return 'stay_published';
}
else if ($moderation_state == 'draft' && $previous_state == 'draft') {
return 'stay_draft';
}
return NULL;
}
function _yufu_admin_concept_drafted_mail(NodeInterface $node) {
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'yufu_admin';
$key = 'concept_drafted';
// To all editors.
$to = _yufu_admin_get_all_editors_emails();
$params['message'] = t('New concept created: @title - @url', [
'title' => $node->getTitle(),
'url' => $node->toUrl()->toString(),
]);
$params['node_title'] = $entity->label();
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = true;
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
if ($result['result'] !== true) {
drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
}
else {
drupal_set_message(t('Your message has been sent.'));
}
}
function _yufu_admin_get_all_editors_emails() {
$user_storage = \Drupal::entityTypeManager()->getStorage('user');
$ids = $user_storage->getQuery()
->condition('status', 1)
->condition('roles', 'administrator')
->execute();
$editors = $user_storage->loadMultiple($ids);
$emails = [];
foreach ($editors as $editor) {
/* @var \Drupal\user\UserInterface $user */
$emails[] = $editor->getEmail();
}
return $emails;
}