diff --git a/src/Entity/Storage/GroupContentStorage.php b/src/Entity/Storage/GroupContentStorage.php index 25bdbd7..ef9d856 100644 --- a/src/Entity/Storage/GroupContentStorage.php +++ b/src/Entity/Storage/GroupContentStorage.php @@ -99,10 +99,14 @@ class GroupContentStorage extends SqlContentEntityStorage implements GroupConten // Statically cache all group content IDs for the group content types. if (!empty($group_content_types)) { - $this->loadByEntityCache[$entity->getEntityTypeId()][$entity->id()] = $this->getQuery() - ->condition('type', array_keys($group_content_types), 'IN') - ->condition('entity_id', $entity->id()) - ->execute(); + // Use an optimized plain query to avoid the overhead of entity and SQL + // query builders. + $query = "SELECT id from {{$this->dataTable}} WHERE entity_id = :entity_id AND type IN (:types[])"; + $this->loadByEntityCache[$entity->getEntityTypeId()][$entity->id()] = $this->database->query($query, [ + ':entity_id' => $entity->id(), + ':types[]' => array_keys($group_content_types) + ]) + ->fetchCol(); } // If no responsible group content types were found, we return nothing. else { diff --git a/src/Entity/Storage/GroupContentTypeStorage.php b/src/Entity/Storage/GroupContentTypeStorage.php index 7818abe..34e71de 100644 --- a/src/Entity/Storage/GroupContentTypeStorage.php +++ b/src/Entity/Storage/GroupContentTypeStorage.php @@ -27,6 +27,13 @@ class GroupContentTypeStorage extends ConfigEntityStorage implements GroupConten */ protected $pluginManager; + /** + * Statically caches loaded group content types by target entity type ID. + * + * @var \Drupal\group\Entity\GroupContentTypeInterface[][] + */ + protected $byEntityTypeCache = []; + /** * Constructs a GroupContentTypeStorage object. * @@ -81,7 +88,11 @@ class GroupContentTypeStorage extends ConfigEntityStorage implements GroupConten */ public function loadByEntityTypeId($entity_type_id) { $plugin_ids = []; - + + if (isset($this->byEntityTypeCache[$entity_type_id])) { + return $this->byEntityTypeCache[$entity_type_id]; + } + /** @var \Drupal\group\Plugin\GroupContentEnablerInterface $plugin */ foreach ($this->pluginManager->getAll() as $plugin_id => $plugin) { if ($plugin->getEntityTypeId() === $entity_type_id) { @@ -91,11 +102,13 @@ class GroupContentTypeStorage extends ConfigEntityStorage implements GroupConten // If no responsible group content plugins were found, we return nothing. if (empty($plugin_ids)) { + $this->byEntityTypeCache[$entity_type_id] = []; return []; } // Otherwise load all group content types being handled by gathered plugins. - return $this->loadByContentPluginId($plugin_ids); + $this->byEntityTypeCache[$entity_type_id] = $this->loadByContentPluginId($plugin_ids); + return $this->byEntityTypeCache[$entity_type_id]; } /** @@ -118,8 +131,17 @@ class GroupContentTypeStorage extends ConfigEntityStorage implements GroupConten 'content_plugin' => $plugin_id, 'plugin_config' => $plugin->getConfiguration(), ]; - + return $this->create($values); } + /** + * {@inheritdoc} + */ + public function resetCache(array $ids = NULL) { + parent::resetCache($ids); + // Reset the static cache if any gropu content type entity changes. + $this->byEntityTypeCache = []; + } + } diff --git a/tests/src/Kernel/GroupTypeCreateTest.php b/tests/src/Kernel/GroupTypeCreateTest.php index 3a296ca..2c475ae 100644 --- a/tests/src/Kernel/GroupTypeCreateTest.php +++ b/tests/src/Kernel/GroupTypeCreateTest.php @@ -18,6 +18,12 @@ class GroupTypeCreateTest extends GroupKernelTestBase { * @covers ::postSave */ public function testCreate() { + + /** @var \Drupal\group\Entity\Storage\GroupContentTypeStorageInterface $group_content_type_storage */ + $group_content_type_storage = $this->entityTypeManager + ->getStorage('group_content_type'); + $this->assertCount(2, $group_content_type_storage->loadByEntityTypeId('user')); + // Check that the group type was created and saved properly. /** @var \Drupal\group\Entity\GroupTypeInterface $group_type */ $group_type = $this->entityTypeManager @@ -49,9 +55,9 @@ class GroupTypeCreateTest extends GroupKernelTestBase { $plugin_config = ['group_type_id' => 'dummy', 'id' => 'group_membership']; $plugin = $this->pluginManager->createInstance('group_membership', $plugin_config); - $group_content_type = $this->entityTypeManager - ->getStorage('group_content_type') + $group_content_type = $group_content_type_storage ->load($plugin->getContentTypeConfigId()); + $this->assertCount(3, $group_content_type_storage->loadByEntityTypeId('user')); $this->assertNotNull($group_content_type, 'Enforced plugins were installed on the group type.'); }