config('roblib_alter_solr.settings'); $solr_config = $this->config('search_api.index.default_solr_index'); $solr_fields = $solr_config->get('field_settings'); $fields_options = [' - ' => ' - ']; foreach ($solr_fields as $name => $field) { $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') ?? []; $form_state->setStorage(['pairs' => $pairs]); $form['open_modal'] = [ '#type' => 'markup', '#markup' => '' . $this->t('How to use this form') . '', '#prefix' => '', ]; $form['pairs_description'] = [ '#type' => 'markup', '#markup' => '

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

', ]; $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', '#options' => $fields_options, '#default_value' => $pair['source'] ?? '', '#attributes' => ['class' => ['source-field']], ]; $form['pairs'][$index]['destination'] = [ '#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'] = [ '#type' => 'submit', '#value' => $this->t('Remove'), '#submit' => ['::removeCallback'], '#ajax' => [ 'callback' => '::ajaxCallback', 'wrapper' => 'pairs-wrapper', ], '#name' => 'remove_' . $index, '#limit_validation_errors' => [], ]; } else { $form['pairs'][$index]['add'] = [ '#type' => 'submit', '#value' => $this->t('Add Row'), '#submit' => ['::addCallback'], '#ajax' => [ 'callback' => '::ajaxCallback', 'wrapper' => 'pairs-wrapper', ], ]; $form['pairs'][$index]['source']['#attributes'] = ['class' => ['source-field']]; $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', '#options' => $fields_options, '#default_value' => $pair['source'] ?? '', '#attributes' => ['class' => ['source-field']], ]; $form['pairs'][$unique_id]['destination'] = [ '#type' => 'select', '#options' => $fields_options, '#default_value' => '', '#attributes' => ['class' => ['destination-field']], ]; $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'], '#ajax' => [ 'callback' => '::ajaxCallback', 'wrapper' => 'pairs-wrapper', ], ]; } $form['#attached']['library'][] = 'roblib_alter_solr/roblib_alter_solr'; return parent::buildForm($form, $form_state); } /** * Callback function to add a new row. */ public function addCallback(array &$form, FormStateInterface $form_state) { $user_input = $form_state->getUserInput(); $unique_id = uniqid(); $pairs = $user_input['pairs'] ?? []; $pairs= array_filter($pairs, function ($item) { return !empty($item['destination']); }); $pairs[$unique_id] = ['source' => '', 'destination' => '']; $form_state->set('pairs', $pairs); $form_state->setRebuild(); } /** * Callback function to remove a row. */ public function removeCallback(array &$form, FormStateInterface $form_state) { $trigger = $form_state->getTriggeringElement(); $index = str_replace('remove_', '', $trigger['#name']); $pairs = $form_state->get('pairs') ?? []; if (isset($pairs[$index])) { unset($pairs[$index]); $form_state->set('pairs', $pairs); } $form_state->setRebuild(); } /** * AJAX callback to update the form. */ public function ajaxCallback(array &$form, FormStateInterface $form_state) { return $form['pairs']; } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $pairs = $form_state->getValue('pairs'); $filtered_pairs= array_filter($pairs, function ($item) { return !empty($item['destination']) && !isset($item['add']); }); $normalize = $form_state->getValue('normalize'); $this->config('roblib_alter_solr.settings') ->set('pairs', $filtered_pairs) ->save(); $this->messenger() ->addStatus($this->t('Configuration saved successfully.')); } }