commit 9c3454dafefb98be951bb5e707618e73788684fa Author: Jibran Ijaz Date: Sat Sep 12 04:18:29 2015 +0500 45 diff --git a/dynamic_entity_reference.views.inc b/dynamic_entity_reference.views.inc index ffdf0bd..08133ba 100644 --- a/dynamic_entity_reference.views.inc +++ b/dynamic_entity_reference.views.inc @@ -153,7 +153,9 @@ function dynamic_entity_reference_views_data() { // Unlimited (-1) or > 1 store field data in a dedicated table. $table = $field->getCardinality() == 1 ? $base_table : $entity_type->getBaseTable() . '__' . $field_name; - // @todo mention the reason here. + // Filter out non SQL entity types because \Drupal\views\EntityViewsData + // class only allows entities with + // \Drupal\Core\Entity\Sql\SqlEntityStorageInterface. $targets = array_intersect(DynamicEntityReferenceItem::getTargetTypes($field->getSettings()), array_keys($sql_entity_types)); foreach ($targets as $target_entity_type_id) { $target_entity_type = $entity_types[$target_entity_type_id]; @@ -191,7 +193,7 @@ function dynamic_entity_reference_views_data() { // Reverse Relationship (Target -> Origin) $psuedo_field = 'reverse__' . $entity_type_id . '__'. $field_name; - $relationship = [ + $data[$target_table][$psuedo_field]['relationship'] = [ 'title' => t('Reverse reference to @field_name @type on @origin_label', $t_args), 'label' => t('Reverse reference to @field_name @type on @origin_label', $t_args), 'group' => $target_entity_type->getLabel(), @@ -201,7 +203,7 @@ function dynamic_entity_reference_views_data() { // are same because field column(s) exist in entity base table therefore // we can't use entity_reverse relationship plugin. if ($field->getCardinality() == 1) { - $data[$target_table][$psuedo_field]['relationship'] = [ + $data[$target_table][$psuedo_field]['relationship'] += [ 'id' => 'standard', 'base' => $table, 'entity type' => $entity_type_id, @@ -222,7 +224,7 @@ function dynamic_entity_reference_views_data() { // 'field table' are not same because field column(s) exist in separate // table therefore we have to use entity_reverse relationship plugin. else { - $data[$target_table][$psuedo_field]['relationship'] = [ + $data[$target_table][$psuedo_field]['relationship'] += [ 'id' => 'entity_reverse', 'base' => $base_table, 'entity_type' => $entity_type_id, @@ -241,7 +243,6 @@ function dynamic_entity_reference_views_data() { ], ]; } - $data[$target_table][$psuedo_field]['relationship'] += $relationship; } } } diff --git a/src/Tests/Views/DynamicEntityReferenceBaseFieldRelationshipTest.php b/src/Tests/Views/DynamicEntityReferenceBaseFieldRelationshipTest.php index 9af6bf0..b0cbe09 100644 --- a/src/Tests/Views/DynamicEntityReferenceBaseFieldRelationshipTest.php +++ b/src/Tests/Views/DynamicEntityReferenceBaseFieldRelationshipTest.php @@ -33,14 +33,14 @@ class DynamicEntityReferenceBaseFieldRelationshipTest extends ViewKernelTestBase * * @var array */ - public static $modules = array('user', 'field', 'entity_test', 'entity_reference', 'dynamic_entity_reference', 'dynamic_entity_reference_entity_test'); + public static $modules = ['user', 'field', 'entity_test', 'entity_reference', 'dynamic_entity_reference']; /** * The entity_test entities used by the test. * * @var array */ - protected $entities = array(); + protected $entities = []; /** * Counts how many test method has run. @@ -54,12 +54,17 @@ class DynamicEntityReferenceBaseFieldRelationshipTest extends ViewKernelTestBase */ protected function setUp() { parent::setUp(); - \Drupal::state()->set('dynamic_entity_reference_entity_test', static::$testRun++); + $this->enableModules(['dynamic_entity_reference_entity_test']); + $this->installConfig(['dynamic_entity_reference_entity_test']); + \Drupal::configFactory() + ->getEditable('dynamic_entity_reference_entity_test.settings') + ->set('test_count', static::$testRun++) + ->save(); $this->installEntitySchema('user'); $this->installEntitySchema('entity_test'); $this->installEntitySchema('entity_test_mul'); static::$testViews = static::$testRun == 1 ? ['test_dynamic_entity_reference_entity_test_view', 'test_dynamic_entity_reference_entity_test_mul_view'] : ['test_dynamic_entity_reference_mul_entity_test_view', 'test_dynamic_entity_reference_mul_entity_test_mul_view']; - ViewTestData::createTestViews(get_class($this), array('dynamic_entity_reference_entity_test')); + ViewTestData::createTestViews(get_class($this), ['dynamic_entity_reference_entity_test']); } /** @@ -109,7 +114,8 @@ class DynamicEntityReferenceBaseFieldRelationshipTest extends ViewKernelTestBase $this->assertEqual($views_data_entity_test['entity_test__dynamic_references']['relationship']['relationship field'], 'dynamic_references__target_id'); $this->assertEqual($views_data_entity_test['entity_test__dynamic_references']['relationship']['extra'][0]['left_field'], 'dynamic_references__target_type'); $this->assertEqual($views_data_entity_test['entity_test__dynamic_references']['relationship']['extra'][0]['value'], 'entity_test'); - // Check views data for test entity - data table referenced from dynamic_references. + // Check views data for test entity - data table referenced from + // dynamic_references. $this->assertEqual($views_data_entity_test['entity_test_mul__dynamic_references']['relationship']['id'], 'standard'); $this->assertEqual($views_data_entity_test['entity_test_mul__dynamic_references']['relationship']['base'], 'entity_test_mul_property_data'); $this->assertEqual($views_data_entity_test['entity_test_mul__dynamic_references']['relationship']['entity type'], 'entity_test_mul'); @@ -125,7 +131,8 @@ class DynamicEntityReferenceBaseFieldRelationshipTest extends ViewKernelTestBase $this->assertEqual($views_data_entity_test['reverse__entity_test__dynamic_references']['relationship']['field'], 'id'); $this->assertEqual($views_data_entity_test['reverse__entity_test__dynamic_references']['relationship']['extra'][0]['field'], 'dynamic_references__target_type'); $this->assertEqual($views_data_entity_test['reverse__entity_test__dynamic_references']['relationship']['extra'][0]['value'], 'entity_test'); - // Check the backwards reference for test entity - data table using dynamic_references. + // Check the backwards reference for test entity - data table using + // dynamic_references. $this->assertEqual($views_data_entity_test['reverse__entity_test_mul__dynamic_references']['relationship']['id'], 'standard'); $this->assertEqual($views_data_entity_test['reverse__entity_test_mul__dynamic_references']['relationship']['base'], 'entity_test_mul_property_data'); $this->assertEqual($views_data_entity_test['reverse__entity_test_mul__dynamic_references']['relationship']['entity type'], 'entity_test_mul'); @@ -145,7 +152,8 @@ class DynamicEntityReferenceBaseFieldRelationshipTest extends ViewKernelTestBase $this->assertEqual($views_data_entity_test_mul['entity_test__dynamic_references']['relationship']['relationship field'], 'dynamic_references__target_id'); $this->assertEqual($views_data_entity_test_mul['entity_test__dynamic_references']['relationship']['extra'][0]['left_field'], 'dynamic_references__target_type'); $this->assertEqual($views_data_entity_test_mul['entity_test__dynamic_references']['relationship']['extra'][0]['value'], 'entity_test'); - // Check views data for test entity - data table referenced from dynamic_references. + // Check views data for test entity - data table referenced from + // dynamic_references. $this->assertEqual($views_data_entity_test_mul['entity_test_mul__dynamic_references']['relationship']['id'], 'standard'); $this->assertEqual($views_data_entity_test_mul['entity_test_mul__dynamic_references']['relationship']['base'], 'entity_test_mul_property_data'); $this->assertEqual($views_data_entity_test_mul['entity_test_mul__dynamic_references']['relationship']['entity type'], 'entity_test_mul'); @@ -161,7 +169,8 @@ class DynamicEntityReferenceBaseFieldRelationshipTest extends ViewKernelTestBase $this->assertEqual($views_data_entity_test_mul['reverse__entity_test__dynamic_references']['relationship']['field'], 'id'); $this->assertEqual($views_data_entity_test_mul['reverse__entity_test__dynamic_references']['relationship']['extra'][0]['field'], 'dynamic_references__target_type'); $this->assertEqual($views_data_entity_test_mul['reverse__entity_test__dynamic_references']['relationship']['extra'][0]['value'], 'entity_test_mul'); - // Check the backwards reference for test entity - data table using dynamic_references. + // Check the backwards reference for test entity - data table using + // dynamic_references. $this->assertEqual($views_data_entity_test_mul['reverse__entity_test_mul__dynamic_references']['relationship']['id'], 'standard'); $this->assertEqual($views_data_entity_test_mul['reverse__entity_test_mul__dynamic_references']['relationship']['base'], 'entity_test_mul_property_data'); $this->assertEqual($views_data_entity_test_mul['reverse__entity_test_mul__dynamic_references']['relationship']['entity type'], 'entity_test_mul'); @@ -421,7 +430,8 @@ class DynamicEntityReferenceBaseFieldRelationshipTest extends ViewKernelTestBase $this->assertEqual($views_data_entity_test['entity_test__dynamic_references']['relationship']['relationship field'], 'dynamic_references_target_id'); $this->assertEqual($views_data_entity_test['entity_test__dynamic_references']['relationship']['extra'][0]['left_field'], 'dynamic_references_target_type'); $this->assertEqual($views_data_entity_test['entity_test__dynamic_references']['relationship']['extra'][0]['value'], 'entity_test'); - // Check views data for test entity - data table referenced from dynamic_references. + // Check views data for test entity - data table referenced from + // dynamic_references. $this->assertEqual($views_data_entity_test['entity_test_mul__dynamic_references']['relationship']['id'], 'standard'); $this->assertEqual($views_data_entity_test['entity_test_mul__dynamic_references']['relationship']['base'], 'entity_test_mul_property_data'); $this->assertEqual($views_data_entity_test['entity_test_mul__dynamic_references']['relationship']['entity type'], 'entity_test_mul'); @@ -447,7 +457,8 @@ class DynamicEntityReferenceBaseFieldRelationshipTest extends ViewKernelTestBase // Check the backwards reference for test entity using field_test. $views_data_entity_test = Views::viewsData()->get('entity_test_mul_property_data'); - // Check the backwards reference for test entity - data table using dynamic_references. + // Check the backwards reference for test entity - data table using + // dynamic_references. $this->assertEqual($views_data_entity_test['reverse__entity_test__dynamic_references']['relationship']['id'], 'entity_reverse'); $this->assertEqual($views_data_entity_test['reverse__entity_test__dynamic_references']['relationship']['base'], 'entity_test'); $this->assertEqual($views_data_entity_test['reverse__entity_test__dynamic_references']['relationship']['entity_type'], 'entity_test'); @@ -469,7 +480,8 @@ class DynamicEntityReferenceBaseFieldRelationshipTest extends ViewKernelTestBase $this->assertEqual($views_data_entity_test_mul['entity_test__dynamic_references']['relationship']['relationship field'], 'dynamic_references_target_id'); $this->assertEqual($views_data_entity_test_mul['entity_test__dynamic_references']['relationship']['extra'][0]['left_field'], 'dynamic_references_target_type'); $this->assertEqual($views_data_entity_test_mul['entity_test__dynamic_references']['relationship']['extra'][0]['value'], 'entity_test'); - // Check views data for test entity - data table referenced from dynamic_references. + // Check views data for test entity - data table referenced from + // dynamic_references. $this->assertEqual($views_data_entity_test_mul['entity_test_mul__dynamic_references']['relationship']['id'], 'standard'); $this->assertEqual($views_data_entity_test_mul['entity_test_mul__dynamic_references']['relationship']['base'], 'entity_test_mul_property_data'); $this->assertEqual($views_data_entity_test_mul['entity_test_mul__dynamic_references']['relationship']['entity type'], 'entity_test_mul'); @@ -495,7 +507,8 @@ class DynamicEntityReferenceBaseFieldRelationshipTest extends ViewKernelTestBase // Check the backwards reference for test entity using field_test. $views_data_entity_test_mul = Views::viewsData()->get('entity_test_mul_property_data'); - // Check the backwards reference for test entity - data table using dynamic_references. + // Check the backwards reference for test entity - data table using + // dynamic_references. $this->assertEqual($views_data_entity_test_mul['reverse__entity_test_mul__dynamic_references']['relationship']['id'], 'entity_reverse'); $this->assertEqual($views_data_entity_test_mul['reverse__entity_test_mul__dynamic_references']['relationship']['base'], 'entity_test_mul_property_data'); $this->assertEqual($views_data_entity_test_mul['reverse__entity_test_mul__dynamic_references']['relationship']['entity_type'], 'entity_test_mul'); diff --git a/tests/modules/dynamic_entity_reference_entity_test/config/install/dynamic_entity_reference_entity_test.settings.yml b/tests/modules/dynamic_entity_reference_entity_test/config/install/dynamic_entity_reference_entity_test.settings.yml new file mode 100644 index 0000000..540c98d --- /dev/null +++ b/tests/modules/dynamic_entity_reference_entity_test/config/install/dynamic_entity_reference_entity_test.settings.yml @@ -0,0 +1,3 @@ +# Settings of dynamic_entity_reference_entity_test + +test_count: 0 diff --git a/tests/modules/dynamic_entity_reference_entity_test/config/schema/dynamic_entity_reference_entity_test.schema.yml b/tests/modules/dynamic_entity_reference_entity_test/config/schema/dynamic_entity_reference_entity_test.schema.yml new file mode 100644 index 0000000..e6b085a --- /dev/null +++ b/tests/modules/dynamic_entity_reference_entity_test/config/schema/dynamic_entity_reference_entity_test.schema.yml @@ -0,0 +1,9 @@ +# Schema for the configuration files of the views field view module. + +dynamic_entity_reference_entity_test.settings: + type: config_entity + label: 'dynamic_entity_reference_entity_test settings' + mapping: + test_count: + type: integer + label: 'Count the test runs' diff --git a/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module b/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module index aef361c..48e09a7 100644 --- a/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module +++ b/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module @@ -49,8 +49,7 @@ function dynamic_entity_reference_entity_test_entity_base_field_info(EntityTypeI ->setDisplayConfigurable('form', TRUE) ->setDisplayConfigurable('view', TRUE); - // @todo use \Drupal::state()->get('dynamic_entity_reference_entity_test', 0); here. - if (DynamicEntityReferenceBaseFieldRelationshipTest::$testRun) { + if (\Drupal::config('dynamic_entity_reference_entity_test.settings')->get('test_count')) { $fields['dynamic_references'] ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); } @@ -68,8 +67,8 @@ function dynamic_entity_reference_entity_test_views_data() { // function adds a temporary fix for that remove this when is fixed // https://www.drupal.org/node/2477899. $data = []; - // @todo use \Drupal::state()->get('dynamic_entity_reference_entity_test', 0); here. - if (DynamicEntityReferenceBaseFieldRelationshipTest::$testRun) { + + if (\Drupal::config('dynamic_entity_reference_entity_test.settings')->get('test_count')) { $data['entity_test__dynamic_references'] = [ 'table' => [ 'join' => [