183 lines
5.2 KiB
183 lines
5.2 KiB
<?php |
|
|
|
declare(strict_types=1); |
|
|
|
namespace Drupal\roblib_alter_solr\Form; |
|
|
|
use Drupal\Core\Form\ConfigFormBase; |
|
use Drupal\Core\Form\FormStateInterface; |
|
|
|
/** |
|
* Configure Roblib Alter Solr settings for this site. |
|
*/ |
|
final class RoblibAlterSolrSettingsForm extends ConfigFormBase { |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function getFormId(): string { |
|
return 'roblib_alter_solr_roblib_alter_solr_settings'; |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
protected function getEditableConfigNames(): array { |
|
return ['roblib_alter_solr.settings']; |
|
} |
|
|
|
/** |
|
* {@inheritdoc} |
|
*/ |
|
public function buildForm(array $form, FormStateInterface $form_state) { |
|
$config = $this->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']; |
|
} |
|
// 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' => '<a href="#" id="open-modal-link">' . $this->t('How to use this form') . '</a>', |
|
'#prefix' => '<div id="open_link-wrapper">', |
|
'#suffix' => '</div>', |
|
]; |
|
|
|
$form['pairs'] = [ |
|
'#type' => 'table', |
|
'#header' => [ |
|
$this->t('Source'), |
|
$this->t('Destination'), |
|
$this->t('Operations'), |
|
], |
|
'#prefix' => '<div id="pairs-wrapper">', |
|
'#suffix' => '</div>', |
|
]; |
|
$added = FALSE; |
|
|
|
foreach ($pairs as $index => $pair) { |
|
$form['pairs'][$index]['source'] = [ |
|
'#type' => 'select', |
|
'#multiple' => TRUE, |
|
'#options' => $fields_options, |
|
'#default_value' => $pair['source'] ?? '', |
|
]; |
|
|
|
$form['pairs'][$index]['destination'] = [ |
|
'#type' => 'textfield', |
|
'#default_value' => $pair['destination'] ?? '', |
|
]; |
|
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]['source'] = [ |
|
'#type' => 'select', |
|
'#multiple' => TRUE, |
|
'#options' => $fields_options, |
|
'#default_value' => '', |
|
'#attributes' => ['class' => ['source-field']], |
|
]; |
|
|
|
$form['pairs'][$unique_id]['destination'] = [ |
|
'#type' => 'textfield', |
|
'#default_value' => '', |
|
'#attributes' => ['class' => ['destination-field']], |
|
]; |
|
$form['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[$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'); |
|
$config = $this->config('roblib_alter_solr.settings'); |
|
$config->set('pairs', $pairs)->save(); |
|
|
|
$this->messenger()->addStatus($this->t('Configuration saved successfully.')); |
|
} |
|
|
|
|
|
|
|
}
|
|
|