From 2715379e4751130cdba4582270fe67b5fabeef52 Mon Sep 17 00:00:00 2001 From: astanley Date: Wed, 3 Sep 2025 12:51:17 -0300 Subject: [PATCH] trimmed consoldation --- css/styles.css | 8 ++ js/help_modal.js | 18 ++--- roblib_alter_solr.services.yml | 2 +- .../RoblibAlterSolrSubscriber.php | 71 +++++++++++------ src/Form/RoblibAlterSolrSettingsForm.php | 78 +++++++++++++------ 5 files changed, 120 insertions(+), 57 deletions(-) diff --git a/css/styles.css b/css/styles.css index fa6a270..2e8f4f5 100644 --- a/css/styles.css +++ b/css/styles.css @@ -32,3 +32,11 @@ text-decoration: none; cursor: pointer; } + +#pairs-wrapper .source-field, +#pairs-wrapper .destination-field { + max-width: 49%; + width: 49%; + box-sizing: border-box; + display: inline-block; /* keeps both at 49% inside a cell */ +} diff --git a/js/help_modal.js b/js/help_modal.js index c3d39bf..ebc5484 100644 --- a/js/help_modal.js +++ b/js/help_modal.js @@ -7,17 +7,15 @@ var modalContent = ''; + ''; $('body').append(modalContent); $('#help-modal').fadeIn(); diff --git a/roblib_alter_solr.services.yml b/roblib_alter_solr.services.yml index c4cafdd..82f65c5 100644 --- a/roblib_alter_solr.services.yml +++ b/roblib_alter_solr.services.yml @@ -1,6 +1,6 @@ services: roblib_alter_solr.event_subscriber: class: Drupal\roblib_alter_solr\EventSubscriber\RoblibAlterSolrSubscriber - arguments: ['@logger.factory', '@config.factory'] + arguments: ['@logger.factory', '@config.factory', '@entity_type.manager'] tags: - { name: event_subscriber } diff --git a/src/EventSubscriber/RoblibAlterSolrSubscriber.php b/src/EventSubscriber/RoblibAlterSolrSubscriber.php index 9e6acea..73562a1 100644 --- a/src/EventSubscriber/RoblibAlterSolrSubscriber.php +++ b/src/EventSubscriber/RoblibAlterSolrSubscriber.php @@ -7,6 +7,9 @@ use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\search_api_solr\Event\SearchApiSolrEvents; use Drupal\search_api_solr\Event\PostCreateIndexDocumentEvent; use Drupal\Core\Config\ConfigFactory; +use Drupal\search_api\Plugin\search_api\data_type\value\TextValue; +use Drupal\taxonomy\Entity\Term; +use Drupal\Core\Entity\EntityTypeManagerInterface; /** * Allows users to combine Solr fields dynamically. @@ -27,6 +30,13 @@ final class RoblibAlterSolrSubscriber implements EventSubscriberInterface { */ protected $config; + /** + * The Entity Type Manager. + * + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $entityTypeManager; + /** * Constructs a new RoblibAlterSolrSubscriber. * @@ -34,10 +44,13 @@ final class RoblibAlterSolrSubscriber implements EventSubscriberInterface { * The logger factory service. * @param \Drupal\Core\Config\ConfigFactory $config * The config factory service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * The EntityTypeManager. */ - public function __construct(LoggerChannelFactoryInterface $logger_factory, ConfigFactory $config) { + public function __construct(LoggerChannelFactoryInterface $logger_factory, ConfigFactory $config, EntityTypeManagerInterface $entityTypeManager) { $this->logger = $logger_factory->get('roblib_alter_solr'); $this->config = $config->get('roblib_alter_solr.settings'); + $this->entityTypeManager = $entityTypeManager; } /** @@ -59,35 +72,45 @@ final class RoblibAlterSolrSubscriber implements EventSubscriberInterface { $solrDocument = $event->getSolariumDocument(); $item = $event->getSearchApiItem(); $pairs = $this->config->get('pairs'); - foreach ($pairs as $index => $values) { - $fields = array_values($values['source']); - $consolidated_values = []; + $results = []; + foreach ($pairs as $pair) { + $dest = $pair['destination']; + if (!isset($results[$dest])) { + $results[$dest] = []; + } + $results[$dest][] = $pair['source']; + } + $consolidated_values = []; + foreach ($results as $index => $fields) { + $consolidated_values[$index] = []; foreach ($fields as $field) { $field_value = $item->getField($field); if ($field_value && $field_value->getValues()) { - $all_values = $field_value->getValues(); - $consolidated_values[] = $all_values[0]; + $values = $field_value->getValues(); + $normalized = array_map(function($v) { + if ($v instanceof TextValue) { + return $v->getText(); + } + elseif (is_object($v) && method_exists($v, 'getValue')) { + return $v->getValue(); + } + elseif (is_array($v)) { + return implode(', ', array_map('strval', $v)); + } + return (string) $v; + }, $values); + $consolidated_values[$index] = array_merge($normalized, $consolidated_values[$index]); } } - if ($consolidated_values) { - $field_name = "sm_{$values['destination']}"; - $solrDocument->setField($field_name, $consolidated_values); - } - foreach ($this->config->get('normalize') as $name) { - $field_value = $item->getField($name); - if ($field_value && $field_value->getValues()) { - $value = $field_value->getValues()[0] ?? ''; - $cleaned_string = preg_match('/^\|+$/', $value) ? '' : preg_replace('/\|+/', ', ', $value); - if ($cleaned_string) { - $solrDocument->setField("ss_{$name}", $cleaned_string); - } - else { - $solrDocument->removeField("ss_{$name}"); - } - - } + } + if ($consolidated_values) { + foreach ($consolidated_values as $field_name => $values) { + $source = $item->getField($field_name); + $source_values = $source->getValues(); + $array_values = array_merge($source_values, $values); + $field_name = "sm_{$field_name}"; + $solrDocument->setField($field_name, $array_values); } - } } diff --git a/src/Form/RoblibAlterSolrSettingsForm.php b/src/Form/RoblibAlterSolrSettingsForm.php index d0db28e..4b7a988 100644 --- a/src/Form/RoblibAlterSolrSettingsForm.php +++ b/src/Form/RoblibAlterSolrSettingsForm.php @@ -35,7 +35,7 @@ final class RoblibAlterSolrSettingsForm extends ConfigFormBase { $solr_fields = $solr_config->get('field_settings'); $fields_options = []; foreach ($solr_fields as $name => $field) { - $fields_options[$name] = $field['label']; + $fields_options[$name] = $field['label'] . ' - ' . $name; } // Use getStorage() to persist state across AJAX calls. $pairs = $form_state->getStorage()['pairs'] ?? $form_state->get('pairs') ?? $config->get('pairs') ?? []; @@ -46,6 +46,9 @@ final class RoblibAlterSolrSettingsForm extends ConfigFormBase { '#prefix' => '', ]; + + + $form['pairs_description'] = [ '#type' => 'markup', '#markup' => '

' . $this->t('Define the source and destination mappings.') . '

', @@ -53,26 +56,52 @@ final class RoblibAlterSolrSettingsForm extends ConfigFormBase { $form['pairs'] = [ '#type' => 'table', '#header' => [ + '', $this->t('Source'), $this->t('Destination'), $this->t('Operations'), + $this->t('Weight'), + ], + '#tabledrag' => [ + [ + 'action' => 'order', // we are ordering rows + 'relationship' => 'sibling', + 'group' => 'pairs-weight', // CSS class applied to the weight field + ], ], '#prefix' => '
', '#suffix' => '
', ]; + $added = FALSE; foreach ($pairs as $index => $pair) { + $form['pairs'][$index]['#attributes']['class'][] = 'draggable'; + $form['pairs'][$index]['handle'] = [ + '#markup' => '', + '#attributes' => ['class' => ['tabledrag-handle']], + ]; $form['pairs'][$index]['source'] = [ '#type' => 'select', - '#multiple' => TRUE, '#options' => $fields_options, '#default_value' => $pair['source'] ?? '', + '#attributes' => ['class' => ['source-field']], ]; $form['pairs'][$index]['destination'] = [ - '#type' => 'textfield', + '#type' => 'select', + '#options' => $fields_options, '#default_value' => $pair['destination'] ?? '', + '#attributes' => ['class' => ['destination-field']], + ]; + $form['pairs'][$index]['weight'] = [ + '#type' => 'weight', + '#title' => $this->t('Weight for row @row', ['@row' => $index]), + '#title_display' => 'invisible', + '#default_value' => $pair['weight'] ?? 0, + '#attributes' => [ + 'class' => ['pairs-weight', 'tabledrag-hide'], + ], ]; if ($pair['source'] && $pair['destination']) { $form['pairs'][$index]['remove'] = [ @@ -101,25 +130,37 @@ final class RoblibAlterSolrSettingsForm extends ConfigFormBase { $form['pairs'][$index]['destination']['#attributes'] = ['class' => ['destination-field']]; $added = TRUE; } - } if (!$added) { $unique_id = uniqid(); $pairs[$unique_id] = ['source' => '', 'destination' => '']; + $form['pairs'][$unique_id]['handle'] = [ + '#markup' => '', + '#attributes' => ['class' => ['tabledrag-handle']], + ]; $form['pairs'][$unique_id]['source'] = [ '#type' => 'select', - '#multiple' => TRUE, '#options' => $fields_options, - '#default_value' => '', + '#default_value' => $pair['source'] ?? '', '#attributes' => ['class' => ['source-field']], ]; $form['pairs'][$unique_id]['destination'] = [ - '#type' => 'textfield', + '#type' => 'select', + '#options' => $fields_options, '#default_value' => '', '#attributes' => ['class' => ['destination-field']], ]; - $form['add'] = [ + $form['pairs'][$unique_id]['weight'] = [ + '#type' => 'weight', + '#title' => $this->t('Weight for row @row', ['@row' => $unique_id]), + '#title_display' => 'invisible', + '#default_value' => $pair['weight'] ?? 0, + '#attributes' => [ + 'class' => ['pairs-weight', 'tabledrag-hide'], + ], + ]; + $form['pairs'][$unique_id]['add'] = [ '#type' => 'submit', '#value' => $this->t('Add Row'), '#submit' => ['::addCallback'], @@ -128,18 +169,8 @@ final class RoblibAlterSolrSettingsForm extends ConfigFormBase { 'wrapper' => 'pairs-wrapper', ], ]; - } - $form['normalize_description'] = [ - '#type' => 'markup', - '#markup' => '

' . $this->t('Select aggregated field(s) to be normalized..') . '

', - ]; - $form['normalize'] = [ - '#type' => 'select', - '#multiple' => TRUE, - '#options' => $fields_options, - '#default_value' => $config->get('normalize') ?? '', - ]; + $form['#attached']['library'][] = 'roblib_alter_solr/roblib_alter_solr'; return parent::buildForm($form, $form_state); @@ -185,12 +216,15 @@ final class RoblibAlterSolrSettingsForm extends ConfigFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { $pairs = $form_state->getValue('pairs'); + $filtered_pairs= array_filter($pairs, function ($item) { + return !empty($item['destination']); + }); $normalize = $form_state->getValue('normalize'); $this->config('roblib_alter_solr.settings') - ->set('pairs', $pairs) - ->set('normalize', $normalize) + ->set('pairs', $filtered_pairs) ->save(); - $this->messenger()->addStatus($this->t('Configuration saved successfully.')); + $this->messenger() + ->addStatus($this->t('Configuration saved successfully.')); } }