public function ContentEntity::getPartialItemIds in Search API 8
1 call to ContentEntity::getPartialItemIds()
- ContentEntity::getItemIds in src/Plugin/search_api/datasource/ContentEntity.php
- Returns a list of IDs of items from this datasource.
File
- src/Plugin/search_api/datasource/ContentEntity.php, line 780
Class
- ContentEntity
- Represents a datasource which exposes the content entities.
Namespace
Drupal\search_api\Plugin\search_api\datasource
Code
public function getPartialItemIds($page = NULL, array $bundles = NULL, array $languages = NULL) {
if ($bundles === [] && !$languages || $languages === [] && !$bundles) {
return NULL;
}
$entity_type = $this
->getEntityType();
$entity_id = $entity_type
->getKey('id');
if ($entity_type
->getBaseTable()) {
$select = $this
->getDatabaseConnection()
->select($entity_type
->getBaseTable(), 'base_table')
->fields('base_table', [
$entity_id,
]);
}
else {
$select = $this
->getEntityTypeManager()
->getStorage($this
->getEntityTypeId())
->getQuery();
$select
->accessCheck(FALSE);
}
$batch_page_context = [
'index_id' => $this
->getIndex()
->id(),
'datasource_id' => $this
->getPluginId(),
'bundles' => $bundles,
'languages' => $languages,
];
$context_key = Crypt::hashBase64(serialize($batch_page_context));
$last_ids = $this
->getState()
->get(self::TRACKING_PAGE_STATE_KEY, []);
if ($this
->hasBundles()) {
$bundle_property = $entity_type
->getKey('bundle');
if ($bundles && !$languages) {
$select
->condition($bundle_property, $bundles, 'IN');
}
else {
$enabled_bundles = array_keys($this
->getBundles());
if ($bundles) {
$enabled_bundles = array_unique(array_merge($bundles, $enabled_bundles));
}
if (count($enabled_bundles) < count($this
->getEntityBundles())) {
$select
->condition($bundle_property, $enabled_bundles, 'IN');
}
}
}
if (isset($page)) {
$page_size = $this
->getConfigValue('tracking_page_size');
assert($page_size, 'Tracking page size is not set.');
$offset = $page * $page_size;
if ($page > 0) {
if (isset($last_ids[$context_key]) && $last_ids[$context_key]['page'] == $page - 1 && $this
->getEntityTypeId() !== 'search_api_task') {
$select
->condition($entity_id, $last_ids[$context_key]['last_id'], '>');
$offset = 0;
}
}
$select
->range($offset, $page_size);
if ($select instanceof SelectInterface) {
$select
->orderBy($entity_id);
}
else {
$select
->sort($entity_id);
}
}
if ($select instanceof SelectInterface) {
$entity_ids = $select
->execute()
->fetchCol();
}
else {
$entity_ids = $select
->execute();
}
if (!$entity_ids) {
if (isset($page)) {
unset($last_ids[$context_key]);
$this
->getState()
->set(self::TRACKING_PAGE_STATE_KEY, $last_ids);
}
return NULL;
}
if (isset($page)) {
$last_ids[$context_key] = [
'page' => (int) $page,
'last_id' => end($entity_ids),
];
$this
->getState()
->set(self::TRACKING_PAGE_STATE_KEY, $last_ids);
}
$item_ids = [];
$enabled_languages = array_keys($this
->getLanguages());
if ($languages) {
$enabled_languages = array_unique(array_merge($languages, $enabled_languages));
}
$enabled_languages[] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
$enabled_languages[] = LanguageInterface::LANGCODE_NOT_APPLICABLE;
foreach ($this
->getEntityStorage()
->loadMultiple($entity_ids) as $entity_id => $entity) {
$translations = array_keys($entity
->getTranslationLanguages());
$translations = array_intersect($translations, $enabled_languages);
if ($languages !== NULL && (!$bundles || !in_array($entity
->bundle(), $bundles))) {
$translations = array_intersect($translations, $languages);
}
foreach ($translations as $langcode) {
$item_ids[] = "{$entity_id}:{$langcode}";
}
}
if (Utility::isRunningInCli()) {
$this
->getEntityMemoryCache()
->deleteAll();
}
return $item_ids;
}