Browse Source

added sorting by type

main
astanley 3 hours ago
parent
commit
15795ec762
  1. 63
      src/Form/SettingsForm.php

63
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;
}
$name = $field->getName();
$locations[$name][] = $field->getLabel() . ' (' . $field->getTargetEntityTypeId() . ':' . $field->getTargetBundle() . ')';
}
foreach ($locations as $name => $labels) {
$options[$name] = $name . ' — ' . implode('; ', array_unique($labels));
$bundle = $field->getTargetBundle();
if (!isset($bundles[$bundle])) {
continue;
}
// Preserve configured names when a field has temporarily been removed.
foreach ($selected as $name) {
$options[$name] ??= $name . ' (' . $this->t('not currently available') . ')';
$name = $field->getName();
$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' => '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' => $this->t('Text fields'),
'#title' => $bundles[$bundle]->label(),
'#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.'),
'#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);
}

Loading…
Cancel
Save