diff --git a/modules/redhen_connection/redhen_connection.services.yml b/modules/redhen_connection/redhen_connection.services.yml index 3df5c7d..4b7f4fb 100644 --- a/modules/redhen_connection/redhen_connection.services.yml +++ b/modules/redhen_connection/redhen_connection.services.yml @@ -2,4 +2,6 @@ services: redhen_connection.connections: class: Drupal\redhen_connection\ConnectionService arguments: ['@entity_type.manager', '@entity.query', '@database'] - + redhen_connection.views_data: + class: \Drupal\redhen_connection\ViewsData + arguments: ['@entity_type.manager', '@redhen_connection.connections'] diff --git a/modules/redhen_connection/src/ConnectionService.php b/modules/redhen_connection/src/ConnectionService.php index 6241b8b..6b3b193 100644 --- a/modules/redhen_connection/src/ConnectionService.php +++ b/modules/redhen_connection/src/ConnectionService.php @@ -6,6 +6,7 @@ use Drupal\Core\Access\AccessResultAllowed; use Drupal\Core\Database\Connection as DBConnection; use Drupal\Core\Entity\Entity; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Entity\Query\QueryInterface; @@ -61,6 +62,39 @@ class ConnectionService implements ConnectionServiceInterface { /** * {@inheritdoc} */ + public function getConnectionEntityTypes(array $entity_types) { + $all_connection_types = []; + foreach ($entity_types as $entity_type => $type) { + $query = $this->entityQuery->get('redhen_connection_type'); + $or_group = $query->orConditionGroup(); + + $or_group->condition('endpoints.1.entity_type', $entity_type); + $or_group->condition('endpoints.2.entity_type', $entity_type); + + $query->condition($or_group); + $results = $query->execute(); + + $connection_types = []; + if (!empty($results)) { + $connection_types = ConnectionType::loadMultiple($results); + $all_connection_types = array_merge($all_connection_types, $connection_types); + } + } + + $connected_entities = []; + foreach ($all_connection_types as $connection_type_id => $connection_type) { + $endpoint_1 = $connection_type->get('endpoints')[1]['entity_type']; + $endpoint_2 = $connection_type->get('endpoints')[2]['entity_type']; + $connected_entities[$connection_type_id]['endpoint_1'][$endpoint_1] = $entity_types[$endpoint_1]; + $connected_entities[$connection_type_id]['endpoint_2'][$endpoint_2] = $entity_types[$endpoint_2]; + } + + return $connected_entities; + } + + /** + * {@inheritdoc} + */ public function getConnectionTypes(EntityInterface $entity, EntityInterface $entity2 = NULL) { $query = $this->entityQuery->get('redhen_connection_type'); $or_group = $query->orConditionGroup(); diff --git a/modules/redhen_connection/src/ConnectionServiceInterface.php b/modules/redhen_connection/src/ConnectionServiceInterface.php index 1464879..20ddeff 100644 --- a/modules/redhen_connection/src/ConnectionServiceInterface.php +++ b/modules/redhen_connection/src/ConnectionServiceInterface.php @@ -11,6 +11,16 @@ use Drupal\Core\Session\AccountInterface; */ interface ConnectionServiceInterface { + /** + * Filters an entity list to just bundle definitions for entities with connection types. + * + * @param EntityTypeInterface[] $entity_types + * The master entity type list filter. + * + * @return ConfigEntityTypeInterface[] + * An array of only the config entities we want to modify. + */ + public function getConnectionEntityTypes(array $entity_types); /** * Returns the connection types that can be connected to a single entity or two @@ -106,4 +116,4 @@ interface ConnectionServiceInterface { */ public function checkConnectionPermission(EntityInterface $entity, $operation, AccountInterface $account = NULL); -} \ No newline at end of file +} diff --git a/modules/redhen_connection/src/Entity/ConnectionViewsData.php b/modules/redhen_connection/src/Entity/ConnectionViewsData.php index 340da21..25192ca 100644 --- a/modules/redhen_connection/src/Entity/ConnectionViewsData.php +++ b/modules/redhen_connection/src/Entity/ConnectionViewsData.php @@ -17,41 +17,9 @@ class ConnectionViewsData extends EntityViewsData implements EntityViewsDataInte // Unset the default entity relationships. // It does not work properly, the target type it is not defined. - unset($data['redhen_connection']['entity1']['relationship']); - unset($data['redhen_connection']['entity2']['relationship']); - - // Collect all connectionable entity types. - $connection_types = ConnectionType::loadMultiple(); - $entity_type_ids = []; - /** @var \Drupal\redhen_connection\ConnectionTypeInterface $connection_type */ - foreach ($connection_types as $connection_type) { - if ($entity_type_id = $connection_type->getEndpointEntityTypeId('1')) { - $entity_type_ids[] = $entity_type_id; - } - if ($entity_type_id = $connection_type->getEndpointEntityTypeId('2')) { - $entity_type_ids[] = $entity_type_id; - } - } - $entity_type_ids = array_unique($entity_type_ids); - - // Provide a relationship for each entity type found. - foreach ($entity_type_ids as $entity_type_id) { - /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */ - $entity_type = $this->entityManager->getDefinition($entity_type_id); - $data['redhen_connection'][$entity_type_id] = [ - 'relationship' => [ - 'title' => $entity_type->getLabel(), - 'help' => t('The related @entity_type.', ['@entity_type' => $entity_type->getLowercaseLabel()]), - 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(), - 'base field' => $entity_type->getKey('id'), - 'relationship field' => 'related_entity', - 'id' => 'standard', - 'label' => $entity_type->getLabel(), - ], - ]; - } + unset($data['redhen_connection']['endpoint_1']['relationship']); + unset($data['redhen_connection']['endpoint_2']['relationship']); return $data; } - }