diff --git a/core/modules/system/system.views.inc b/core/modules/system/system.views.inc new file mode 100644 index 0000000..fc2824c --- /dev/null +++ b/core/modules/system/system.views.inc @@ -0,0 +1,16 @@ + $entity_info) { + if (isset($entity_info['views_controller_class'])) { + $views_controller = views_entity_get_views_controller($entity_type); + $data += $views_controller->viewsData(); + } + } + + return $data; +} diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTest.php index 3c948bd..59f9ccc 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Plugin/Core/Entity/EntityTest.php @@ -25,6 +25,7 @@ * translation_controller_class = "Drupal\entity_test\EntityTestTranslationController", * base_table = "entity_test", * data_table = "entity_test_property_data", + * views_controller_class = "\Drupal\views\EntityViewsController", * fieldable = TRUE, * entity_keys = { * "id" = "id", diff --git a/core/modules/views/lib/Drupal/views/EntityViewsController.php b/core/modules/views/lib/Drupal/views/EntityViewsController.php new file mode 100644 index 0000000..347b6ad --- /dev/null +++ b/core/modules/views/lib/Drupal/views/EntityViewsController.php @@ -0,0 +1,108 @@ +entityType = $entity_type; + $this->entityInfo = entity_get_info($entity_type); + $this->storageController = entity_get_controller($entity_type); + } + + /** + * Implements EntityViewsControllerInterface::viewsData(). + */ + public function viewsData() { + $data = array(); + $base_table = $this->entityInfo['base_table']; + $base_field = $this->entityInfo['entity_keys']['id']; + $data_table = $this->entityInfo['data_table']; + + // Setup base information of the views data. + $data[$base_table]['table']['entity_type'] = $this->entityType; + $data[$base_table]['table']['group'] = $this->entityInfo['label']; + $data[$base_table]['table']['base'] = array( + 'field' => $base_field, + 'title' => $this->entityInfo['label'], + ); + + // Setup relations to the revisions/property data. + // @todo Add revisions/property data as currently just base table properties + // are supported. + $data[$data_table]['table']['join'][$base_table] = array( + 'left_field' => $base_field, + 'field' => $base_field + ); + + // Load all typed data definitions of all fields. + $field_definitions = $this->storageController->getFieldDefinitions(array('entity type' => $this->entityType)); + + foreach ($this->entityInfo['schema_fields_sql']['base_table'] as $field) { + $typed_data = $field_definitions[$field]; + + $views_field = &$data[$base_table][$field]; + // @todo Is translating the right way to handle label/description? + $views_field['title'] = t($typed_data['label']); + $views_field['help'] = t($typed_data['description']); + // @todo Find a proper find the mappings between typed data and views + // handlers. Maybe the data types should define it with fallback to + // standard or views should have the same naming scheme. + dsm($typed_data); + switch ($typed_data['type']) { + case 'integer_field': + $views_field['field']['id'] = 'numeric'; + $views_field['argument']['id'] = 'numeric'; + $views_field['filter']['id'] = 'numeric'; + $views_field['sort']['id'] = 'standard'; + break; + case 'string_field': + $views_field['field']['id'] = 'standard'; + $views_field['argument']['id'] = 'string'; + $views_field['filter']['id'] = 'string'; + $views_field['sort']['id'] = 'standard'; + break; + case 'language_field': + $views_field['field']['id'] = 'language'; + $views_field['argument']['id'] = 'language'; + $views_field['filter']['id'] = 'language'; + $views_field['sort']['id'] = 'standard'; + break; + } + } + + return $data; + } +} diff --git a/core/modules/views/lib/Drupal/views/Tests/Entity/EntityViewsControllerTest.php b/core/modules/views/lib/Drupal/views/Tests/Entity/EntityViewsControllerTest.php new file mode 100644 index 0000000..b239ab2 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/Entity/EntityViewsControllerTest.php @@ -0,0 +1,64 @@ + 'Entity views controller', + 'description' => 'Tests the generic entity views controller on the entity_test entity type.', + 'group' => 'Views Modules', + ); + } + + /** + * Tests the expected views data of entity_test. + */ + public function testViewsData() { + $views_data = views_fetch_data('entity_test'); + debug($views_data); + + // @todo Quite dump simple testing. + $this->assertEqual($views_data['id']['field']['id'], 'numeric'); + $this->assertEqual($views_data['id']['filter']['id'], 'numeric'); + $this->assertEqual($views_data['id']['argument']['id'], 'numeric'); + $this->assertEqual($views_data['id']['sort']['id'], 'standard'); + + $this->assertEqual($views_data['uuid']['field']['id'], 'standard'); + $this->assertEqual($views_data['uuid']['filter']['id'], 'string'); + $this->assertEqual($views_data['uuid']['argument']['id'], 'string'); + $this->assertEqual($views_data['uuid']['sort']['id'], 'standard'); + + // @todo Does it make sense to just integrate the langcode column once + // language module is enabled? + $this->assertEqual($views_data['langcode']['field']['id'], 'language'); + $this->assertEqual($views_data['langcode']['filter']['id'], 'language'); + $this->assertEqual($views_data['langcode']['argument']['id'], 'language'); + $this->assertEqual($views_data['langcode']['sort']['id'], 'standard'); + } + + /** + * @todo + * Test an actual running view. + */ +} diff --git a/core/modules/views/views.module b/core/modules/views/views.module index cf10c1b..0c6f769 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -1338,6 +1338,21 @@ function views_get_enabled_display_extenders() { } /** + * Gets the entity views controller class for an entity type. + * + * @return \Drupal\views\EntityViewsController + */ +function views_entity_get_views_controller($entity_type) { + $controllers = &drupal_static(__FUNCTION__, array()); + if (!isset($controllers[$entity_type])) { + $type_info = entity_get_info($entity_type); + $class = $type_info['views_controller_class']; + $controllers[$entity_type] = new $class($entity_type); + } + return $controllers[$entity_type]; +} + +/** * Create an empty view to work with. * * @return Drupal\views\Plugin\Core\Entity\View