-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonapi_frontend.module
More file actions
122 lines (103 loc) · 3.46 KB
/
jsonapi_frontend.module
File metadata and controls
122 lines (103 loc) · 3.46 KB
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
120
121
122
<?php
/**
* @file
* Hook implementations for JSON:API Frontend module.
*/
declare(strict_types=1);
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\jsonapi_frontend\Event\HeadlessContentChangedEvent;
/**
* Implements hook_entity_insert().
*/
function jsonapi_frontend_entity_insert(EntityInterface $entity): void {
_jsonapi_frontend_dispatch_content_event($entity, 'insert');
}
/**
* Implements hook_entity_update().
*/
function jsonapi_frontend_entity_update(EntityInterface $entity): void {
_jsonapi_frontend_dispatch_content_event($entity, 'update');
}
/**
* Implements hook_entity_delete().
*/
function jsonapi_frontend_entity_delete(EntityInterface $entity): void {
_jsonapi_frontend_dispatch_content_event($entity, 'delete');
}
/**
* Dispatches content changed event for headless entities.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that changed.
* @param string $operation
* The operation: 'insert', 'update', or 'delete'.
*/
function _jsonapi_frontend_dispatch_content_event(EntityInterface $entity, string $operation): void {
// Only dispatch for headless-enabled entities.
if (!_jsonapi_frontend_is_entity_headless($entity)) {
return;
}
// Only dispatch for published entities (or for delete operations).
if ($operation !== 'delete' && method_exists($entity, 'isPublished') && !$entity->isPublished()) {
return;
}
// Build cache tags.
$entity_type_id = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$uuid = $entity->uuid();
$tags = [
'drupal',
"type:{$entity_type_id}--{$bundle}",
"bundle:{$bundle}",
"{$entity_type_id}:{$uuid}",
"uuid:{$uuid}",
];
// Build paths.
$paths = [];
if ($entity->hasLinkTemplate('canonical')) {
try {
$url = $entity->toUrl('canonical');
$paths[] = $url->toString();
}
catch (\Exception $e) {
// Entity may not have a canonical URL - this is expected for some entity types.
\Drupal::logger('jsonapi_frontend')->debug('Could not get canonical URL for @type @uuid: @message', [
'@type' => $entity_type_id,
'@uuid' => $uuid,
'@message' => $e->getMessage(),
]);
}
}
// Dispatch the event.
$event = new HeadlessContentChangedEvent($entity, $operation, $paths, $tags);
/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $dispatcher */
$dispatcher = \Drupal::service('event_dispatcher');
$dispatcher->dispatch($event, HeadlessContentChangedEvent::EVENT_NAME);
}
/**
* Checks if an entity is configured as headless.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to check.
*
* @return bool
* TRUE if the entity is headless-enabled, FALSE otherwise.
*/
function _jsonapi_frontend_is_entity_headless(EntityInterface $entity): bool {
// Only content entities with canonical routes can be resolved headlessly.
if (!$entity instanceof ContentEntityInterface || !$entity->hasLinkTemplate('canonical')) {
return FALSE;
}
$entity_type_id = $entity->getEntityTypeId();
$config = \Drupal::config('jsonapi_frontend.settings');
// If enable_all is true, all supported entities are headless.
if ($config->get('enable_all')) {
return TRUE;
}
// Check if this specific bundle is enabled.
$bundle = $entity->bundle();
$bundle_key = "{$entity_type_id}:{$bundle}";
$enabled_bundles = $config->get('headless_bundles') ?: [];
return in_array($bundle_key, $enabled_bundles, TRUE);
}