diff --git a/dynamic_entity_reference.views.inc b/dynamic_entity_reference.views.inc index f4178cf..c83963a 100644 --- a/dynamic_entity_reference.views.inc +++ b/dynamic_entity_reference.views.inc @@ -15,18 +15,18 @@ function dynamic_entity_reference_field_views_data(FieldStorageConfigInterface $ $entity_manager = \Drupal::entityManager(); $labels = $entity_manager->getEntityTypeLabels(TRUE); $options = $labels['Content']; - // Add a relationship to the entity types. - if ($field_storage->getSetting('exclude_entity_types')) { - $entity_type_ids = array_diff(array_keys($options), $field_storage->getSetting('entity_type_ids') ?: array()); - } - else { - $entity_type_ids = array_intersect(array_keys($options), $field_storage->getSetting('entity_type_ids') ?: array()); - } foreach ($data as $table_name => $table_data) { - foreach ($entity_type_ids as $target_entity_type_id) { + $entity_type_ids = $field_storage->getSetting('entity_type_ids'); + if ($field_storage->getSetting('exclude_entity_types')) { + $target_entity_type_ids = array_diff_key($options, $entity_type_ids ?: array()); + } + else { + $target_entity_type_ids = array_intersect_key($options, $entity_type_ids ?: array()); + } + // Add a relationship to the target entity types. + foreach (array_keys($target_entity_type_ids) as $target_entity_type_id) { $target_entity_type = $entity_manager->getDefinition($target_entity_type_id); - $base_table = $target_entity_type->getBaseTable(); - + $target_base_table = $target_entity_type->getBaseTable(); // Provide a relationship for the entity type with the entity reference // field. $args = array( @@ -35,31 +35,33 @@ function dynamic_entity_reference_field_views_data(FieldStorageConfigInterface $ ); $data[$table_name][$field_storage->getName()]['relationship'] = array( 'id' => 'standard', - 'base' => $base_table, + 'base' => $target_base_table, 'entity type' => $target_entity_type_id, 'base field' => $target_entity_type->getKey('id'), 'relationship field' => $field_storage->getName() . '_target_id', 'title' => t('@label referenced from @field_name', $args), 'label' => t('@field_name: @label', $args), ); - // Provide a reverse relationship for the entity type that is referenced - // by the field. - $host_entity_type_id = $field_storage->getTargetEntityTypeId(); - $pseudo_field_name = 'reverse__' . $host_entity_type_id . '__' . $field_storage->getName(); + + // Provide a reverse relationship for the entity type that is referenced by + // the field. + $entity_type_id = $field_storage->getTargetEntityTypeId(); + $pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_storage->getName(); /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ - $table_mapping = $entity_manager->getStorage($host_entity_type_id)->getTableMapping(); - $data[$base_table][$pseudo_field_name]['relationship'] = array( + $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping(); + $data[$target_base_table][$pseudo_field_name]['relationship'] = array( 'title' => t('@label using @field_name', $args), 'help' => t('Relate each @label with a @field_name.', $args), 'id' => 'entity_reverse', 'field_name' => $field_storage->getName(), 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), 'field field' => $field_storage->getName() . '_target_id', - 'base' => $base_table, + 'base' => $target_base_table, 'base field' => $target_entity_type->getKey('id'), 'label' => t('@field_name', array('@field_name' => $field_storage->getName())), ); } } + return $data; } diff --git a/src/Tests/Views/DynamicEntityReferenceRelationshipTest.php b/src/Tests/Views/DynamicEntityReferenceRelationshipTest.php index 013c2ec..4316039 100644 --- a/src/Tests/Views/DynamicEntityReferenceRelationshipTest.php +++ b/src/Tests/Views/DynamicEntityReferenceRelationshipTest.php @@ -55,13 +55,13 @@ class DynamicEntityReferenceRelationshipTest extends ViewUnitTestBase { ViewTestData::createTestViews(get_class($this), array('dynamic_entity_reference_test_views')); $field_storage = FieldStorageConfig::create(array( + 'entity_type' => 'entity_test', + 'field_name' => 'field_test', + 'type' => 'dynamic_entity_reference', 'settings' => array( 'exclude_entity_types' => FALSE, 'entity_type_ids' => array('entity_test'), ), - 'entity_type' => 'entity_test', - 'name' => 'field_test', - 'type' => 'dynamic_entity_reference', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, )); $field_storage->save(); @@ -115,9 +115,6 @@ class DynamicEntityReferenceRelationshipTest extends ViewUnitTestBase { // Check an actual test view. $view = Views::getView('test_dynamic_entity_reference_view'); - if (!$view) { - $this->assertFalse(TRUE, 'view is empty.'); - } $this->executeView($view); foreach (array_keys($view->result) as $index) { @@ -136,9 +133,6 @@ class DynamicEntityReferenceRelationshipTest extends ViewUnitTestBase { } $view->destroy(); - if (!$view) { - $this->assertFalse(TRUE, 'This view is empty.'); - } $this->executeView($view, 'embed_1'); foreach (array_keys($view->result) as $index) {