-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultimate_offer.module
86 lines (73 loc) · 2.18 KB
/
ultimate_offer.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* @file
* Contains ultimate_offer.module
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_theme()
*/
function ultimate_offer_theme() {
$themes['ultimate_offer_form'] = [
'render element' => 'form',
'template' => 'ultimate_offer_form'
];
return $themes;
}
/**
* Implementation of hook_ENTITY_TYPE_view()
*
* Ultimate offer can be viewed by the administrator only
*/
function ultimate_offer_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
if($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'ultimate_offers' && $view_mode == 'full') {
$account = \Drupal::currentUser();
if($account->id() == 0) {
drupal_set_message(t('Access Denied'));
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
if($language == 'en') {
$url = Url::fromRoute('<front>');
}
else {
$url = Url::fromRoute('<front>', [], ['language' => $language]);
}
$redirect = new RedirectResponse($url->toString());
return $redirect->send();
}
$roles = \Drupal::currentUser()->getRoles();
if(!in_array("Administrator", $roles)) {
drupal_set_message(t('Access Denied'));
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
if($language == 'en') {
$url = Url::fromRoute('<front>');
}
else {
$url = Url::fromRoute('<front>', [], ['language' => $language]);
}
$redirect = new RedirectResponse($url->toString());
return $redirect->send();
}
$build['#cache']['max-age'] = 0;
}
//don't cache
\Drupal::service('page_cache_kill_switch')->trigger();
}
/**
* Implements hook_mail().
*/
function ultimate_offer_mail($key, &$message, $params) {
$options = array(
'langcode' => $message['langcode'],
);
switch ($key) {
case 'create_article':
$message['from'] = $params['from'];
$message['subject'] = $params['title'];
$message['body'][] = $params['message'];
break;
}
}