Skip to content

Commit

Permalink
EWPP-593: Ensuring only entity type/bundle combos that are indexed ar…
Browse files Browse the repository at this point in the history
…e for list pages.
  • Loading branch information
upchuk committed Dec 7, 2020
1 parent 492392d commit 6bacd76
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 102 deletions.
11 changes: 8 additions & 3 deletions src/Form/ListPageConfigurationSubForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,11 @@ protected function areExposedFiltersOverridden(ListSourceInterface $list_source)
protected function getEntityTypeOptions(): array {
$entity_type_options = [];
$entity_types = $this->entityTypeManager->getDefinitions();
foreach ($entity_types as $entity_type_key => $entity_type) {
if (!$entity_type instanceof ContentEntityTypeInterface) {
foreach ($entity_types as $entity_type_id => $entity_type) {
if (!$entity_type instanceof ContentEntityTypeInterface || !$this->listSourceFactory->isEntityTypeSourced($entity_type_id)) {
continue;
}
$entity_type_options[$entity_type_key] = $entity_type->getLabel();
$entity_type_options[$entity_type_id] = $entity_type->getLabel();
}

$event = new ListPageSourceAlterEvent(array_keys($entity_type_options));
Expand All @@ -469,6 +469,11 @@ protected function getBundleOptions(string $selected_entity_type): array {
$bundle_options = [];
$bundles = $this->entityTypeBundleInfo->getBundleInfo($selected_entity_type);
foreach ($bundles as $bundle_key => $bundle) {
$list_source = $this->listSourceFactory->get($selected_entity_type, $bundle_key);
if (!$list_source instanceof ListSourceInterface) {
continue;
}

$bundle_options[$bundle_key] = $bundle['label'];
}

Expand Down
56 changes: 52 additions & 4 deletions src/ListSourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\facets\FacetManager\DefaultFacetManager;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\IndexInterface;

/**
Expand All @@ -31,7 +32,7 @@ class ListSourceFactory implements ListSourceFactoryInterface {
/**
* The list sources.
*
* @var array
* @var \Drupal\oe_list_pages\ListSourceInterface[]
*/
protected $listsSources;

Expand All @@ -52,7 +53,6 @@ public function __construct(DefaultFacetManager $facetsManager, EntityTypeManage
* {@inheritdoc}
*/
public function get(string $entity_type, string $bundle): ?ListSourceInterface {

if (empty($this->listsSources)) {
$this->instantiateLists();
}
Expand All @@ -61,6 +61,23 @@ public function get(string $entity_type, string $bundle): ?ListSourceInterface {
return !empty($this->listsSources[$id]) ? $this->listsSources[$id] : NULL;
}

/**
* {@inheritdoc}
*/
public function isEntityTypeSourced(string $entity_type): bool {
if (empty($this->listsSources)) {
$this->instantiateLists();
}

foreach ($this->listsSources as $lists_source) {
if ($lists_source->getEntityType() === $entity_type) {
return TRUE;
}
}

return FALSE;
}

/**
* {@inheritdoc}
*/
Expand All @@ -72,7 +89,6 @@ public static function generateFacetSourcePluginId(string $entity_type, string $
* Instantiate the list sources from the indexed content bundles.
*/
protected function instantiateLists(): void {

if (!empty($this->listsSources)) {
return;
}
Expand All @@ -93,7 +109,7 @@ protected function instantiateLists(): void {
$bundles = $datasource->getBundles();
foreach ($bundles as $bundle => $label) {
// In case not all bundles are indexed.
if (!empty($datasource->getConfiguration()['bundles']['selected']) && !in_array($bundle, $datasource->getConfiguration()['bundles']['selected'])) {
if (!$this->isBundleIndexed($datasource, $bundle)) {
continue;
}

Expand Down Expand Up @@ -138,4 +154,36 @@ protected function create(string $entity_type, string $bundle, IndexInterface $i
return new ListSource($id, $entity_type, $bundle, $bundle_field_id, $index, $filters);
}

/**
* Checks if a given bundle is indexed on a data source.
*
* @param \Drupal\search_api\Datasource\DatasourceInterface $datasource
* The datasource.
* @param string $bundle
* The bundle.
*
* @return bool
* Whether the bundle is indexed.
*/
protected function isBundleIndexed(DatasourceInterface $datasource, string $bundle): bool {
$configuration = $datasource->getConfiguration();
$selected = $configuration['bundles']['selected'];
if ($configuration['bundles']['default'] === TRUE && empty($selected)) {
// All bundles are indexed.
return TRUE;
}

if ($configuration['bundles']['default'] === TRUE && !empty($selected) && !in_array($bundle, $selected)) {
// All bundles are indexed, except a few that are selected.
return TRUE;
}

if ($configuration['bundles']['default'] === FALSE && in_array($bundle, $selected)) {
// Only specific bundles are indexed.
return TRUE;
}

return FALSE;
}

}
11 changes: 11 additions & 0 deletions src/ListSourceFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@ public static function generateFacetSourcePluginId(string $entity_type, string $
*/
public function get(string $entity_type, string $bundle): ?ListSourceInterface;

/**
* Checks whether a given entity type has a list source.
*
* @param string $entity_type
* The entity type ID.
*
* @return bool
* Whether the entity type is used in any list sources.
*/
public function isEntityTypeSourced(string $entity_type): bool;

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ field_settings:
datasource_settings:
'entity:node':
bundles:
default: true
selected: { }
default: false
selected:
- content_type_one
- content_type_two
languages:
default: true
selected: { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
langcode: en
status: true
dependencies:
config:
- search_api.server.database_server
module:
- search_api
- taxonomy
id: taxonomy
name: Taxonomy
description: ''
read_only: false
field_settings: { }
datasource_settings:
'entity:taxonomy_term':
bundles:
default: true
selected: { }
languages:
default: true
selected: { }
processor_settings:
add_url: { }
aggregated_field: { }
language_with_fallback: { }
rendered_item: { }
tracker_settings:
default:
indexing_order: fifo
options:
index_directly: true
cron_limit: 50
server: database_server
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
langcode: en
status: true
dependencies: { }
name: 'Vocabulary one'
vid: vocabulary_one
description: ''
weight: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
langcode: en
status: true
dependencies: { }
name: 'Vocabulary two'
vid: vocabulary_two
description: ''
weight: 0
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
- drupal:options
- drupal:datetime
- drupal:datetime_range
- drupal:taxonomy
- extra_field:extra_field
- oe_list_pages:oe_list_pages
- oe_list_pages:oe_list_page_content_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ public function testListPagePluginFiltersFormConfiguration(): void {
$actual_entity_types = $this->getSelectOptions('Source entity type');
$expected_entity_types = [
'' => '- Select -',
'entity_meta_relation' => 'Entity Meta Relation',
'entity_meta' => 'Entity meta',
'node' => 'Content',
'path_alias' => 'URL alias',
'search_api_task' => 'Search task',
'user' => 'User',
'taxonomy_term' => 'Taxonomy term',
];
$this->assertEquals($expected_entity_types, $actual_entity_types);
// By default, Node is selected if there are no stored values.
Expand Down
Loading

0 comments on commit 6bacd76

Please sign in to comment.