diff --git a/core/modules/views/lib/Drupal/views/EntityViewsController.php b/core/modules/views/lib/Drupal/views/EntityViewsController.php index 4369a73..4f3b648 100644 --- a/core/modules/views/lib/Drupal/views/EntityViewsController.php +++ b/core/modules/views/lib/Drupal/views/EntityViewsController.php @@ -10,10 +10,11 @@ use Drupal\Core\Entity\EntityControllerInterface; use Drupal\Core\Entity\EntityManager; use Drupal\Core\Entity\EntityStorageControllerInterface; +use Drupal\Core\StringTranslation\Translator\TranslationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides a generic views integration of entity-NG. + * Provides generic views integration for entities. */ class EntityViewsController implements EntityControllerInterface { @@ -29,7 +30,7 @@ class EntityViewsController implements EntityControllerInterface { * * @var array * - * @see entity_get_info() + * @see \Drupal\Core\Entity\EntityManager::getDefinition() */ protected $entityInfo; @@ -41,7 +42,14 @@ class EntityViewsController implements EntityControllerInterface { protected $storageController; /** - * Constructs a EntityViewsController object. + * The translation manager + * + * @var \Drupal\Core\StringTranslation\Translator\TranslationInterface + */ + protected $translationManager; + + /** + * Constructs an EntityViewsController object. * * @param string $entity_type * The entity type to provide views integration for. @@ -49,12 +57,15 @@ class EntityViewsController implements EntityControllerInterface { * The storage controller used for this entity type. * @param \Drupal\Core\Entity\EntityManager $entity_manager * The entity manager. + * @param \Drupal\Core\StringTranslation\Translator\TranslationInterface $translation_manager + * The translation manager. */ - function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage_controller, EntityManager $entity_manager) { + function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage_controller, EntityManager $entity_manager, TranslationInterface $translation_manager) { $this->entityType = $entity_type; $this->entityInfo = $entity_info; $this->entityManager = $entity_manager; $this->storageController = $storage_controller; + $this->translationManager = $translation_manager; } /** @@ -65,7 +76,8 @@ public static function createInstance(ContainerInterface $container, $entity_typ $entity_type, $entity_info, $container->get('entity.manager')->getStorageController($entity_type), - $container->get('entity.manager') + $container->get('entity.manager'), + $container->get('string_translation') ); } @@ -74,10 +86,12 @@ public static function createInstance(ContainerInterface $container, $entity_typ */ public function viewsData() { $data = array(); + $base_table = $this->entityInfo['base_table']; $base_field = $this->entityInfo['entity_keys']['id']; $data_table = isset($this->entityInfo['data_table']) ? $this->entityInfo['data_table']: NULL; $revision_table = isset($this->entityInfo['revision_table']) ? $this->entityInfo['revision_table'] : NULL; + $revision_data_table = isset($this->entityInfo['revision_data_table']) ? $this->entityInfo['revision_data_table'] : NULL; $revision_field = isset($this->entityInfo['entity_keys']['revision']) ? $this->entityInfo['entity_keys']['revision'] : NULL; // Setup base information of the views data. @@ -101,34 +115,41 @@ public function viewsData() { $data[$revision_table]['table']['group'] = $this->entityInfo['label']; $data[$revision_table]['table']['base'] = array( 'field' => $revision_field, - 'title' => t('@entity_type revisions', array('@entity_type' => $this->entityInfo['label'])), + 'title' => $this->t('@entity_type revisions', array('@entity_type' => $this->entityInfo['label'])), ); + // Join the revision table to the base table. $data[$revision_table]['table']['join'][$base_table] = array( 'left_field' => $base_field, 'field' => $base_field, ); - } - - // Load all typed data definitions of all fields. - $field_definitions = $this->entityManager->getFieldDefinitions($this->entityType); - foreach (array('base_table', 'data_table', 'revision_table', 'revision_data_table') as $table_key) { - if (!isset($this->entityInfo[$table_key])) { - continue; + if ($revision_data_table) { + $data[$revision_data_table]['table']['join'][$revision_table] = array( + 'left_field' => $revision_field, + 'field' => $revision_field, + ); } + } - $table = $this->entityInfo[$table_key]; + // Load all typed data definitions of all fields. This should cover each of + // the entity base, revision, data tables. + $field_definitions = $this->entityManager->getFieldDefinitions($this->entityType); - foreach (drupal_schema_fields_sql($this->entityInfo[$table_key]) as $field_name) { + // Iterate over each table we have so far and collect field data for each. + // Based on whether the field is in the field_definitions provided by the + // entity manager. + foreach (array_keys($data) as $table) { + foreach (drupal_schema_fields_sql($table) as $field_name) { if (isset($field_definitions[$field_name])) { $views_field = &$data[$table][$field_name]; // @todo Is translating the right way to handle label/description? - $views_field['title'] = t($field_definitions[$field_name]['label']); - $views_field['help'] = t($field_definitions[$field_name]['description']); + // This is/should be translated in the UI when it's displayed? + $views_field['title'] = $this->t($field_definitions[$field_name]['label']); + $views_field['help'] = $this->t($field_definitions[$field_name]['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. - $views_field = $this->mapTypedDataHandlerId($field_definitions[$field_name], $views_field); + $views_field = $this->mapTypedDataToHandlerId($field_definitions[$field_name], $views_field); } } } @@ -137,9 +158,17 @@ public function viewsData() { } /** - * @todo + * Provides a mapping from typed data plugin types to view plugin types. + * + * @param array $typed_data + * The typed data plugin definition + * @param array $views_field + * The views field data definition. + * + * @return array + * The modified views data field definition. */ - protected function mapTypedDataHandlerId($typed_data, $views_field) { + protected function mapTypedDataToHandlerId($typed_data, $views_field) { switch ($typed_data['type']) { case 'field_item:integer': $views_field['field']['id'] = 'numeric'; @@ -184,11 +213,21 @@ protected function mapTypedDataHandlerId($typed_data, $views_field) { $views_field['relationship'] = array( 'base' => $entity_info['base_table'], 'base field' => $entity_info['entity_keys']['id'], - 'label' => t($typed_data['label']), + 'label' => $this->t($typed_data['label']), 'id' => 'standard', ); } + return $views_field; } + /** + * Translates a string to the current language or to a given language. + * + * See the t() documentation for details. + */ + protected function t($string, array $args = array(), array $options = array()) { + return $this->translationManager->translate($string, $args, $options); + } + } diff --git a/core/modules/views/tests/Drupal/views/Tests/EntityViewsControllerTest.php b/core/modules/views/tests/Drupal/views/Tests/EntityViewsControllerTest.php index d5fa31f..1b78b2c 100644 --- a/core/modules/views/tests/Drupal/views/Tests/EntityViewsControllerTest.php +++ b/core/modules/views/tests/Drupal/views/Tests/EntityViewsControllerTest.php @@ -1,51 +1,69 @@ getMockBuilder('Drupal\Core\Entity\DatabaseStorageControllerNG') -// ->disableOriginalConstructor() -// ->getMock(); -// $entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager') -// ->disableOriginalConstructor() -// ->getMock(); -// -// $entity_manager->expects($this->any()) -// ->method('getDefinition') -// ->with($this->equalTo('entity_test')) -// ->will($this->returnValue(array( -// 'id' => 'entity_test', -// 'base_table' => 'entity_test', -// 'label' => 'Test entity', -// 'entity_keys' => array( -// 'id' => 'id', -// 'uuid' => 'uuid', -// 'bundle' => 'type', -// ), -// ))); -// -// $views_controller = new EntityViewsController('entity_test', $storage, $entity_manager); -// -// $data = $views_controller->viewsData(); -// $this->assertEquals('id', $data['base']['base']['field']); -// } -// -//} + +/** + * @file + * Contains \Drupal\views\Tests\EntityViewsControllerTest. + */ + +namespace Drupal\views\Tests; + +use Drupal\Tests\UnitTestCase; +use Drupal\views\EntityViewsController; + +/** + * Tests the generic entity views controller. + * + * @see \Drupal\views\EntityViewsController + */ +class EntityViewsControllerTest extends UnitTestCase { + + /** + * Entity info to use in this test. + * + * @var array + */ + protected $entityInfo = array( + 'base_table' => 'entity_test', + 'label' => 'Entity test', + 'entity_keys' => array( + 'id' => 'id', + ), + ); + + /** + * Test base tables. + */ + public function testBaseTables() { + $storage = $this->getMockBuilder('Drupal\Core\Entity\DatabaseStorageController') + ->disableOriginalConstructor() + ->getMock(); + $entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $entity_manager->expects($this->any()) + ->method('getDefinition') + ->with($this->equalTo('entity_test')) + ->will($this->returnValue(array( + 'id' => 'entity_test', + 'base_table' => 'entity_test', + 'label' => 'Test entity', + 'entity_keys' => array( + 'id' => 'id', + 'uuid' => 'uuid', + 'bundle' => 'type', + ), + ))); + + $translation_manager = $this->getMock('Drupal\Core\StringTranslation\Translator\TranslationInterface'); + $translation_manager->expects($this->any()) + ->method('translate') + ->will($this->returnArgument(0)); + + $views_controller = new EntityViewsController('entity_test', $this->entityInfo, $storage, $entity_manager, $translation_manager); + + $data = $views_controller->viewsData(); + $this->assertEquals('id', $data['base']['base']['field']); + } + +}