From f003c872fb575f298d98b32637b09ad583833aa7 Mon Sep 17 00:00:00 2001 From: Andrew Berezovsky Date: Tue, 15 Feb 2022 10:46:14 +0300 Subject: [PATCH] Index only the ages from the ages configuration. --- .../search_api/processor/AgesMinMax.php | 80 ++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/src/Plugin/search_api/processor/AgesMinMax.php b/src/Plugin/search_api/processor/AgesMinMax.php index 8935e66..94e3a07 100644 --- a/src/Plugin/search_api/processor/AgesMinMax.php +++ b/src/Plugin/search_api/processor/AgesMinMax.php @@ -2,10 +2,12 @@ namespace Drupal\openy_activity_finder\Plugin\search_api\processor; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\search_api\Datasource\DatasourceInterface; use Drupal\search_api\Item\ItemInterface; use Drupal\search_api\Processor\ProcessorPluginBase; use Drupal\search_api\Processor\ProcessorProperty; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Adds the Ages Min Max to the indexed data. @@ -23,6 +25,60 @@ */ class AgesMinMax extends ProcessorPluginBase { + /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + /** @var static $processor */ + $plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition); + + $plugin->setConfigFactory($container->get('config.factory')); + + return $plugin; + } + + /** + * Retrieves the config factory service. + * + * @return \Drupal\Core\Config\ConfigFactoryInterface + * The config factory. + */ + protected function getConfigFactory() { + return $this->configFactory ?: \Drupal::configFactory(); + } + + /** + * Sets the config factory service. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. + * + * @return $this + */ + protected function setConfigFactory(ConfigFactoryInterface $config_factory) { + $this->configFactory = $config_factory; + return $this; + } + + /** + * Retrieves the AF backend service. + * + * @return \Drupal\openy_activity_finder\OpenyActivityFinderBackendInterface + * The AF backend service. + */ + protected function getBackendService() { + $config = $this->getConfigFactory()->get('openy_activity_finder.settings'); + $backend_service_id = $config->get('backend'); + return \Drupal::service($backend_service_id); + } + /** * {@inheritdoc} */ @@ -64,7 +120,7 @@ public function addFieldValues(ItemInterface $item) { $range = [$min_age]; } else { - $range = range($min_age, $max_age, 6); + $range = $this->getAgesRange($min_age, $max_age); } $fields = $this->getFieldsHelper() ->filterForPropertyPath($item->getFields(), NULL, 'search_api_af_ages_min_max'); @@ -76,4 +132,26 @@ public function addFieldValues(ItemInterface $item) { } } + /** + * Determines range of ages to index. + * + * @param int $min_age + * The min age of the range. + * @param int $max_age + * The max age of the range. + * + * @return array + * The array of values to index. + */ + private function getAgesRange(int $min_age, int $max_age) { + $backend = $this->getBackendService(); + $ages = $backend->getAges(); + // Get only count of months. + $ages = array_column($ages, 'value'); + + return array_values(array_filter($ages, function($value) use ($min_age, $max_age) { + return $value >= $min_age && $value <= $max_age; + })); + } + }