View source
<?php
namespace Drupal\search_api\Plugin\search_api\processor;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\search_api\Plugin\PluginFormTrait;
use Drupal\search_api\Processor\ProcessorPluginBase;
use Drupal\search_api\Utility\Utility;
class TypeBoost extends ProcessorPluginBase implements PluginFormInterface {
use PluginFormTrait;
public function defaultConfiguration() {
return [
'boosts' => [],
];
}
public function buildConfigurationForm(array $form, FormStateInterface $formState) {
$datasource_configurations = [];
$additional_factors = [];
foreach ($this->index
->getDatasources() as $datasource_id => $datasource) {
$datasource_configuration = $this->configuration['boosts'][$datasource_id] ?? [];
$datasource_configuration += [
'datasource_boost' => Utility::formatBoostFactor(1),
'bundle_boosts' => [],
];
$datasource_configurations[$datasource_id] = $datasource_configuration;
$additional_factors = array_merge($additional_factors, [
$datasource_configuration['datasource_boost'],
], $datasource_configuration['bundle_boosts']);
}
$boost_factors = Utility::getBoostFactors($additional_factors);
$bundle_boost_options = [
'' => $this
->t('Use datasource default'),
] + $boost_factors;
foreach ($this->index
->getDatasources() as $datasource_id => $datasource) {
$form['boosts'][$datasource_id] = [
'#type' => 'details',
'#title' => $this
->t('Boost settings for %datasource', [
'%datasource' => $datasource
->label(),
]),
'#open' => TRUE,
'datasource_boost' => [
'#type' => 'select',
'#title' => $this
->t('Default boost for items from this datasource'),
'#options' => $boost_factors,
'#description' => $this
->t('A boost of 1.00 is the default. Assign a boost of 0.00 to not score the item at all.'),
'#default_value' => $datasource_configurations[$datasource_id]['datasource_boost'],
],
];
$bundles = $datasource
->getBundles();
if (count($bundles) === 1) {
unset($bundles[$datasource_id], $bundles[$datasource
->getEntityTypeId()]);
}
$bundle_boosts = $datasource_configurations[$datasource_id]['bundle_boosts'];
foreach ($bundles as $bundle => $bundle_label) {
$bundle_boost = Utility::formatBoostFactor($bundle_boosts[$bundle] ?? 0);
$form['boosts'][$datasource_id]['bundle_boosts'][$bundle] = [
'#type' => 'select',
'#title' => $this
->t('Boost for the %bundle bundle', [
'%bundle' => $bundle_label,
]),
'#options' => $bundle_boost_options,
'#default_value' => $bundle_boost,
];
}
}
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
foreach ($this->index
->getDatasourceIds() as $datasource_id) {
foreach ($values['boosts'][$datasource_id]['bundle_boosts'] ?? [] as $bundle => $boost) {
if ($boost === '') {
unset($values['boosts'][$datasource_id]['bundle_boosts'][$bundle]);
}
}
if (empty($values['boosts'][$datasource_id]['bundle_boosts'])) {
unset($values['boosts'][$datasource_id]['bundle_boosts']);
}
}
$form_state
->setValues($values);
$this
->setConfiguration($values);
}
public function preprocessIndexItems(array $items) {
$boosts = $this->configuration['boosts'];
foreach ($items as $item) {
$datasource_id = $item
->getDatasourceId();
$bundle = $item
->getDatasource()
->getItemBundle($item
->getOriginalObject());
$item_boost = (double) ($boosts[$datasource_id]['datasource_boost'] ?? 1.0);
if ($bundle && isset($boosts[$datasource_id]['bundle_boosts'][$bundle])) {
$item_boost = (double) $boosts[$datasource_id]['bundle_boosts'][$bundle];
}
$item
->setBoost($item
->getBoost() * $item_boost);
}
}
}