FlagSearchApiReindexService.php in Flag Search API 8
File
src/FlagSearchApiReindexService.php
View source
<?php
namespace Drupal\flag_search_api;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\flag\FlaggingInterface;
use Drupal\search_api\Plugin\search_api\datasource\ContentEntity;
class FlagSearchApiReindexService implements FlagSearchApiReindexServiceInterface {
protected $configFactory;
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
public function reindexItem(FlaggingInterface $entity) {
$reindex_on_flagging = $this->configFactory
->get('flag_search_api.settings')
->get('reindex_on_flagging');
if ($reindex_on_flagging) {
$datasource_id = 'entity:' . $entity
->getFlaggableType();
$content_flagged = $entity
->getFlaggable();
$indexes = ContentEntity::getIndexesForEntity($content_flagged);
$entity_id = $entity
->getFlaggableId();
$updated_item_ids = $content_flagged
->getTranslationLanguages();
foreach ($updated_item_ids as $langcode => $language) {
$inserted_item_ids[] = $langcode;
}
$combine_id = function ($langcode) use ($entity_id) {
return $entity_id . ':' . $langcode;
};
$updated_item_ids = array_map($combine_id, array_keys($updated_item_ids));
foreach ($indexes as $index) {
if ($updated_item_ids) {
$index
->trackItemsUpdated($datasource_id, $updated_item_ids);
}
}
}
}
}