diff --git a/src/Form/IndexAddFieldsForm.php b/src/Form/IndexAddFieldsForm.php index 078fbb46..7a7f7434 100644 --- a/src/Form/IndexAddFieldsForm.php +++ b/src/Form/IndexAddFieldsForm.php @@ -229,10 +229,6 @@ protected function getDatasourceListItem(DatasourceInterface $datasource = NULL) $base_url->setOption('query', ['datasource' => $datasource_id_param]); $item = $this->getPropertiesList($properties, $active_property_path, $base_url, $datasource_id); - - // Sort the retrieved properties by their label. - uasort($item, [$this->fieldsHelper, 'sortByFieldLabel']); - $item['#title'] = $datasource ? $datasource->label() : $this->t('General'); return $item; } @@ -263,9 +259,7 @@ protected function getDatasourceListItem(DatasourceInterface $datasource = NULL) * properties. */ protected function getPropertiesList(array $properties, $active_property_path, Url $base_url, $datasource_id, $parent_path = '', $label_prefix = '') { - $list = [ - '#theme' => 'search_api_form_item_list', - ]; + $list = []; $active_item = ''; if ($active_property_path) { @@ -398,10 +392,37 @@ protected function getPropertiesList(array $properties, $active_property_path, U $list[$key] = $item; } + if ($list) { + uasort($list, [static::class, 'compareFieldLabels']); + $list['#theme'] = 'search_api_form_item_list'; + } + return $list; } /** + * Compares labels of property render arrays. + * + * Used as an uasort() callback in + * \Drupal\search_api\Form\IndexAddFieldsForm::getPropertiesList(). + * + * @param array $a + * First property render array. + * @param array $b + * Second property render array. + * + * @return int + * -1, 0 or 1 if the first array should be considered, respectively, less + * than, equal to or greater than the second. + */ + public static function compareFieldLabels(array $a, array $b) { + $a_title = (string) $a['label']['#markup']; + $b_title = (string) $b['label']['#markup']; + + return strnatcasecmp($a_title, $b_title); + } + + /** * {@inheritdoc} */ protected function actions(array $form, FormStateInterface $form_state) { diff --git a/src/Utility/FieldsHelper.php b/src/Utility/FieldsHelper.php index dfe92f39..9e612cf4 100644 --- a/src/Utility/FieldsHelper.php +++ b/src/Utility/FieldsHelper.php @@ -488,23 +488,4 @@ public function getNewFieldId(IndexInterface $index, $propertyPath) { return $fieldId; } - /** - * Uasort callback. Compares field labels found in $field['label']['#markup]. - * - * @param mixed $a - * First item for comparison. The compared items should be associative - * arrays that include a ['label']['#markup] key. - * @param mixed $b - * Second item for comparison. - * - * @return int - * The comparison result for uasort(). - */ - public function sortByFieldLabel($a, $b) { - $a_title = (is_array($a) && isset($a['label']['#markup'])) ? $a['label']['#markup'] : ''; - $b_title = (is_array($b) && isset($b['label']['#markup'])) ? $b['label']['#markup'] : ''; - - return strnatcasecmp($a_title, $b_title); - } - }