Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Remove OG complex widget #181

Closed
wants to merge 154 commits into from
Closed
Show file tree
Hide file tree
Changes from 152 commits
Commits
Show all changes
154 commits
Select commit Hold shift + click to select a range
a658b0e
Start removing complex widget
amitaibu Sep 15, 2016
bce0c5e
Add core widgets
amitaibu Sep 15, 2016
bad2c09
Allow privileged user to create content
amitaibu Sep 15, 2016
c75b654
Check permissions
amitaibu Sep 15, 2016
d49ac2b
Start fixing tests
amitaibu Sep 15, 2016
1d061ee
Fix logic and tests
amitaibu Sep 15, 2016
d8591fb
Sniffer fixes
amitaibu Sep 15, 2016
97fb566
Fix return value in docs
amitaibu Sep 16, 2016
77eb562
Re-add Functional test
amitaibu Sep 19, 2016
9bb346e
Sniffer fixes
amitaibu Sep 19, 2016
ce13e41
Sniffer fixes
amitaibu Sep 19, 2016
d317614
Merge branch '8.x-1.x' into 160-remove-complex-widget
amitaibu Sep 20, 2016
0e10e5f
Setting up the base for the new test.
Sep 30, 2016
793715e
Commit more work.
Sep 30, 2016
3d4013f
First version of the tests.
Sep 30, 2016
9c4858b
Make code sniffer happy.
Sep 30, 2016
5346d09
Merge 8.x-1.x
amitaibu Oct 4, 2016
1551922
Remove un-needed method
amitaibu Oct 4, 2016
3f69920
Remove unused statement
amitaibu Oct 4, 2016
ca3bc5d
Start expanding the widget tests
amitaibu Oct 15, 2016
3e3c90d
Create node types
amitaibu Oct 15, 2016
78a427e
Comments cleanup
amitaibu Oct 15, 2016
11f4bd2
Remove traits
amitaibu Oct 15, 2016
8c94013
Try to find cause of bug
amitaibu Oct 15, 2016
e98354c
Remove unusued code
amitaibu Oct 15, 2016
1508540
Remove custom settings
amitaibu Oct 15, 2016
afd3f71
Remove unused use
amitaibu Oct 15, 2016
586e677
Add todo
amitaibu Oct 15, 2016
c5fdf87
Filter out the bundles that are not groups.
amitaibu Oct 15, 2016
328c913
Fix indentation
amitaibu Oct 15, 2016
68a9abd
Improve exception error
amitaibu Oct 15, 2016
a117c7b
Improve names
amitaibu Oct 15, 2016
0940d99
Remove unusued code
amitaibu Oct 15, 2016
521b508
Remove custom settings
amitaibu Oct 15, 2016
ef4af0d
Filter out the bundles that are not groups.
amitaibu Oct 15, 2016
d5d34d6
Fix indentation
amitaibu Oct 15, 2016
7334e9c
Sniffer fixes
amitaibu Oct 18, 2016
9ac5241
Convert test to Functional
amitaibu Oct 18, 2016
71e903a
Add comment
amitaibu Oct 18, 2016
00525d9
Debug failing tests
amitaibu Oct 18, 2016
a60a300
Revert debug
amitaibu Oct 18, 2016
cb75f16
Fix wrong node type name
amitaibu Oct 18, 2016
f7b0d29
Simplify and debug test
amitaibu Oct 18, 2016
43f8acd
Add status 200
amitaibu Oct 18, 2016
cf30f3a
gst
amitaibu Oct 18, 2016
94db0c1
Quicker debugs
amitaibu Oct 18, 2016
a2bdbab
Check 404 page
amitaibu Oct 18, 2016
e0c2655
Rebuild routes
amitaibu Oct 18, 2016
ba47934
Fields page
amitaibu Oct 18, 2016
4d65046
click link
amitaibu Oct 18, 2016
2104177
Back to drupalGet
amitaibu Oct 18, 2016
4f47cc0
Check access
amitaibu Oct 18, 2016
a988745
Enable field ui
amitaibu Oct 18, 2016
470e6ef
Allow debug
amitaibu Oct 18, 2016
5c939ec
Import URL
amitaibu Oct 18, 2016
0fcf95e
Debug user access
amitaibu Oct 18, 2016
81f1b28
Cleanup
amitaibu Oct 18, 2016
f33eb3d
Admin user
amitaibu Oct 18, 2016
7762e4f
Back to permissions
amitaibu Oct 18, 2016
3a1929b
Remove use
amitaibu Oct 19, 2016
8f241b2
Permissions
amitaibu Oct 19, 2016
1b18142
Core test
amitaibu Oct 19, 2016
1d5b92d
Run core
amitaibu Oct 19, 2016
ef4d2db
Debug core
amitaibu Oct 19, 2016
8dc9aa1
Remove core debug
amitaibu Oct 19, 2016
d817fc4
Start conveting test to Kernel
amitaibu Oct 19, 2016
d64af00
More work
amitaibu Oct 19, 2016
e2e82b9
Use entity test with bundle
amitaibu Oct 19, 2016
ee625d3
Merge field settings
amitaibu Oct 19, 2016
2fdf2bd
Fix test
amitaibu Oct 19, 2016
a21eb83
Sniffer fixes
amitaibu Oct 19, 2016
85849ae
Remove debug
amitaibu Oct 19, 2016
a1cc01e
Create Node type correctly
amitaibu Oct 19, 2016
7a18d5c
Add unpublished node
amitaibu Oct 19, 2016
780b54a
Fix OgSelectionWidgetTest
amitaibu Oct 19, 2016
352551d
Validate that the user have access to post a group content.
Nov 25, 2016
7fafaef
Adding skeleton for a test [skip ci]
Nov 26, 2016
093324c
Commit work [skip ci]
Nov 26, 2016
8aa95f5
Fix a typo and continue working on the test [skip ci]
Nov 27, 2016
24a1221
Set the role properly [skip ci]
Dec 3, 2016
6bdaa20
Use forbidden and not allowed.
Apr 6, 2017
b174e23
Checkin when forbidden.
Apr 8, 2017
4bb4819
Somehow fix the tests.
Apr 9, 2017
0741340
Getting the root entity.
Apr 11, 2017
ae94377
Merge branch '8.x-1.x' into 160-remove-complex-widget
Apr 11, 2017
12838fb
Merge branch '8.x-1.x' into 160-remove-complex-widget
Apr 11, 2017
8ab5433
Fix conflict
Apr 12, 2017
55b3fcf
Stbalize tests.
Apr 12, 2017
a2271be
Minor refactor.
Apr 12, 2017
7744bc3
Wrong operator.
Apr 12, 2017
9f96bc4
Trying to understand what’s the difference [skip ci]
Apr 13, 2017
7ecdedd
Find another solution.
Apr 13, 2017
86dee21
Adding basic constraint to add entities [skip ci]
Apr 20, 2017
f20e279
Commit logic for now.
Apr 20, 2017
3134064
Don’t add constant to all the entities, just check the field.
Apr 20, 2017
8abc283
Fix tests.
Apr 21, 2017
8c7cadb
Remove the testin file
Sep 26, 2017
564682f
Fix name.
Sep 27, 2017
a2d92a2
Fix cs
Sep 27, 2017
74e677d
Get the text.
Sep 27, 2017
2c5af11
Maybe now?
Sep 28, 2017
df17662
Catching and not version.
Sep 28, 2017
779efee
CS fix.
Sep 28, 2017
1450634
Handle another case for the fields.
Sep 28, 2017
961eb7d
Set the field as required in specific cases.
Sep 29, 2017
0e030db
More changes.
Sep 29, 2017
15debda
Adding the validation.
Sep 30, 2017
f43add7
The validation works as expected. Now, we need to write tests.
Sep 30, 2017
2257ece
More improvements.
Sep 30, 2017
35b019f
Adding the boilerplate for the new test.
Sep 30, 2017
12cc927
Done with the tests.
Sep 30, 2017
365da2f
Minor fixes.
Oct 4, 2017
f3894cd
Make CS happy.
Oct 4, 2017
16ff320
make cs happy.
Oct 4, 2017
b7e2d5e
Merge remote-tracking branch 'origin/8.x-1.x' into 160-remove-complex…
pfrenssen Jul 3, 2018
b7ac85b
Fix grammar in comments.
mariano-dagostino Jul 3, 2018
3493f91
Improve comment of access check logic
mariano-dagostino Jul 3, 2018
3439183
GroupTypeManager::getAllGroupBundles should respect GroupTypeManagerI…
mariano-dagostino Jul 3, 2018
fd83a5d
Merge branch '8.x-1.x' into 160-remove-complex-widget
RoySegall Jul 8, 2018
c304aea
Use single quotes
MPParsley May 12, 2019
0625119
Undo single quotes
MPParsley May 12, 2019
5fa4b7a
Improve readability foreach
MPParsley May 12, 2019
352ba22
Updated documentation for the entity reference field
MPParsley May 12, 2019
57dfc44
Use invalidMessage from constraint
MPParsley May 12, 2019
fab2fe6
Added dependency injection to ValidOgMembershipMultipleReferenceConst…
MPParsley May 12, 2019
3d3365a
Merge remote-tracking branch 'gizra/8.x-1.x' into 160-remove-complex-…
pfrenssen May 12, 2019
2faaad5
Fix tests
MPParsley May 12, 2019
214f5cd
Fixed unnecessarily gendered language in a comment
MPParsley May 12, 2019
9c94943
Cleanup call to MembershipManager::reset()
MPParsley May 12, 2019
84a85fc
Protected properties should start with a small cap
MPParsley May 12, 2019
c194f0e
Protected properties should start with a small cap.
MPParsley May 12, 2019
e5b3b60
Update src/Plugin/Validation/Constraint/ValidOgMembershipMultipleRefe…
MPParsley May 12, 2019
fde09bc
Revert commit
MPParsley May 12, 2019
86301b0
Deprecate Drupal\simpletest\ContentTypeCreationTrait
MPParsley May 12, 2019
5622d85
Deprecated Drupal\simpletest\NodeCreationTrait
MPParsley May 12, 2019
85d87ae
Fixed namespaces
MPParsley May 12, 2019
e9e2152
Removed deprecated call to Drupal::entityManager
MPParsley May 12, 2019
991ea33
Removed deprecated entity.query service
MPParsley May 12, 2019
d6ceee6
Removed calls to deprecated entityManager
MPParsley May 12, 2019
7e58f33
Use EntityTypeManagerInterface
MPParsley May 12, 2019
ffd5afe
Remove call to deprecated entity.manager
MPParsley May 12, 2019
412f880
Fixed EntityManager deprecations in OgSelection
MPParsley May 12, 2019
aa82d18
Fixed call to undefined function getBundleInfo() on EntityTypeManager
MPParsley May 12, 2019
34ffca4
Added getQuery
MPParsley May 12, 2019
bdc92db
Clear membership cache
MPParsley May 12, 2019
417d548
Restore cache reset
MPParsley May 12, 2019
0f87dbe
Test with cache delete.
MPParsley May 12, 2019
c980ace
Cleanup test
MPParsley May 12, 2019
38a25a7
Fixed indentation
MPParsley May 13, 2019
d0b227f
Added missing use statement
MPParsley May 13, 2019
6f6a733
Fix tests
MPParsley May 13, 2019
fd95fdb
Revert permission format
MPParsley May 20, 2019
0439c0e
Revert permission format
MPParsley May 20, 2019
eff1923
Revert permission format
MPParsley May 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions config/schema/og.schema.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
field.storage_settings.og_membership_reference:
type: mapping
label: 'Organic Groups reference field storage settings'
mapping:
target_type:
type: string
label: 'Type of entity to reference'

field.field_settings.og_membership_reference:
type: mapping
label: 'Organic Groups reference field settings'
mapping:
handler:
type: string
label: 'Reference method'
handler_settings:
type: entity_reference_selection.[%parent.handler]
label: 'Organic Groups reference selection plugin settings'
access_override:
type: boolean
label: 'Access Override'

field.storage_settings.og_standard_reference:
type: mapping
label: 'Organic Groups reference field storage settings'
Expand All @@ -38,9 +16,6 @@ field.field_settings.og_standard_reference:
handler_settings:
type: entity_reference_selection.[%parent.handler]
label: 'Organic Groups reference selection plugin settings'
access_override:
type: boolean
label: 'Access Override'

entity_reference_selection.og:default:
type: entity_reference_selection.default
Expand Down Expand Up @@ -136,20 +111,6 @@ og.og_role.*:
type: string
label: 'Role type'

field.widget.settings.og_complex:
type: mapping
label: 'OG Group Audience field widget'
mapping:
match_operator:
type: string
label: 'Autocomplete matching'
size:
type: integer
label: 'Size of textfield'
placeholder:
type: label
label: 'Placeholder'

views.field.og_membership_bulk_form:
type: views_field_bulk_form
label: 'OG Membership bulk form'
Expand Down
70 changes: 65 additions & 5 deletions og.module
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function og_entity_delete(EntityInterface $entity) {
* Implements hook_entity_access().
*/
function og_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {

// Grant access to view roles, so that they can be shown in listings.
if ($entity instanceof OgRoleInterface && $operation == 'view') {
return AccessResult::allowed();
Expand Down Expand Up @@ -159,6 +160,21 @@ function og_entity_access(EntityInterface $entity, $operation, AccountInterface
/** @var \Drupal\Core\Access\AccessResult $access */
$access = \Drupal::service('og.access')->userAccessEntity($operation, $entity, $account);

$audience_fields = \Drupal::service('og.group_audience_helper')->getAllGroupAudienceFields($entity_type_id, $bundle_id);
if (count($audience_fields) === 1) {
$field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle_id);
foreach ($field_definitions as $field_name => $field_definition) {

/** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
if (!in_array($field_definition->getName(), array_keys($audience_fields))) {
continue;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no documentation for this change, but from reading the code it seems that it is doing the following:

If there is only one audience field, and the user has access to perform an operation on it, then the field will be required.

If this code is intended to make a field required or not it does not belong in hook_entity_access(). This hook is intended for returning a boolean to indicate whether the user has access. It is not a good idea to hijjack it to make changes to fields. This should be moved to a more relevant location.

On top of the code being in the wrong place, we should add some documentation to it. From reading the code it is unclear why this is needed. Why does the field need to become required when the user has access? Why is this only needed when there is 1 single field, why not when there are multiple fields?

The reasoning behind this is explained by the test OgSelectionWidgetOptionsTest::testNonRequiredAudienceField():

    // The user can't post group content site wide so the audience is required
    // field.
    // todo: is that correct?
    // $this->assertSession()->optionExists('Groups audience', '_none');.
    $this->assertSession()->optionExists('Groups audience', $this->group1->label());
    $this->assertSession()->optionNotExists('Groups audience', $this->group2->label());

So apparently there are certain cases where a user is not allowed to post site wide content, and then the group audience field should become required. Let's document this clearly, and also move this code out of hook_entity_access().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pfrenssen, I would suggest a custom constant or would you prefer a form_alter?


// Check if the field needs to be required.
$field_definition->setRequired($access);
}
}

if ($access->isAllowed()) {
return $access;
}
Expand All @@ -179,6 +195,11 @@ function og_entity_access(EntityInterface $entity, $operation, AccountInterface
function og_entity_create_access(AccountInterface $account, array $context, $bundle) {
$entity_type_id = $context['entity_type_id'];

if (!empty($context['skip_og_permission'])) {
// We've already been here or we want to skip the access check.
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would try to see if it is possible to find a solution that avoids adding this section. Since this hook is dealing with checking access it is important for security reasons and introducing a way to skip the checks might have unintended consequences.

It is also not allowed to return NULL here according to the entity API, we should always return an AccessResult.

}

if (!Og::isGroupContent($entity_type_id, $bundle)) {
// Not a group content.
return AccessResult::neutral();
Expand All @@ -203,17 +224,32 @@ function og_entity_create_access(AccountInterface $account, array $context, $bun
// @see \Drupal\og\Plugin\EntityReferenceSelection\OgSelection::buildEntityQuery()
$required = FALSE;

/** @var \Drupal\og\OgGroupAudienceHelperInterface $audience_helper */
$audience_helper = \Drupal::service('og.group_audience_helper');
$audience_fields = $audience_helper->getAllGroupAudienceFields($entity_type_id, $bundle);
$field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle);

foreach ($field_definitions as $field_name => $field_definition) {
/** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
if (!\Drupal::service('og.group_audience_helper')->isGroupAudienceField($field_definition)) {
if (!in_array($field_definition->getName(), array_keys($audience_fields))) {
continue;
}

$handler = Og::getSelectionHandler($field_definition);
if (count($audience_fields) === 1) {
// Set the field to required only if we have a single audience field.
// If we have multiple fields then the require logic will perform at the
// entity constraint level.
$field_definition->setRequired(!\Drupal::entityTypeManager()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the change made to hook_entity_access(), this should not be done here. hook_entity_create_access() is intended to inform the entity system about whether a user has access to create an entity, it should not be hijjacked to introduce changes to field definitions.

->getAccessControlHandler($entity_type_id)
->createAccess($bundle, $account, ['skip_og_permission' => TRUE]));
}

if ($handler->getReferenceableEntities()) {
return AccessResult::neutral();
$handler = Og::getSelectionHandler($field_definition);
if ($handler->countReferenceableEntities()) {
// At least one of the fields has referenceable groups. That mean that
// the user can create group content when referencing the group content in
// at least one of the fields.
return AccessResult::allowed();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user gets access based on OG, and doesn't have site wide permission to create content, we need to set the audience field to be required.

Things become more complicated when we might have multiple audience fields :/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could form_alter() and if no group was selected, fail the form.

Copy link
Member Author

@amitaibu amitaibu Oct 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two ideas for simplification, that we need to think about:

  1. Allow having only a single audience field (back to D6 days 😄 )
  2. Once a bundle has an audience field, that field is required. So you can't for example have a post outside of a group context.

(1) Is removing some advanced features, which I have honestly never seen used, like creating different memberships based on the field group was selected from. Or allowing to add a group content to groups of different entity types.
(2) Which is less important might be used for example for adding articles on groups and articles for the site-wide. I can think of easy workflows to keep that (e.g. create a "site-wide" group)

/cc @pfrenssen @RoySegall

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about it more. (1) sounds like going back to D6, but in a bad way. (2) sounds ok, but we won't gain much.

In short, I think we should keep those features in.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RoySegall is there a test to validate:

If the user gets access based on OG, and doesn't have site wide permission to create content, we need to set the audience field to be required.

}

// Allow users to create content outside of groups, if none of the
Expand Down Expand Up @@ -284,7 +320,16 @@ function og_field_formatter_info_alter(array &$info) {
* Implements hook_field_widget_info_alter().
*/
function og_field_widget_info_alter(array &$info) {
$info['options_buttons']['field_types'][] = OgGroupAudienceHelperInterface::GROUP_REFERENCE;
$field_types = [
'entity_reference_autocomplete',
'entity_reference_autocomplete_tags',
'options_buttons',
'options_select',
];

foreach ($field_types as $field_type) {
$info[$field_type]['field_types'][] = OgGroupAudienceHelperInterface::GROUP_REFERENCE;
}
}

/**
Expand All @@ -297,9 +342,24 @@ function og_field_widget_info_alter(array &$info) {
* be rebuilt via RouteBuilder::setRebuildNeeded.
*/
function og_entity_type_alter(array &$entity_types) {

/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
foreach ($entity_types as $entity_type_id => $entity_type) {

// Add link template to groups. We add it to all the entity types, and later
// on return the correct access, depending if the bundle is indeed a group
// and accessible. We do not filter here the entity type by groups, so
// whenever GroupTypeManager::addGroup is called, it's enough to mark route
// to be rebuilt via RouteBuilder::setRebuildNeeded.
$entity_type->setLinkTemplate('og-admin-routes', "/group/$entity_type_id/{{$entity_type_id}}/admin");

// We won't check if an entity might be group content because we need to
// check if that entity has any bundle that could be a group entity and this
// would cause a recursion. The constraint will check there if the entity is
// group content or not.
if ($entity_type->entityClassImplements(ContentEntityInterface::class)) {
$entity_type->addConstraint('ValidOgMembershipMultipleReference');
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/Entity/OgMembership.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,12 @@ public function preSave(EntityStorageInterface $storage) {
}
}

$uid = $this->get('uid')->target_id;

// Check for an existing membership.
$query = \Drupal::entityQuery('og_membership');
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
$query
->condition('uid', $this->get('uid')->target_id)
->condition('uid', $uid)
->condition('entity_id', $this->get('entity_id')->value)
->condition('entity_type', $this->get('entity_type')->value);

Expand All @@ -468,7 +470,7 @@ public function preSave(EntityStorageInterface $storage) {
->execute();

if ($count) {
throw new \LogicException(sprintf('An OG membership already exists for uid %s in group of entity-type %s and ID: %s', $this->get('uid')->target_id, $entity_type_id, $this->getGroup()->id()));
throw new \LogicException(sprintf('An OG membership already exists for user ID %d and group of entity type %s and ID %s', $uid, $entity_type_id, $this->getGroup()->id()));
}

parent::preSave($storage);
Expand All @@ -482,7 +484,6 @@ public function save() {

// Reset internal cache.
Og::reset();
\Drupal::service('og.access')->reset();
MPParsley marked this conversation as resolved.
Show resolved Hide resolved

return $result;
}
Expand Down
16 changes: 15 additions & 1 deletion src/GroupTypeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,22 @@ public function getGroupBundleIdsByEntityType($entity_type_id) {
* {@inheritdoc}
*/
public function getAllGroupBundles($entity_type = NULL) {
// Todo - should be remove since this method don't do any thing.
return $this->getGroupMap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's undo this change, it's out of scope for this issue and it causes a fatal error because now the signature is different from the one defined in GroupTypeManagerInterface.

There is already an issue to remove this method: #372, let's do it in there.

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make sense to me, why have a method that doesn't do anything but wrap another method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, should be removed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add todo for that one.


/**
* Get group bundles of an entity type.
*
* @param string $entity_type_id
* The entity type ID.
*
* @return array
* An associative array of bundle IDs, or an empty array if none found.
*/
public function getGroupBundlesByEntityType($entity_type_id) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this method, it exactly duplicates the code from GroupTypeManager::getGroupsForEntityType().

$group_map = $this->getGroupMap();
return !empty($group_map[$entity_type]) ? $group_map[$entity_type] : $group_map;
return isset($group_map[$entity_type_id]) ? $group_map[$entity_type_id] : [];
}

/**
Expand Down
30 changes: 18 additions & 12 deletions src/Og.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ protected static function getFieldBaseDefinition($plugin_id) {
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
* @param array $options
* Overriding the default options of the selection handler.
*
* @return \Drupal\og\Plugin\EntityReferenceSelection\OgSelection
* Returns the OG selection handler.
Expand All @@ -384,22 +382,28 @@ protected static function getFieldBaseDefinition($plugin_id) {
* Thrown when the passed in field definition is not of a group audience
* field.
*/
public static function getSelectionHandler(FieldDefinitionInterface $field_definition, array $options = []) {
public static function getSelectionHandler(FieldDefinitionInterface $field_definition) {
if (!\Drupal::service('og.group_audience_helper')->isGroupAudienceField($field_definition)) {
$field_name = $field_definition->getName();
throw new \Exception("The field $field_name is not an audience field.");
throw new \Exception("The field $field_name is not a group audience field.");
}

$options = NestedArray::mergeDeep([
$entity_type_id = $field_definition->getTargetEntityTypeId();
$definition = \Drupal::entityTypeManager()->getDefinition($entity_type_id);

$values = [];
if ($bundle_key = $definition->getKey('bundle')) {
$values[$bundle_key] = $field_definition->getTargetBundle();
}

$entity = \Drupal::entityTypeManager()->getStorage($entity_type_id)->create($values);

$options = [
'target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'),
'handler' => $field_definition->getSetting('handler'),
'handler_settings' => [
'field_mode' => 'default',
],
], $options);

// Deep merge the handler settings.
$options['handler_settings'] = NestedArray::mergeDeep($field_definition->getSetting('handler_settings'), $options['handler_settings']);
'handler_settings' => $field_definition->getSetting('handler_settings'),
'entity' => $entity,
];

return \Drupal::service('plugin.manager.entity_reference_selection')->createInstance('og:default', $options);
}
Expand All @@ -409,6 +413,8 @@ public static function getSelectionHandler(FieldDefinitionInterface $field_defin
*/
public static function reset() {
static::$cache = [];

\Drupal::service('og.access')->reset();
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
}

}
Loading