From 15795ec762ee9e21c8e7ccaf550459020a04829d Mon Sep 17 00:00:00 2001 From: astanley Date: Fri, 4 Dec 2026 19:57:44 -0400 Subject: [PATCH] added sorting by type --- src/Form/SettingsForm.php | 67 ++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index 42c65fb..a2efc42 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -4,11 +4,10 @@ namespace Drupal\dynamic_field_label\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\field\Entity\FieldConfig; use Drupal\Core\Render\Element\Checkboxes; /** - * Selects existing configurable text fields by machine name. + * Selects configurable node text fields, grouped by content type. */ final class SettingsForm extends ConfigFormBase { @@ -22,40 +21,58 @@ final class SettingsForm extends ConfigFormBase { public function buildForm(array $form, FormStateInterface $form_state): array { $selected = $this->config('dynamic_field_label.settings')->get('fields') ?? []; - $options = []; - $locations = []; - foreach (FieldConfig::loadMultiple() as $field) { + $manager = \Drupal::entityTypeManager(); + $bundles = $manager->getStorage('node_type')->loadMultiple(); + $fields = $manager->getStorage('field_config') + ->loadByProperties(['entity_type' => 'node']); + $groups = []; + + // Group eligible fields by their content type. + foreach ($fields as $field) { if (!in_array($field->getType(), ['string', 'string_long', 'text', 'text_long', 'text_with_summary'], TRUE)) { continue; } + $bundle = $field->getTargetBundle(); + if (!isset($bundles[$bundle])) { + continue; + } $name = $field->getName(); - $locations[$name][] = $field->getLabel() . ' (' . $field->getTargetEntityTypeId() . ':' . $field->getTargetBundle() . ')'; - } - foreach ($locations as $name => $labels) { - $options[$name] = $name . ' — ' . implode('; ', array_unique($labels)); - } - // Preserve configured names when a field has temporarily been removed. - foreach ($selected as $name) { - $options[$name] ??= $name . ' (' . $this->t('not currently available') . ')'; + $groups[$bundle][$name] = $field->getLabel() . ' (' . $name . ')'; } - ksort($options); + + // Sort content types and their fields alphabetically by displayed label. + uksort($groups, static fn ($a, $b) => strnatcasecmp((string) $bundles[$a]->label(), (string) $bundles[$b]->label()) ?: strcmp($a, $b)); $form['fields'] = [ - '#type' => 'checkboxes', - '#title' => $this->t('Text fields'), - '#options' => $options, - '#default_value' => $selected, - '#description' => $this->t('Selected machine names apply across entity types and bundles. Values must use label:value. Labels are plain text; values retain their assigned text format for formatted fields.'), + '#type' => 'container', + '#tree' => TRUE, + ]; + $form['fields_help'] = [ + '#markup' => $this->t('Fields shared across content types are enabled globally if checked in any group. To disable a shared field, uncheck it in every group. Values must use label:value. Formatted values retain their assigned text format.'), ]; + foreach ($groups as $bundle => $options) { + natcasesort($options); + $form['fields'][$bundle] = [ + '#type' => 'checkboxes', + '#title' => $bundles[$bundle]->label(), + '#options' => $options, + '#default_value' => array_values(array_intersect($selected, array_keys($options))), + ]; + } return parent::buildForm($form, $form_state); } public function submitForm(array &$form, FormStateInterface $form_state): void { - $fields = array_values( - Checkboxes::getCheckedCheckboxes( - $form_state->getValue('fields') ?? [] - ) - ); - $this->config('dynamic_field_label.settings')->set('fields', $fields)->save(); + // Keep the existing flat configuration format and deduplicate shared fields. + $selected = []; + foreach ($form_state->getValue('fields') ?? [] as $values) { + if (is_array($values)) { + foreach (Checkboxes::getCheckedCheckboxes($values) as $name) { + $selected[$name] = $name; + } + } + } + ksort($selected); + $this->config('dynamic_field_label.settings')->set('fields', array_values($selected))->save(); parent::submitForm($form, $form_state); }