Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 5 additions & 2 deletions og.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ services:
plugin.manager.og.fields:
class: Drupal\og\OgFieldsPluginManager
parent: default_plugin_manager
og.group.manager:
class: Drupal\og\GroupManager
og.group_map:
class: Drupal\og\GroupMap
arguments: ['@config.factory']
og.permissions:
class: Drupal\og\OgPermissionHandler
arguments: ['@module_handler', '@string_translation', '@controller_resolver']
og.group_repository:
cllass: Drupal\og\GroupRepository

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

whoops

arguments: ['@entity_field.manager', '@entity.query']
18 changes: 13 additions & 5 deletions src/GroupManager.php → src/GroupMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* A manager to keep track of which entity type/bundles are OG group enabled.
*/
class GroupManager {
class GroupMap {

/**
* The OG settings configuration key.
Expand Down Expand Up @@ -47,9 +47,14 @@ class GroupManager {
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
$this->refreshGroupMap();
}

protected function groupMap() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Mhhh GroupMap::groupMap let's have a better name.

if (!isset($this->groupMap)) {
$this->refreshGroupMap();
}
return $this->groupMap;
}
/**
* Determines whether an entity type ID and bundle ID are group enabled.
*
Expand All @@ -59,7 +64,8 @@ public function __construct(ConfigFactoryInterface $config_factory) {
* @return bool
*/
public function isGroup($entity_type_id, $bundle) {
return isset($this->groupMap[$entity_type_id]) && in_array($bundle, $this->groupMap[$entity_type_id]);
$group_map = $this->groupMap();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We had this like this originally, but removed it. It can come back though.

return isset($group_map[$entity_type_id]) && in_array($bundle, $group_map[$entity_type_id]);
}

/**
Expand All @@ -68,7 +74,8 @@ public function isGroup($entity_type_id, $bundle) {
* @return array
*/
public function getGroupsForEntityType($entity_type_id) {
return isset($this->groupMap[$entity_type_id]) ? $this->groupMap[$entity_type_id] : [];
$group_map = $this->groupMap();
return isset($group_map[$entity_type_id]) ? $group_map[$entity_type_id] : [];
}

/**
Expand All @@ -77,7 +84,8 @@ public function getGroupsForEntityType($entity_type_id) {
* @return array
*/
public function getAllGroupBundles($entity_type = NULL) {
return !empty($this->groupMap[$entity_type]) ? $this->groupMap[$entity_type] : $this->groupMap;
$group_map = $this->groupMap;
return !empty($group_map[$entity_type]) ? $group_map[$entity_type] : $group_map;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

While your changing these can you fix the logic, for me that will lead to some WTF's. Only return all if the $entity_type is empty, and rename that to $entity_type_id.

}

/**
Expand Down
139 changes: 139 additions & 0 deletions src/GroupRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

/**
* @file
* Contains \Drupal\og\GroupRepository.
*/

namespace Drupal\og;

use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Field\FieldDefinitionInterface;

class GroupRepository {

/**
* @var array
*/
protected $entityGroupCache = [];

/**
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
protected $queryFactory;

/**
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;

/**
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

public function __construct(EntityFieldManagerInterface $entity_field_manager, QueryFactory $query_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->entityFieldManager = $entity_field_manager;
$this->queryFactory = $query_factory;
$this->entityTypeManager = $entity_type_manager;
}

public function getAllGroupAudienceFields($entity_type_id, $bundle, $group_type_id = NULL, $group_bundle = NULL) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This seems like it doesn't really belong on here.

$filter = function (FieldDefinitionInterface $field_definition) use ($group_type_id, $group_bundle) {
if (!Og::isGroupAudienceField($field_definition)) {
// Not a group audience field.
return FALSE;
}
$target_type = $field_definition->getFieldStorageDefinition()
->getSetting('target_type');

if (isset($group_type_id) && $target_type != $group_type_id) {
// Field doesn't reference this group type.
return FALSE;
}

$handler_settings = $field_definition->getSetting('handler_settings');
if (isset($group_bundle) && !empty($handler_settings['target_bundles']) && !in_array($group_bundle, $handler_settings['target_bundles'])) {
return FALSE;
}
return TRUE;
};
return array_filter($this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle), $filter);
}

/**
/**

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

typo

* Gets the groups an entity is associated with.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to get groups for.
* @param $states
* (optional) Array with the state to return. Defaults to active.
* @param $field_name
* (optional) The field name associated with the group.
*
* @return array
* An array with the group's entity type as the key, and array - keyed by
* the OG membership ID and the group ID as the value. If nothing found,
* then an empty array.
*/
public function getEntityGroups(EntityInterface $entity, array $states = [OG_STATE_ACTIVE], $field_name = NULL) {
$entity_type_id = $entity->getEntityTypeId();
$entity_id = $entity->id();

// Get a string identifier of the states, so we can retrieve it from cache.
if ($states) {
sort($states);
$state_identifier = implode(':', $states);
}
else {
$state_identifier = FALSE;
}

$identifier = [
$entity_type_id,
$entity_id,
$state_identifier,
$field_name,
];

$identifier = implode(':', $identifier);
if (isset($this->entityGroupCache[$identifier])) {
// Return cached values.
return $this->entityGroupCache[$identifier];
}

$this->entityGroupCache[$identifier] = [];
$query = $this->queryFactory->get('og_membership')
->condition('entity_type', $entity_type_id)
->condition('etid', $entity_id);

if ($states) {
$query->condition('state', $states, 'IN');
}

if ($field_name) {
$query->condition('field_name', $field_name);
}

$results = $query->execute();

/** @var \Drupal\og\Entity\OgMembership[] $memberships */
$memberships = $this->entityTypeManager
->getStorage('og_membership')
->loadMultiple($results);

/** @var \Drupal\og\Entity\OgMembership $membership */
foreach ($memberships as $membership) {
$this->entityGroupCache[$identifier][$membership->getGroupType()][$membership->id()] = $membership->getGroup();
}
return $this->entityGroupCache[$identifier];
}

public function resetCache() {
$this->entityGroupCache = [];
}
}
96 changes: 15 additions & 81 deletions src/Og.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,57 +116,7 @@ public static function createField($plugin_id, $entity_type, $bundle, array $set
* then an empty array.
*/
public static function getEntityGroups(EntityInterface $entity, array $states = [OG_STATE_ACTIVE], $field_name = NULL) {
$entity_type_id = $entity->getEntityTypeId();
$entity_id = $entity->id();

// Get a string identifier of the states, so we can retrieve it from cache.
if ($states) {
sort($states);
$state_identifier = implode(':', $states);
}
else {
$state_identifier = FALSE;
}

$identifier = [
$entity_type_id,
$entity_id,
$state_identifier,
$field_name,
];

$identifier = implode(':', $identifier);
if (isset(static::$entityGroupCache[$identifier])) {
// Return cached values.
return static::$entityGroupCache[$identifier];
}

static::$entityGroupCache[$identifier] = [];
$query = \Drupal::entityQuery('og_membership')
->condition('entity_type', $entity_type_id)
->condition('etid', $entity_id);

if ($states) {
$query->condition('state', $states, 'IN');
}

if ($field_name) {
$query->condition('field_name', $field_name);
}

$results = $query->execute();

/** @var \Drupal\og\Entity\OgMembership[] $memberships */
$memberships = \Drupal::entityTypeManager()
->getStorage('og_membership')
->loadMultiple($results);

/** @var \Drupal\og\Entity\OgMembership $membership */
foreach ($memberships as $membership) {
static::$entityGroupCache[$identifier][$membership->getGroupType()][$membership->id()] = $membership->getGroup();
}

return static::$entityGroupCache[$identifier];
return static::groupRepository()->getEntityGroups($entity, $states, $field_name);
}

/**
Expand Down Expand Up @@ -287,42 +237,27 @@ public static function isGroupAudienceField(FieldDefinitionInterface $field_defi
* none found.
*/
public static function getAllGroupAudienceFields($entity_type_id, $bundle, $group_type_id = NULL, $group_bundle = NULL) {
$return = [];

foreach (\Drupal::entityManager()->getFieldDefinitions($entity_type_id, $bundle) as $field_definition) {
if (!static::isGroupAudienceField($field_definition)) {
// Not a group audience field.
continue;
}

$target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');

if (isset($group_type_id) && $target_type != $group_type_id) {
// Field doesn't reference this group type.
continue;
}

$handler_settings = $field_definition->getSetting('handler_settings');

if (isset($group_bundle) && !empty($handler_settings['target_bundles']) && !in_array($group_bundle, $handler_settings['target_bundles'])) {
continue;
}

$field_name = $field_definition->getName();
$return[$field_name] = $field_definition;
}

return $return;
return static::groupRepository()->getAllGroupAudienceFields($entity_type_id, $bundle, $group_type_id, $group_bundle);
}

/**
* Returns the group manager instance.
*
* @return \Drupal\og\GroupManager
* @return \Drupal\og\GroupMap
*/
public static function groupManager() {
// @todo store static reference for this?
return \Drupal::service('og.group.manager');
return \Drupal::service('og.group_manager');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

map?

}

/**
* Returns the group repository instance.
*
* @return \Drupal\og\GroupRepository
*/
public static function groupRepository() {
// @todo store static reference for this?
return \Drupal::service('og.group_repository');
}

/**
Expand Down Expand Up @@ -359,8 +294,7 @@ public static function invalidateCache($group_ids = array()) {
drupal_static_reset($cache);
}

// @todo Consider using a reset() method.
static::$entityGroupCache = [];
static::groupRepository()->resetCache();

// Invalidate the entity property cache.
\Drupal::entityManager()->clearCachedDefinitions();
Expand Down
6 changes: 3 additions & 3 deletions tests/src/Unit/GroupManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Drupal\Tests\og\Unit;

use Drupal\og\GroupManager;
use Drupal\og\GroupMap;
use Drupal\Tests\UnitTestCase;

/**
Expand Down Expand Up @@ -214,14 +214,14 @@ public function testRemoveGroup() {
/**
* Creates a group manager instance with a mock config factory.
*
* @return \Drupal\og\GroupManager
* @return \Drupal\og\GroupMap
*/
protected function createGroupManager() {
$this->configFactoryProphecy->get('og.settings')
->willReturn($this->configProphecy->reveal())
->shouldBeCalled();

return new GroupManager($this->configFactoryProphecy->reveal());
return new GroupMap($this->configFactoryProphecy->reveal());
}

}
Loading