This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
og_sm.module
227 lines (196 loc) · 6.03 KB
/
og_sm.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* @file
* Base module for the Organic Groups Sites functionality.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\og\OgGroupAudienceHelper;
use Drupal\og_sm\Entity\SiteNode;
use Drupal\og_sm\Event\SiteEvents;
use Drupal\og_sm\Event\SiteTypeEvent;
use Drupal\og_sm\Event\SiteTypeEvents;
use Drupal\og_sm\OgSm;
/**
* Implements hook_hook_info().
*/
function og_sm_hook_info() {
$group = ['group' => 'og_sm'];
$hooks = [
'og_sm_site_view' => $group,
'og_sm_site_homepage_alter' => $group,
'og_sm_site_menu_links_discovered_alter' => $group,
];
return $hooks;
}
/**
* Implements hook_form_FORM_ID_alter() for node_type_form().
*/
function og_sm_form_node_type_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
$form_state->loadInclude('og_sm', 'inc', 'og_sm.admin');
_og_sm_form_node_type_form_alter($form, $form_state);
}
/**
* Implements hook_entity_field_values_init().
*/
function og_sm_entity_field_values_init(FieldableEntityInterface $entity) {
$site = OgSm::siteManager()->currentSite();
if (!$site) {
return;
}
/** @var \Drupal\og\OgGroupAudienceHelperInterface $groupAudienceHelper */
$groupAudienceHelper = Drupal::service('og.group_audience_helper');
$fields = $groupAudienceHelper->getAllGroupAudienceFields(
$entity->getEntityTypeId(),
$entity->bundle(),
$site->getEntityTypeId(),
$site->bundle()
);
if (!$fields) {
return;
}
$entity->set(reset($fields)->getName(), $site->id());
}
/**
* Implements hook_ENTITY_TYPE_view() for node entities.
*/
function og_sm_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
// Only for Site node types.
if (!OgSm::isSite($node)) {
return;
}
Drupal::moduleHandler()->invokeAll('og_sm_site_view', [
$build,
$node,
$display,
$view_mode,
]);
}
/**
* Implements hook_ENTITY_TYPE_delete() for node entities.
*/
function og_sm_node_delete(NodeInterface $node) {
OgSm::siteEventDispatch(SiteEvents::DELETE, $node);
}
/**
* Implements hook_ENTITY_TYPE_presave() for node entities.
*/
function og_sm_node_presave(NodeInterface $node) {
OgSm::siteEventDispatch(SiteEvents::PRESAVE, $node);
}
/**
* Implements hook_ENTITY_TYPE_update() for node entities.
*/
function og_sm_node_update(NodeInterface $node) {
OgSm::siteEventDispatch(SiteEvents::UPDATE, $node);
}
/**
* Implements hook_ENTITY_TYPE_insert() for node entities.
*/
function og_sm_node_insert(NodeInterface $node) {
OgSm::siteEventDispatch(SiteEvents::INSERT, $node);
}
/**
* Implements hook_ENTITY_TYPE_update() for node_type entities.
*/
function og_sm_node_type_update(NodeTypeInterface $node_type) {
_og_sm_node_type_save($node_type);
}
/**
* Implements hook_ENTITY_TYPE_insert() for node_type entities.
*/
function og_sm_node_type_insert(NodeTypeInterface $node_type) {
_og_sm_node_type_save($node_type);
}
/**
* Helper function called when a node type is either inserted or updated.
*
* @param \Drupal\node\NodeTypeInterface $node_type
* The node type.
*/
function _og_sm_node_type_save(NodeTypeInterface $node_type) {
$isSiteTypeOriginal = FALSE;
if (isset($node_type->original)) {
$isSiteTypeOriginal = OgSm::isSiteType($node_type->original);
}
$is_site_type = OgSm::isSiteType($node_type);
if ($isSiteTypeOriginal === $is_site_type) {
return;
}
$event = new SiteTypeEvent($node_type);
$event_name = $is_site_type
? SiteTypeEvents::ADD
: SiteTypeEvents::REMOVE;
\Drupal::service('event_dispatcher')->dispatch($event, $event_name);
}
/**
* Implements hook_ENTITY_TYPE_delete() for node_type entities.
*/
function og_sm_node_type_delete(NodeTypeInterface $node_type) {
if (OgSm::isSiteType($node_type)) {
$event = new SiteTypeEvent($node_type);
\Drupal::service('event_dispatcher')->dispatch($event, SiteTypeEvents::REMOVE);
}
}
/**
* Implements hook_entity_field_access().
*/
function og_sm_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
// Only intervene with audience fields.
if (!OgGroupAudienceHelper::isGroupAudienceField($field_definition)) {
return AccessResult::neutral();
}
// Don't intervene when we are not within site context.
if (!OgSm::siteManager()->currentSite()) {
return AccessResult::neutral();
}
// Hide the audience field when we have site context since it will be
// prefilled in og_sm_field_widget_form_alter() anyway.
return AccessResult::forbidden();
}
/**
* Helper function that checks whether an account has a certain site permission.
*
* @param string $operation
* The entity operation being checked for.
* @param \Drupal\Core\Session\AccountInterface $account
* (optional) The user to check. Defaults to the current user.
*
* @return bool
* TRUE if the user has the site permission.
*/
function og_sm_site_user_access($operation, AccountInterface $account = NULL) {
if (!isset($account)) {
$account = \Drupal::currentUser()->getAccount();
}
$site = OgSm::siteManager()->currentSite();
if (!$site) {
return FALSE;
}
/** @var \Drupal\og\OgAccess $og_access */
$og_access = \Drupal::service('og.access');
return $og_access->userAccess($site, $operation, $account)->isAllowed();
}
/**
* Implements hook_entity_bundle_info_alter().
*
* Adds custom class for site path.
*/
function og_sm_entity_bundle_info_alter(array &$bundles): void {
if (!isset($bundles['node'])) {
return;
}
$siteTypeManager = \Drupal::service('og_sm.site_type_manager');
foreach (array_keys($bundles['node']) as $bundle) {
if ($siteTypeManager->isSiteTypeId($bundle)) {
$bundles['node'][$bundle]['class'] = SiteNode::class;
}
}
}