-
Notifications
You must be signed in to change notification settings - Fork 16
cleanup #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 8.x-1.x
Are you sure you want to change the base?
cleanup #78
Changes from all commits
f4f6ac2
421a508
1850086
7fb873b
333daae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -47,9 +47,14 @@ class GroupManager { | |
| */ | ||
| public function __construct(ConfigFactoryInterface $config_factory) { | ||
| $this->configFactory = $config_factory; | ||
| $this->refreshGroupMap(); | ||
| } | ||
|
|
||
| protected function groupMap() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mhhh |
||
| if (!isset($this->groupMap)) { | ||
| $this->refreshGroupMap(); | ||
| } | ||
| return $this->groupMap; | ||
| } | ||
| /** | ||
| * Determines whether an entity type ID and bundle ID are group enabled. | ||
| * | ||
|
|
@@ -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(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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] : []; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
|
|
||
| /** | ||
|
|
||
| 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) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
|
||
| /** | ||
| /** | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = []; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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'); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops