-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsilverback_gatsby.module
119 lines (107 loc) · 3.69 KB
/
silverback_gatsby.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\silverback_gatsby\Plugin\Gatsby\Feed\EntityFeed;
use Drupal\user\UserInterface;
function _silverback_gatsby_entity_event(EntityInterface $entity) {
/** @var \Drupal\silverback_gatsby\GatsbyUpdateHandler $updateHandler */
$updateHandler = \Drupal::service('silverback_gatsby.update_handler');
$updateHandler->handle(EntityFeed::class, $entity);
if (\Drupal::hasService('entity_usage.usage')) {
/** @var \Drupal\entity_usage\EntityUsage $entityUsage */
$entityUsage = \Drupal::service('entity_usage.usage');
foreach ($entityUsage->listSources($entity) as $sourceEntityType => $sources) {
$entities = \Drupal::entityTypeManager()
->getStorage($sourceEntityType)
->loadMultiple(array_keys($sources));
foreach ($entities as $source) {
$updateHandler->handle(EntityFeed::class, $source);
}
}
}
}
/**
* Implements hook_entity_insert().
*/
function silverback_gatsby_entity_insert(EntityInterface $entity) {
_silverback_gatsby_entity_event($entity);
}
/**
* Implements hook_entity_presave().
*/
function silverback_gatsby_entity_presave(EntityInterface $entity) {
if (!empty($entity->original)) {
_silverback_gatsby_entity_event($entity->original);
}
}
/**
* Implements hook_entity_update().
*/
function silverback_gatsby_entity_update(EntityInterface $entity) {
\Drupal::entityTypeManager()
->getAccessControlHandler($entity->getEntityTypeId())
->resetCache();
_silverback_gatsby_entity_event($entity);
}
/**
* Implements hook_entity_predelete().
*/
function silverback_gatsby_entity_predelete(EntityInterface $entity) {
_silverback_gatsby_entity_event($entity);
}
/**
* Implements hook_module_implements_alter().
*/
function silverback_gatsby_module_implements_alter(&$implementations, $hook) {
// Act before entity_usage module.
if (in_array($hook, ['entity_insert', 'entity_update', 'entity_predelete'], TRUE)) {
$implementations = [
'silverback_gatsby' => $implementations['silverback_gatsby'],
] + $implementations;
}
}
/**
* Implements hook_entity_type_alter().
*/
function silverback_gatsby_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
foreach ($entity_types as $entity_type) {
if ($entity_type->id() === 'graphql_server') {
if (!$entity_type->hasHandlerClass('build')) {
$entity_type->setHandlerClass(
'build',
Drupal\silverback_gatsby\GraphQL\Build::class
);
}
if (!$entity_type->getFormClass('build')) {
$entity_type->setFormClass(
'build',
Drupal\silverback_gatsby\GraphQL\Build::class
);
}
}
}
}
/**
* Implements hook_user_login().
*/
function silverback_gatsby_user_login(UserInterface $account) {
// Write a javascript-accessible cookie that tells the frontend if the user is
// authenticated, so it can make additional requests based on that information.
$opts = \Drupal::getContainer()->getParameter('session.storage.options');
if (isset($opts['cookie_domain'])) {
$expires = isset($opts['cookie_lifetime'])
? \Drupal::time()->getRequestTime() + $opts['cookie_lifetime']
: 0;
setcookie('drupal_user', $account->id(), $expires, '/', $opts['cookie_domain'], false, false);
}
}
/**
* Implements hook_user_logout().
*/
function silverback_gatsby_user_logout() {
// Unset the frontend user-indicator cookie.
unset($_COOKIE['drupal_user']);
$opts = \Drupal::getContainer()->getParameter('session.storage.options');
$expires = \Drupal::time()->getRequestTime() - 3600;
setcookie('drupal_user', '', $expires, '/', $opts['cookie_domain'] ?? '', false, false);
}