-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonapi_frontend.api.php
More file actions
129 lines (123 loc) · 3.7 KB
/
jsonapi_frontend.api.php
File metadata and controls
129 lines (123 loc) · 3.7 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
123
124
125
126
127
128
129
<?php
/**
* @file
* Hooks and API documentation for JSON:API Frontend module.
*/
/**
* @defgroup jsonapi_frontend_api JSON:API Frontend API
* @{
* Information about the JSON:API Frontend module's API.
*
* The JSON:API Frontend module provides events that allow other modules to
* react to content changes and customize cache invalidation behavior.
*
* @section events Events
*
* The module dispatches the following Symfony event:
*
* - \Drupal\jsonapi_frontend\Event\HeadlessContentChangedEvent::EVENT_NAME
* ('jsonapi_frontend.content_changed'): Dispatched when a headless-enabled
* entity is inserted, updated, or deleted. Subscribers can use this event
* to add custom cache tags, modify paths, or perform additional actions.
*
* @section services Services
*
* The module provides the following services:
*
* - jsonapi_frontend.path_resolver: Resolves frontend paths to JSON:API
* resource URLs. Useful for programmatic path resolution.
*
* @}
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Example event subscriber for HeadlessContentChangedEvent.
*
* To subscribe to the HeadlessContentChangedEvent, create an event subscriber
* service in your module:
*
* @code
* // mymodule.services.yml
* services:
* mymodule.content_changed_subscriber:
* class: Drupal\mymodule\EventSubscriber\ContentChangedSubscriber
* tags:
* - { name: event_subscriber }
* @endcode
*
* @code
* // src/EventSubscriber/ContentChangedSubscriber.php
* namespace Drupal\mymodule\EventSubscriber;
*
* use Drupal\jsonapi_frontend\Event\HeadlessContentChangedEvent;
* use Symfony\Component\EventDispatcher\EventSubscriberInterface;
*
* class ContentChangedSubscriber implements EventSubscriberInterface {
*
* public static function getSubscribedEvents(): array {
* return [
* HeadlessContentChangedEvent::EVENT_NAME => ['onContentChanged', 10],
* ];
* }
*
* public function onContentChanged(HeadlessContentChangedEvent $event): void {
* // Add custom cache tags based on entity relationships.
* $entity = $event->getEntity();
*
* // Example: Add tags for referenced taxonomy terms.
* if ($entity->hasField('field_tags')) {
* foreach ($entity->get('field_tags')->referencedEntities() as $term) {
* $event->addTag('taxonomy_term:' . $term->uuid());
* }
* }
*
* // Example: Add a custom path for invalidation.
* $event->addPath('/custom/listing-page');
* }
*
* }
* @endcode
*
* @see \Drupal\jsonapi_frontend\Event\HeadlessContentChangedEvent
*/
/**
* Example programmatic path resolution.
*
* Use the path resolver service to resolve paths programmatically:
*
* @code
* $resolver = \Drupal::service('jsonapi_frontend.path_resolver');
* $result = $resolver->resolve('/about-us', 'en');
*
* if ($result['resolved']) {
* // Path was resolved.
* $kind = $result['kind']; // 'entity', 'view', 'redirect', or 'route'
* $headless = $result['headless']; // TRUE if headless-enabled
*
* if ($result['kind'] === 'entity') {
* $jsonapi_url = $result['jsonapi_url'];
* $entity_type = $result['entity']['type']; // e.g., 'node--page'
* $uuid = $result['entity']['id'];
* }
* elseif ($result['kind'] === 'view') {
* $data_url = $result['data_url'];
* }
* elseif ($result['kind'] === 'redirect') {
* $to = $result['redirect']['to'];
* $status = $result['redirect']['status']; // 301/302/etc
* }
* elseif ($result['kind'] === 'route') {
* // Non-entity Drupal route. Frontends typically proxy/redirect to drupal_url.
* $drupal_url = $result['drupal_url'];
* }
* }
* @endcode
*
* @see \Drupal\jsonapi_frontend\Service\PathResolver
*/
/**
* @} End of "addtogroup hooks".
*/