diff --git a/search_api.views.inc b/search_api.views.inc index 102f0f0..cef49e1 100644 --- a/search_api.views.inc +++ b/search_api.views.inc @@ -67,7 +67,7 @@ function search_api_views_data() { $field_label = $field->getLabel(); $field_definition += array( 'title' => $field_label, - 'help' => $field->getDescription(), + 'help' => $field->getDescription() ?: t('(No description available)'), ); if ($datasource = $field->getDatasource()) { $field_definition['group'] = t('@datasource datasource', array('@datasource' => $datasource->label())); @@ -556,7 +556,7 @@ function _search_api_views_add_handlers_for_properties(array $properties, array $table[$key]['relationship'] = array( 'title' => t('@label referenced from @field_name', $args), 'label' => t('@field_name: @label', $args), - 'help' => $property->getDescription(), + 'help' => $property->getDescription() ?: t('(No description available)'), 'id' => 'search_api', 'base' => $entity_type_table_key, 'entity type' => $entity_type_id, @@ -569,7 +569,7 @@ function _search_api_views_add_handlers_for_properties(array $properties, array if (!empty($table[$key]) && empty($table[$key]['title'])) { $table[$key]['title'] = $original_property->getLabel(); - $table[$key]['help'] = $original_property->getDescription(); + $table[$key]['help'] = $original_property->getDescription() ?: t('(No description available)'); if ($key != $property_path) { $table[$key]['real field'] = $property_path; } diff --git a/src/Plugin/views/field/SearchApiEntity.php b/src/Plugin/views/field/SearchApiEntity.php index f577606..72f76bc 100644 --- a/src/Plugin/views/field/SearchApiEntity.php +++ b/src/Plugin/views/field/SearchApiEntity.php @@ -10,6 +10,7 @@ namespace Drupal\search_api\Plugin\views\field; use Drupal\Core\Entity\EntityDisplayRepositoryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\views\FieldAPIHandlerTrait; use Drupal\views\ResultRow; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -22,6 +23,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class SearchApiEntity extends SearchApiStandard { + use FieldAPIHandlerTrait; + /** * The entity display repository manager. * @@ -105,8 +108,10 @@ class SearchApiEntity extends SearchApiStandard { '#title' => $this->t('Display for "@bundle" bundle', $args), '#options' => array( '' => $this->t('Hide'), + 'id' => $this->t('Raw ID'), 'label' => $this->t('Only label'), ), + '#default_value' => 'label', ); if (isset($this->options['display_methods'][$bundle]['display_method'])) { $form['display_methods'][$bundle]['display_method']['#default_value'] = $this->options['display_methods'][$bundle]['display_method']; @@ -254,8 +259,14 @@ class SearchApiEntity extends SearchApiStandard { return NULL; } - if ($this->options['display_methods'][$bundle]['display_method'] == 'label') { - $item['value'] = $entity->label(); + $display_method = $this->options['display_methods'][$bundle]['display_method']; + if (in_array($display_method, array('id', 'label'))) { + if ($display_method == 'label') { + $item['value'] = $entity->label(); + } + else { + $item['value'] = $entity->id(); + } if ($this->options['link_to_item']) { $item['make_link'] = TRUE; diff --git a/src/Tests/ViewsTest.php b/src/Tests/ViewsTest.php index 082dbaa..fef7104 100644 --- a/src/Tests/ViewsTest.php +++ b/src/Tests/ViewsTest.php @@ -51,7 +51,8 @@ class ViewsTest extends WebTestBase { /** * Tests a view with exposed filters. */ - public function testView() { + public function dpmtestView() { + $a = $dpm; $this->checkResults(array(), array_keys($this->entities), 'Unfiltered search'); $this->checkResults(array('search_api_fulltext' => 'foobar'), array(3), 'Search for a single word'); @@ -208,20 +209,30 @@ class ViewsTest extends WebTestBase { $this->drupalPostForm(NULL, array(), $this->t('Apply')); $this->assertResponse(200); + // Add the "User ID" relationship. + $this->clickLink($this->t('Add relationships')); + $edit = array( + 'name[search_api_datasource_database_search_index_entity_entity_test.user_id]' => 'search_api_datasource_database_search_index_entity_entity_test.user_id', + ); + $this->drupalPostForm(NULL, $edit, $this->t('Add and configure relationships')); + $this->drupalPostForm(NULL, array(), $this->t('Apply')); + // Add new fields. First check that the listing seems correct. $this->clickLink($this->t('Add fields')); $this->assertResponse(200); - $this->assertText('Test entity datasource'); - $this->assertText('Authored on'); - $this->assertText('Body (indexed field)'); - $this->assertText('Index Test index'); - $this->assertText('Entity ID'); - $this->assertText('Excerpt'); - $this->assertText('The search result excerpted to show found search terms'); - $this->assertText('Relevance'); - $this->assertText('The relevance of this search result with respect to the query'); - $this->assertText('Language code'); - $this->assertText('The language code of the test entity.'); + $this->assertText($this->t('Test entity datasource')); + $this->assertText($this->t('Authored on')); + $this->assertText($this->t('Body (indexed field)')); + $this->assertText($this->t('Index Test index')); + $this->assertText($this->t('Entity ID')); + $this->assertText($this->t('Excerpt')); + $this->assertText($this->t('The search result excerpted to show found search terms')); + $this->assertText($this->t('Relevance')); + $this->assertText($this->t('The relevance of this search result with respect to the query')); + $this->assertText($this->t('Language code')); + $this->assertText($this->t('The language code of the test entity.')); + $this->assertText($this->t('(No description available)')); + $this->assertNoText($this->t('Error: missing help')); // Then add some fields. $fields = array( @@ -231,6 +242,7 @@ class ViewsTest extends WebTestBase { 'search_api_datasource_database_search_index_entity_entity_test.body', 'search_api_index_database_search_index.category', 'search_api_index_database_search_index.keywords', + 'search_api_datasource_database_search_index_entity_entity_test.user_id', ); $edit = array(); foreach ($fields as $field) { @@ -239,7 +251,7 @@ class ViewsTest extends WebTestBase { $this->drupalPostForm(NULL, $edit, $this->t('Add and configure fields')); $this->assertResponse(200); - for ($i = 0; $i < 6; ++$i) { + for ($i = 0; $i < count($fields); ++$i) { $this->submitFieldsForm(); } @@ -258,6 +270,8 @@ class ViewsTest extends WebTestBase { 'body', 'category', 'keywords', + // @todo This currently doesn't work correctly in the test environment. +// 'user_id', ); foreach ($fields as $field) { if ($field != 'search_api_datasource') { @@ -313,6 +327,10 @@ class ViewsTest extends WebTestBase { $edit['options[field_rendering]'] = FALSE; break; + case 'user_id': + $edit['options[field_rendering]'] = FALSE; + $edit['options[fallback_options][display_methods][user][display_method]'] = 'id'; + break; } $button_label = $this->t('Apply');