0

I'm trying to implement a super simple proof of concept for pulling in non-drupal stored content into search api via Solr. The plugin is super simple:

<?php namespace Drupal\sla_indexing\Plugin\search_api\datasource; use Drupal\search_api\Datasource\DatasourcePluginBase; use Drupal\search_api\Item\Item; use Drupal\search_api\Item\ItemInterface; use Drupal\search_api\Item\Field; use Drupal\Core\TypedData\DataDefinition; /** * @SearchApiDatasource( * id = "test_external_datasource", * label = @Translation("Test External Datasource"), * description = @Translation("A test datasource for indexing non-Drupal content."), * ) */ class TestExternalDatasource extends DatasourcePluginBase { /** * {@inheritdoc} */ public function getItemIds($page = NULL) { \Drupal::logger('test_external_datasource')->debug('getitemids - @page', ['@page'=> print_r($page, TRUE) ]); // Hardcoded dummy IDs $all_ids = ['chainsaw_ferret_1', 'chainsaw_ferret_2']; // Only return on page 0 for proof-of-concept if ($page === 0 || $page === NULL) { return $all_ids; } return []; } public function getTotalItemsCount() { return 2; // Hardcoded to the two dummy records } /** * {@inheritdoc} */ public function getItemId($item) { \Drupal::logger('test_external_datasource')->debug('getItemId called for item: @item', ['@item' => print_r($item, TRUE)]); if (is_object($item) && method_exists($item, 'getId')) { return $item->getId(); } return $item; } /** * {@inheritdoc} */ public function getItem($item_id) { \Drupal::logger('test_external_datasource')->debug('getItem called for @id', ['@id' => $item_id]); $item = new Item($this->getPluginId(), $item_id); $label = ($item_id === 'chainsaw_ferret_1') ? 'Chainsaw Ferret Alpha: The legend begins' : 'Chainsaw Ferret Beta: The sequel nobody asked for'; $description = 'This is a dummy external record about chainsaw-wielding ferrets.'; $item->setField('label', Field::createByDatasource($this->getPluginId(), 'label')->addValue($label)); $item->setField('description', Field::createByDatasource($this->getPluginId(), 'description')->addValue($description)); return $item; } /** * {@inheritdoc} */ public function getPropertyDefinitions(ItemInterface $item = NULL) { return [ 'label' => DataDefinition::create('string')->setLabel(t('Label')), 'description' => DataDefinition::create('string')->setLabel(t('Description')), ]; } } 

All it should be doing is adding two items to the index. That's it. But when I turn the plug in on, it seems to get caught in an endless loop. Checking my logs, I see that the logger in getItemIds is getting called multiple times, and that the pager is continually incrementing. It got up to getitemids - 178289 before I shut it off. That's way more content than is on this test site. I'm not sure how to stop it. I tried making sure the getItemIds returns a null set so maybe the search API dosn't think there's more content and keeps trying to track it. I also tried implementing getTotalItemCount to make sure it stops after two itmes. Nothing seems to work, and I can't figure out how get it to stop looping.

2
  • What is the actual data type of $page? It's supposed to be an int|null but that's not formally type-hinted here or in the interface. Why are you passing $page to print_r? Commented Jul 14 at 17:07
  • I had that put in just in case page was an object, forgot to take it out. Just ran gettype on the page and can confirm it's an int. Commented Jul 14 at 18:32

1 Answer 1

2

Figured this out. Looks like the issue is search api will go until it gets a null response from getItemIds. An empty array is not null, so drupal was viewing that as a valid response and just kept going.

Returning NULL fixed the loop.

2
  • 1
    If you use PHPStorm, plugins.jetbrains.com/plugin/15560-phpstorm-inheritdoc can be really useful - it shows the original docblock in place of {@inheritdoc}, and it would've made it easier to spot the or NULL if there are no more items for this and all following pages. part of the return type docs Commented Jul 14 at 19:12
  • or try mouseover, the inherited docblock should show in a tooltip. Commented Jul 16 at 9:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.