Skip to content

Commit 479f8ed

Browse files
tatarbjgheydon
authored andcommitted
Gizra#242 Improvements for og_access port. (Gizra#2)
* Gizra#242 Remove og_ui dependency, replace defines with OgAccess class and its consts, also in the whole module. * Gizra#242 Grammar issue is fixed. * Gizra#242 Introduce service injection and replace t() with the injected one. * Gizra#242 PHPCS fixes.
1 parent 9cb2c72 commit 479f8ed

File tree

6 files changed

+69
-54
lines changed

6 files changed

+69
-54
lines changed

og_access/og_access.info.yml

-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ type: module
77

88
dependencies:
99
- og
10-
- og_ui

og_access/og_access.module

+18-42
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,7 @@ use Drupal\field\Entity\FieldConfig;
1818
use Drupal\node\NodeInterface;
1919
use Drupal\og\Og;
2020
use Drupal\og_access\OgAccessBundleFormAlter;
21-
22-
/**
23-
* The access realm of group member.
24-
*/
25-
define('OG_ACCESS_REALM', 'og_access');
26-
27-
/**
28-
* Group public access field.
29-
*/
30-
define('OG_ACCESS_FIELD', 'group_access');
31-
32-
/**
33-
* Group public access field.
34-
*/
35-
define('OG_ACCESS_CONTENT_FIELD', 'group_content_access');
36-
37-
/**
38-
* Public group/group content access.
39-
*/
40-
define('OG_ACCESS_PUBLIC', 0);
41-
42-
/**
43-
* Private group/group content access.
44-
*/
45-
define('OG_ACCESS_PRIVATE', 1);
21+
use Drupal\og_access\OgAccess;
4622

4723
/**
4824
* Implements hook_node_grants().
@@ -58,7 +34,7 @@ function og_access_node_grants(AccountInterface $account, $op) {
5834
foreach ($groups as $group_type => $entity_groups) {
5935
/** @var \Drupal\core\Entity\EntityInterface $group */
6036
foreach ($entity_groups as $group) {
61-
$realm = OG_ACCESS_REALM . ':' . $group_type;
37+
$realm = OgAccess::OG_ACCESS_REALM . ':' . $group_type;
6238
$grants[$realm][] = $group->id();
6339
}
6440
}
@@ -72,30 +48,30 @@ function og_access_node_grants(AccountInterface $account, $op) {
7248
*/
7349
function og_access_node_access_records(NodeInterface $node) {
7450
if (!$node->isPublished()) {
75-
// Node is unpublished, so we don't allow every group member to see it.
51+
// Node is unpublished, so we don't allow any group member to see it.
7652
return [];
7753
}
7854

7955
// The group IDs, that in case access is granted, will be recorded.
8056
$gids = [];
8157

8258
if (Og::isGroup('node', $node->getType()) &&
83-
$node->hasField(OG_ACCESS_FIELD) &&
84-
!empty($node->{OG_ACCESS_FIELD}) && $node->{OG_ACCESS_FIELD}->value) {
59+
$node->hasField(OgAccess::OG_ACCESS_FIELD) &&
60+
!empty($node->{OgAccess::OG_ACCESS_FIELD}) && $node->{OgAccess::OG_ACCESS_FIELD}->value) {
8561
// Private group.
8662
$gids['node'][] = $node->id();
8763
}
8864

89-
if ($node->hasField(OG_ACCESS_CONTENT_FIELD) &&
90-
!empty($node->get(OG_ACCESS_CONTENT_FIELD))) {
91-
$content_access = $node->get(OG_ACCESS_CONTENT_FIELD)->value;
65+
if ($node->hasField(OgAccess::OG_ACCESS_CONTENT_FIELD) &&
66+
!empty($node->get(OgAccess::OG_ACCESS_CONTENT_FIELD))) {
67+
$content_access = $node->get(OgAccess::OG_ACCESS_CONTENT_FIELD)->value;
9268
}
9369
else {
94-
$content_access = OG_ACCESS_PUBLIC;
70+
$content_access = OgAccess::OG_ACCESS_PUBLIC;
9571
}
9672

9773
switch ($content_access) {
98-
case OG_ACCESS_PUBLIC:
74+
case OgAccess::OG_ACCESS_PUBLIC:
9975
// Skip non-group content nodes.
10076
if (!Og::isGroupContent('node', $node->getType())) {
10177
break;
@@ -114,8 +90,8 @@ function og_access_node_access_records(NodeInterface $node) {
11490
continue;
11591
}
11692

117-
if ($group->hasField(OG_ACCESS_FIELD) && !empty($group->get(OG_ACCESS_FIELD)) &&
118-
$group->get(OG_ACCESS_FIELD)->value) {
93+
if ($group->hasField(OgAccess::OG_ACCESS_FIELD) && !empty($group->get(OgAccess::OG_ACCESS_FIELD)) &&
94+
$group->get(OgAccess::OG_ACCESS_FIELD)->value) {
11995
$has_private = TRUE;
12096
}
12197
}
@@ -125,7 +101,7 @@ function og_access_node_access_records(NodeInterface $node) {
125101
}
126102
break;
127103

128-
case OG_ACCESS_PRIVATE:
104+
case OgAccess::OG_ACCESS_PRIVATE:
129105
$list_gids = [];
130106
/** @var \Drupal\og\OgGroupAudienceHelper $audience_helper */
131107
$audience_helper = \Drupal::service('og.group_audience_helper');
@@ -142,7 +118,7 @@ function og_access_node_access_records(NodeInterface $node) {
142118
foreach ($gids as $group_type => $values) {
143119
foreach ($values as $gid) {
144120
$grants[] = [
145-
'realm' => OG_ACCESS_REALM . ':' . $group_type,
121+
'realm' => OgAccess::OG_ACCESS_REALM . ':' . $group_type,
146122
'gid' => $gid,
147123
'grant_view' => 1,
148124
'grant_update' => 0,
@@ -198,9 +174,9 @@ function og_access_entity_type_save(EntityInterface $entity) {
198174
// Add/remove on the group itself.
199175
$is_group = Og::isGroup($entity_type_id, $bundle);
200176
if ($entity->og_is_group || $is_group) {
201-
$field = FieldConfig::loadByName($entity_type_id, $bundle, OG_ACCESS_FIELD);
177+
$field = FieldConfig::loadByName($entity_type_id, $bundle, OgAccess::OG_ACCESS_FIELD);
202178
if (!$field && $enable_og_access) {
203-
Og::createField(OG_ACCESS_FIELD, $entity_type_id, $bundle);
179+
Og::createField(OgAccess::OG_ACCESS_FIELD, $entity_type_id, $bundle);
204180
}
205181
elseif ($field) {
206182
if (!$enable_og_access || $is_group && !$entity->og_is_group) {
@@ -212,10 +188,10 @@ function og_access_entity_type_save(EntityInterface $entity) {
212188
// Add remove the relevant field to the group content bundle.
213189
$is_group_content = Og::isGroupContent($entity_type_id, $bundle);
214190
if ($entity->og_group_content_bundle || $is_group_content) {
215-
$field = FieldConfig::loadByName($entity_type_id, $bundle, OG_ACCESS_CONTENT_FIELD);
191+
$field = FieldConfig::loadByName($entity_type_id, $bundle, OgAccess::OG_ACCESS_CONTENT_FIELD);
216192

217193
if (!$field && $enable_og_access) {
218-
Og::createField(OG_ACCESS_CONTENT_FIELD, $entity_type_id, $bundle);
194+
Og::createField(OgAccess::OG_ACCESS_CONTENT_FIELD, $entity_type_id, $bundle);
219195
}
220196
elseif ($field) {
221197
if (!$enable_og_access || $is_group_content && !$entity->og_group_content_bundle) {

og_access/src/OgAccess.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Drupal\og_access;
4+
5+
/**
6+
* Helper class for constants.
7+
*/
8+
class OgAccess {
9+
10+
/**
11+
* The access realm of group member.
12+
*/
13+
const OG_ACCESS_REALM = 'og_access';
14+
15+
/**
16+
* Group public access field.
17+
*/
18+
const OG_ACCESS_FIELD = 'group_access';
19+
20+
/**
21+
* Group public access field.
22+
*/
23+
const OG_ACCESS_CONTENT_FIELD = 'group_content_access';
24+
25+
/**
26+
* Public group/group content access.
27+
*/
28+
const OG_ACCESS_PUBLIC = 0;
29+
30+
/**
31+
* Private group/group content access.
32+
*/
33+
const OG_ACCESS_PRIVATE = 1;
34+
35+
}

og_access/src/OgAccessBundleFormAlter.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use Drupal\Core\Entity\EntityInterface;
66
use Drupal\Core\Form\FormStateInterface;
77
use Drupal\og\Og;
8+
use Drupal\Core\StringTranslation\TranslationInterface;
89

910
/**
1011
* Helper for og_access_form_alter().
1112
*/
1213
class OgAccessBundleFormAlter {
14+
use StringTranslationTrait;
1315

1416
/**
1517
* The entity bundle.
@@ -37,9 +39,12 @@ class OgAccessBundleFormAlter {
3739
*
3840
* @param \Drupal\Core\Entity\EntityInterface $entity
3941
* The entity object.
42+
* @param Drupal\Core\StringTranslation\TranslationInterface $string_translation
43+
* The string translation object.
4044
*/
41-
public function __construct(EntityInterface $entity) {
45+
public function __construct(EntityInterface $entity, TranslationInterface $string_translation) {
4246
$this->entity = $entity;
47+
$this->stringTranslation = $string_translation;
4348
}
4449

4550
/**
@@ -59,8 +64,8 @@ public function formAlter(array &$form, FormStateInterface $form_state) {
5964

6065
$form['og']['og_enable_access'] = [
6166
'#type' => 'checkbox',
62-
'#title' => t('Restrict access to group members'),
63-
'#description' => t('Enable OG access control. Provides a new field that determines the group/group content visibility. Public groups can have member-only content. Any public group content belonging to a private group will be restricted to the members of that group only.'),
67+
'#title' => $this->t('Restrict access to group members'),
68+
'#description' => $this->t('Enable OG access control. Provides a new field that determines the group/group content visibility. Public groups can have member-only content. Any public group content belonging to a private group will be restricted to the members of that group only.'),
6469
'#default_value' => $this->bundle ? $this->hasAccessControl() : FALSE,
6570
'#states' => [
6671
'visible' => [
@@ -75,7 +80,7 @@ public function formAlter(array &$form, FormStateInterface $form_state) {
7580
* Checks whether the existing bundle has OG access control enabled.
7681
*
7782
* @return bool
78-
* True if the group bundle has the OG_ACCESS_FIELD field -OR-
83+
* True if the group bundle has the OgAccess::OG_ACCESS_FIELD field -OR-
7984
* if the group content bundle has the OG_CONTENT_ACCESS_FIELD field.
8085
* False otherwise.
8186
*/
@@ -84,11 +89,11 @@ protected function hasAccessControl() {
8489
->getFieldDefinitions($this->entityTypeId, $this->bundle);
8590

8691
if (Og::isGroup($this->entityTypeId, $this->bundle)) {
87-
return isset($field_definitions[OG_ACCESS_FIELD]);
92+
return isset($field_definitions[OgAccess::OG_ACCESS_FIELD]);
8893
}
8994

9095
if (Og::isGroupContent($this->entityTypeId, $this->bundle)) {
91-
return isset($field_definitions[OG_ACCESS_CONTENT_FIELD]);
96+
return isset($field_definitions[OgAccess::OG_ACCESS_CONTENT_FIELD]);
9297
}
9398

9499
return FALSE;

og_access/src/Plugin/OgFields/OgAccessField.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function getFieldStorageBaseDefinition(array $values = []) {
2424
'cardinality' => 1,
2525
'settings' => [
2626
'allowed_values' => [
27-
0 => 'Public - accessible to all site users',
28-
1 => 'Private - accessible only to group members',
27+
0 => $this->t('Public - accessible to all site users'),
28+
1 => $this->t('Private - accessible only to group members'),
2929
],
3030
'allowed_values_function' => '',
3131
],

og_access/src/Plugin/OgFields/OgContentAccessField.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Determine the group content visibility.
1010
*
1111
* @OgFields(
12-
* id = OG_ACCESS_CONTENT_FIELD,
12+
* id = OgAccess::OG_ACCESS_CONTENT_FIELD,
1313
* type = "node",
1414
* description = @Translation("Determine the group content visibility.")
1515
* )
@@ -24,8 +24,8 @@ public function getFieldStorageBaseDefinition(array $values = []) {
2424
'cardinality' => 1,
2525
'settings' => [
2626
'allowed_values' => [
27-
0 => 'Public - accessible to all site users',
28-
1 => 'Private - accessible only to group members',
27+
0 => $this->t('Public - accessible to all site users'),
28+
1 => $this->t('Private - accessible only to group members'),
2929
],
3030
'allowed_values_function' => '',
3131
],

0 commit comments

Comments
 (0)