Skip to content

Commit

Permalink
Merge branch 'hotfix/Dependecy-injection-#546' into DI-Gizra#546
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrodej authored Feb 7, 2020
2 parents b49ee96 + 2beb2c3 commit 2eb8af3
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 75 deletions.
15 changes: 10 additions & 5 deletions og.module
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,23 @@ function og_entity_create_access(AccountInterface $account, array $context, $bun
$required = FALSE;

$field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle);
foreach ($field_definitions as $field_name => $field_definition) {
foreach ($field_definitions as $field_definition) {
/** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
if (!\Drupal::service('og.group_audience_helper')->isGroupAudienceField($field_definition)) {
continue;
}

$handler = Og::getSelectionHandler($field_definition);
$options = [
'target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'),
'handler' => $field_definition->getSetting('handler'),
'field_mode' => 'admin',
];
/** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager $handler */
$handler = \Drupal::service('plugin.manager.entity_reference_selection');

if ($handler->getReferenceableEntities()) {
if ($handler->createInstance('og:default', $options)) {
return AccessResult::neutral();
}

// Allow users to create content outside of groups, if none of the
// audience fields is required.
$required = $field_definition->isRequired();
Expand Down Expand Up @@ -359,7 +364,7 @@ function og_invalidate_group_content_cache_tags(EntityInterface $entity) {
}
}

foreach ($membership_manager->getGroups($entity) as $group_entity_type_id => $groups) {
foreach ($membership_manager->getGroups($entity) as $groups) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $group */
foreach ($groups as $group) {
$tags = Cache::mergeTags($tags, $group->getCacheTagsToInvalidate());
Expand Down
4 changes: 0 additions & 4 deletions phpcs-ruleset.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
<rule ref="./vendor/drupal/coder/coder_sniffer/DrupalPractice">
<!-- https://github.com/Gizra/og/issues/549 -->
<exclude name="DrupalPractice.CodeAnalysis.VariableAnalysis.UndefinedVariable" />
<!-- https://github.com/Gizra/og/issues/545 -->
<exclude name="DrupalPractice.CodeAnalysis.VariableAnalysis.UnusedVariable" />
<!-- https://github.com/Gizra/og/issues/548 -->
<exclude name="DrupalPractice.Commenting.ExpectedException.TagFound" />
<!-- https://github.com/Gizra/og/issues/550 -->
<exclude name="DrupalPractice.Constants.GlobalDefine.GlobalConstant" />
<!-- https://github.com/Gizra/og/issues/544 -->
Expand Down
4 changes: 2 additions & 2 deletions src/Cache/Context/OgRoleCacheContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public function getContext() {
// Sort the memberships, so that the same key can be generated, even if
// the memberships were defined in a different order.
ksort($memberships);
foreach ($memberships as $entity_type_id => &$groups) {
foreach ($memberships as &$groups) {
ksort($groups);
foreach ($groups as $group_id => &$role_names) {
foreach ($groups as &$role_names) {
sort($role_names);
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/ContextProvider/OgContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ protected function getOgContext() {
*/
protected function getBestCandidate() {
$collection = new OgResolvedGroupCollection();
$plugins = [];

// Retrieve the list of group resolvers. These are stored in config, and are
// ordered by priority.
Expand All @@ -135,8 +134,6 @@ protected function getBestCandidate() {
foreach ($group_resolvers as $plugin_id) {
/** @var \Drupal\og\OgGroupResolverInterface $plugin */
if ($plugin = $this->pluginManager->createInstance($plugin_id)) {
$plugins[$plugin_id] = $plugin;

// Set the default vote weight according to the plugin's priority.
$collection->setVoteWeight($priority);

Expand Down
2 changes: 1 addition & 1 deletion src/Event/DefaultRoleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function setRole(OgRole $role) {
* {@inheritdoc}
*/
public function setRoles(array $roles) {
foreach ($roles as $name => $properties) {
foreach ($roles as $properties) {
$this->setRole($properties);
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/GroupTypeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ class GroupTypeManager implements GroupTypeManagerInterface {
*/
protected $groupAudienceHelper;

/**
* The Entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* Constructs a GroupTypeManager object.
*
Expand All @@ -155,6 +162,7 @@ class GroupTypeManager implements GroupTypeManagerInterface {
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EventDispatcherInterface $event_dispatcher, CacheBackendInterface $cache, PermissionManagerInterface $permission_manager, OgRoleManagerInterface $og_role_manager, RouteBuilderInterface $route_builder, OgGroupAudienceHelperInterface $group_audience_helper) {
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->eventDispatcher = $event_dispatcher;
$this->cache = $cache;
Expand Down Expand Up @@ -200,7 +208,7 @@ public function getAllGroupBundles($entity_type = NULL) {
*/
public function getAllGroupContentBundleIds() {
$bundles = [];
foreach ($this->getGroupRelationMap() as $group_entity_type_id => $group_bundle_ids) {
foreach ($this->getGroupRelationMap() as $group_bundle_ids) {
foreach ($group_bundle_ids as $group_content_entity_type_ids) {
foreach ($group_content_entity_type_ids as $group_content_entity_type_id => $group_content_bundle_ids) {
$bundles[$group_content_entity_type_id] = array_merge(isset($bundles[$group_content_entity_type_id]) ? $bundles[$group_content_entity_type_id] : [], $group_content_bundle_ids);
Expand Down Expand Up @@ -377,7 +385,7 @@ protected function populateGroupRelationMap(): void {

$this->groupRelationMap = [];

$user_bundles = \Drupal::entityTypeManager()->getDefinition('user')->getKey('bundle') ?: ['user'];
$user_bundles = $this->entityTypeManager->getDefinition('user')->getKey('bundle') ?: ['user'];

foreach ($this->entityTypeBundleInfo->getAllBundleInfo() as $group_content_entity_type_id => $bundles) {
foreach ($bundles as $group_content_bundle_id => $bundle_info) {
Expand Down
4 changes: 2 additions & 2 deletions src/MembershipManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ public function getGroupMembershipsByRoleNames(EntityInterface $group, array $ro
/**
* {@inheritdoc}
*/
public function createMembership(EntityInterface $group, AccountInterface $user, $membership_type = OgMembershipInterface::TYPE_DEFAULT) {
/** @var \Drupal\user\UserInterface|\Drupal\Core\Session\AccountInterface $user */
public function createMembership(EntityInterface $group, UserInterface $user, $membership_type = OgMembershipInterface::TYPE_DEFAULT) {
/** @var \Drupal\user\UserInterface $user */
/** @var \Drupal\og\OgMembershipInterface $membership */
$membership = OgMembership::create(['type' => $membership_type]);
$membership
Expand Down
6 changes: 3 additions & 3 deletions src/MembershipManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Drupal\og;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\UserInterface;

/**
* Membership manager interface.
Expand Down Expand Up @@ -169,7 +169,7 @@ public function getGroupMembershipsByRoleNames(EntityInterface $group, array $ro
*
* @param \Drupal\Core\Entity\EntityInterface $group
* The group entity.
* @param \Drupal\Core\Session\AccountInterface $user
* @param \Drupal\user\UserInterface $user
* The user object.
* @param string $membership_type
* (optional) The membership type. Defaults to
Expand All @@ -178,7 +178,7 @@ public function getGroupMembershipsByRoleNames(EntityInterface $group, array $ro
* @return \Drupal\og\OgMembershipInterface
* The unsaved membership object.
*/
public function createMembership(EntityInterface $group, AccountInterface $user, $membership_type = OgMembershipInterface::TYPE_DEFAULT);
public function createMembership(EntityInterface $group, UserInterface $user, $membership_type = OgMembershipInterface::TYPE_DEFAULT);

/**
* Returns all group IDs associated with the given group content entity.
Expand Down
19 changes: 18 additions & 1 deletion src/Og.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,9 @@ public static function invalidateCache() {
protected static function getFieldBaseDefinition($plugin_id) {
/** @var OgFieldsPluginManager $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.og.fields');
if (!$field_config = $plugin_manager->getDefinition($plugin_id)) {

$field_config = $plugin_manager->getDefinition($plugin_id);
if (!$field_config) {
throw new \Exception("The Organic Groups field with plugin ID $plugin_id is not a valid plugin.");
}

Expand All @@ -384,8 +386,23 @@ protected static function getFieldBaseDefinition($plugin_id) {
* @throws \Exception
* Thrown when the passed in field definition is not of a group audience
* field.
*
* @deprecated in og:8.x-1.0-alpha4 and is removed from og:8.x-1.0-alpha5.
* Use
* \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager::getInstance()
* Instead.
* @codingStandardsIgnoreStart
* @see https://github.com/Gizra/og/issues/580
* @codingStandardsIgnoreEnd
*/
public static function getSelectionHandler(FieldDefinitionInterface $field_definition, array $options = []) {
// @codingStandardsIgnoreStart
@trigger_error('Og:getSelectionHandler() is deprecated in og:8.x-1.0-alpha4
and is removed from og:8.x-1.0-alpha5.
Use \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager::getInstance()
instead. See https://github.com/Gizra/og/issues/580', E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
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.");
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/EntityReferenceSelection/OgSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS')
$ids = [];
if (!empty($this->getConfiguration()['field_mode']) && $this->getConfiguration()['field_mode'] === 'admin') {
// Don't include the groups, the user doesn't have create permission.
foreach ($user_groups as $delta => $group) {
foreach ($user_groups as $group) {
$ids[] = $group->id();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/src/Kernel/Access/AccessByOgMembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\Tests\og\Kernel\Access;

use Drupal\Core\Entity\EntityStorageException;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\og\Traits\OgMembershipCreationTrait;
Expand Down Expand Up @@ -182,8 +183,6 @@ public function testEntityOperationAccess($user, array $expected_results) {

/**
* Tests exception is thrown when trying to save non-member role.
*
* @expectedException \Drupal\Core\Entity\EntityStorageException
*/
public function testNonMemberRoleMembershipSave() {
/** @var \Drupal\og\Entity\OgRole $role */
Expand All @@ -194,6 +193,7 @@ public function testNonMemberRoleMembershipSave() {
->save();

$membership = OgMembership::create();
$this->expectException(EntityStorageException::class);
$membership
->setOwner($this->users['non-member'])
->setGroup($this->group)
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Kernel/Entity/GroupMembershipManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public function testGetGroups($group_type_id, $group_bundle, array $expected) {
/** @var \Drupal\Core\Entity\EntityInterface $expected_group */
$expected_group = $this->groups[$expected_type][$expected_key];
/** @var \Drupal\Core\Entity\EntityInterface $group */
foreach ($result[$expected_type] as $key => $group) {
foreach ($result[$expected_type] as $group) {
if ($group->getEntityTypeId() === $expected_group->getEntityTypeId() && $group->id() === $expected_group->id()) {
// The expected result was found. Continue the test.
continue 2;
Expand Down
Loading

0 comments on commit 2eb8af3

Please sign in to comment.