From 5eac976ad39886ac1f9cfb07b787ee4d1aa034e0 Mon Sep 17 00:00:00 2001 From: Markus Kalkbrenner Date: Mon, 25 Nov 2024 15:14:05 +0100 Subject: [PATCH] Issue #3486761 by scott_euser, mkalkbrenner: Prepare for updated add fields to index UI --- composer.json | 3 +- search_api_solr.module | 2 +- src/Form/IndexAddSolrDocumentFieldsForm.php | 70 ++++++--------------- 3 files changed, 22 insertions(+), 53 deletions(-) diff --git a/composer.json b/composer.json index 756c8253..f39e31a5 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,8 @@ "conflict": { "drupal/acquia_search_solr": "<1.0.0-beta8", "drupal/search_api_autocomplete": "<1.6.0", - "drupal/search_api_solr_multilingual": "<3.0.0" + "drupal/search_api_solr_multilingual": "<3.0.0", + "drupal/search_api": "<1.36" }, "suggest": { "drupal/facets": "Provides facetted search.", diff --git a/search_api_solr.module b/search_api_solr.module index db0b8821..4aebb181 100644 --- a/search_api_solr.module +++ b/search_api_solr.module @@ -683,7 +683,7 @@ function search_api_solr_form_search_api_index_fields_alter(&$form, FormStateInt ], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode([ - 'width' => 700, + 'width' => '90%', ]), ], '#weight' => -10, diff --git a/src/Form/IndexAddSolrDocumentFieldsForm.php b/src/Form/IndexAddSolrDocumentFieldsForm.php index 0a1c3d52..59896708 100644 --- a/src/Form/IndexAddSolrDocumentFieldsForm.php +++ b/src/Form/IndexAddSolrDocumentFieldsForm.php @@ -2,13 +2,9 @@ namespace Drupal\search_api_solr\Form; -use Drupal\Component\Render\FormattableMarkup; -use Drupal\Component\Utility\Html; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; use Drupal\search_api\Form\IndexAddFieldsForm; use Drupal\search_api_solr\Plugin\search_api\datasource\SolrDocument; -use Drupal\search_api_solr\Plugin\search_api\datasource\SolrMultisiteDocument; /** * Provides a form for adding fields to a search index. @@ -25,25 +21,7 @@ public function getFormId() { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state) { - $index = $this->entity; - - // Do not allow the form to be cached. See - // \Drupal\views_ui\ViewEditForm::form(). - $form_state->disableCache(); - - $this->checkEntityEditable($form, $index); - - $args['%index'] = $index->label(); - $form['#title'] = $this->t('Add fields to index %index', $args); - - $this->formIdAttribute = Html::getUniqueId($this->getFormId()); - $form['#id'] = $this->formIdAttribute; - - $form['messages'] = [ - '#type' => 'status_messages', - ]; - + protected function buildDatasourcesForm(array $form, FormStateInterface $form_state): array { $datasources = [ '' => NULL, ]; @@ -52,40 +30,30 @@ public function buildForm(array $form, FormStateInterface $form_state) { if ($datasource instanceof SolrDocument) { $item = $this->getDatasourceListItem($datasource); if ($item) { - foreach ($index->getFieldsByDatasource($datasource_id) as $field) { - unset($item[$field->getPropertyPath()]); + foreach ($this->entity->getFieldsByDatasource($datasource_id) as $field) { + if (empty($item['table']['#rows'])) { + continue; + } + $property = $field->getPropertyPath(); + + // Fields to add are stored in rows with the machine name column + // matching the field property. Remove any table rows that have + // already been added. + foreach ($item['table']['#rows'] as $key => $row) { + if (empty($row['machine_name']['data'])) { + continue; + } + + if ($row['machine_name']['data'] === $property) { + unset($item['table']['#rows'][$key]); + } + } } $form['datasources']['datasource_' . $datasource_id] = $item; } } } - - $form['actions'] = $this->actionsElement($form, $form_state); - - // Log any unmapped types that were encountered. - if ($this->unmappedFields) { - $unmapped_fields = []; - foreach ($this->unmappedFields as $type => $fields) { - foreach ($fields as $field) { - $unmapped_fields[] = new FormattableMarkup('@field (type "@type")', [ - '@field' => $field, - '@type' => $type, - ]); - } - } - $form['unmapped_types'] = [ - '#type' => 'details', - '#title' => $this->t('Skipped fields'), - 'fields' => [ - '#theme' => 'item_list', - '#items' => $unmapped_fields, - '#prefix' => $this->t('The following fields cannot be indexed since there is no type mapping for them:'), - '#suffix' => $this->t("If you think one of these fields should be available for indexing, please report this in the module's issue queue. (Make sure to first search for an existing issue for this field.) Please note that entity-valued fields generally can be indexed by either indexing their parent reference field, or their child entity ID field.", [':url' => Url::fromUri('https://www.drupal.org/project/issues/search_api')->toString()]), - ], - ]; - } - return $form; }