/** * {@inheritdoc} */ public function render($row) { $datasource_id = $row->search_api_datasource; if (!$row->_item instanceof ComplexDataInterface) { $context = array('%item_id' => $row->search_api_id, '%view' => $this->view->storage->label()); $this->getLogger()->warning('Failed to load item %item_id in view %view.', $context); return ''; } // @todo Use isValidDatasource() instead. try { $datasource = $this->index->getDatasource($datasource_id); } catch (SearchApiException $e) { $context = array('%datasource' => $datasource_id, '%view' => $this->view->storage->label()); $this->getLogger()->warning('Item of unknown datasource %datasource returned in view %view.', $context); return ''; } // Always use the default view mode if it was not set explicitly in the // options. $view_mode = 'default'; $bundle = $this->index->getDatasource($datasource_id)->getItemBundle($row->_item); if (isset($this->options['view_modes'][$datasource_id][$bundle])) { $view_mode = $this->options['view_modes'][$datasource_id][$bundle]; } try { return $this->index->getDatasource($datasource_id)->viewItem($row->_item, $view_mode); } catch (SearchApiException $e) { watchdog_exception('search_api', $e); return ''; } }
/** * {@inheritdoc} */ public function getDatasource() { if (!isset($this->datasource)) { $this->datasource = $this->index->getDatasource($this->getDatasourceId()); } return $this->datasource; }
/** * Tests translation handling of the content entity datasource. */ public function testItemTranslations() { // Test retrieving language and translations when no translations are // available. $entity_1 = EntityTestMul::create(array('id' => 1, 'name' => 'test 1', 'user_id' => $this->container->get('current_user')->id())); $entity_1->save(); $this->assertEqual($entity_1->language()->getId(), 'en', SafeMarkup::format('%entity_type: Entity language set to site default.', array('%entity_type' => $this->testEntityTypeId))); $this->assertFalse($entity_1->getTranslationLanguages(FALSE), SafeMarkup::format('%entity_type: No translations are available', array('%entity_type' => $this->testEntityTypeId))); $entity_2 = EntityTestMul::create(array('id' => 2, 'name' => 'test 2', 'user_id' => $this->container->get('current_user')->id())); $entity_2->save(); $this->assertEqual($entity_2->language()->getId(), 'en', SafeMarkup::format('%entity_type: Entity language set to site default.', array('%entity_type' => $this->testEntityTypeId))); $this->assertFalse($entity_2->getTranslationLanguages(FALSE), SafeMarkup::format('%entity_type: No translations are available', array('%entity_type' => $this->testEntityTypeId))); // Test that the datasource returns the correct item IDs. $datasource = $this->index->getDatasource('entity:' . $this->testEntityTypeId); $datasource_item_ids = $datasource->getItemIds(); sort($datasource_item_ids); $expected = array('1:en', '2:en'); $this->assertEqual($datasource_item_ids, $expected, 'Datasource returns correct item ids.'); // Test indexing the new entity. $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 0, 'The index is empty.'); $this->assertEqual($this->index->getTracker()->getTotalItemsCount(), 2, 'There are two items to be indexed.'); $this->index->index(); $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 2, 'Two items have been indexed.'); // Now, make the first entity language-specific by assigning a language. $default_langcode = $this->langcodes[0]; $entity_1->get('langcode')->setValue($default_langcode); $entity_1->save(); $this->assertEqual($entity_1->language(), \Drupal::languageManager()->getLanguage($this->langcodes[0]), SafeMarkup::format('%entity_type: Entity language retrieved.', array('%entity_type' => $this->testEntityTypeId))); $this->assertFalse($entity_1->getTranslationLanguages(FALSE), SafeMarkup::format('%entity_type: No translations are available', array('%entity_type' => $this->testEntityTypeId))); // Test that the datasource returns the correct item IDs. $datasource_item_ids = $datasource->getItemIds(); sort($datasource_item_ids); $expected = array('1:' . $this->langcodes[0], '2:en'); $this->assertEqual($datasource_item_ids, $expected, 'Datasource returns correct item ids.'); // Test that the index needs to be updated. $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 1, 'The updated item needs to be reindexed.'); $this->assertEqual($this->index->getTracker()->getTotalItemsCount(), 2, 'There are two items in total.'); // Set two translations for the first entity and test that the datasource // returns three separate item IDs, one for each translation. $translation = $entity_1->getTranslation($this->langcodes[1]); $translation->save(); $translation = $entity_1->getTranslation($this->langcodes[2]); $translation->save(); $this->assertTrue($entity_1->getTranslationLanguages(FALSE), SafeMarkup::format('%entity_type: Translations are available', array('%entity_type' => $this->testEntityTypeId))); $datasource_item_ids = $datasource->getItemIds(); sort($datasource_item_ids); $expected = array('1:' . $this->langcodes[0], '1:' . $this->langcodes[1], '1:' . $this->langcodes[2], '2:en'); $this->assertEqual($datasource_item_ids, $expected, 'Datasource returns correct item ids for a translated entity.'); // Test that the index needs to be updated. $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 1, 'The updated items needs to be reindexed.'); $this->assertEqual($this->index->getTracker()->getTotalItemsCount(), 4, 'There are four items in total.'); // Delete one translation and test that the datasource returns only three // items. $entity_1->removeTranslation($this->langcodes[2]); $entity_1->save(); $datasource_item_ids = $datasource->getItemIds(); sort($datasource_item_ids); $expected = array('1:' . $this->langcodes[0], '1:' . $this->langcodes[1], '2:en'); $this->assertEqual($datasource_item_ids, $expected, 'Datasource returns correct item ids for a translated entity.'); // Test reindexing. $this->assertEqual($this->index->getTracker()->getTotalItemsCount(), 3, 'There are three items in total.'); $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 1, 'The updated items needs to be reindexed.'); $this->index->index(); $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 3, 'Three items are indexed.'); }
/** * {@inheritdoc} */ public function getDatasource($datasource_id) { return $this->entity->getDatasource($datasource_id); }