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\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Render\Element\Checkboxes; 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 { final class SettingsForm extends ConfigFormBase {
@ -22,40 +21,58 @@ final class SettingsForm extends ConfigFormBase {
public function buildForm(array $form, FormStateInterface $form_state): array { public function buildForm(array $form, FormStateInterface $form_state): array {
$selected = $this->config('dynamic_field_label.settings')->get('fields') ?? []; $selected = $this->config('dynamic_field_label.settings')->get('fields') ?? [];
$options = []; $manager = \Drupal::entityTypeManager();
$locations = []; $bundles = $manager->getStorage('node_type')->loadMultiple();
foreach (FieldConfig::loadMultiple() as $field) { $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)) { if (!in_array($field->getType(), ['string', 'string_long', 'text', 'text_long', 'text_with_summary'], TRUE)) {
continue; continue;
} }
$name = $field->getName(); $bundle = $field->getTargetBundle();
$locations[$name][] = $field->getLabel() . ' (' . $field->getTargetEntityTypeId() . ':' . $field->getTargetBundle() . ')'; if (!isset($bundles[$bundle])) {
} continue;
foreach ($locations as $name => $labels) {
$options[$name] = $name . ' — ' . implode('; ', array_unique($labels));
} }
// Preserve configured names when a field has temporarily been removed. $name = $field->getName();
foreach ($selected as $name) { $groups[$bundle][$name] = $field->getLabel() . ' (' . $name . ')';
$options[$name] ??= $name . ' (' . $this->t('not currently available') . ')';
} }
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'] = [ $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', '#type' => 'checkboxes',
'#title' => $this->t('Text fields'), '#title' => $bundles[$bundle]->label(),
'#options' => $options, '#options' => $options,
'#default_value' => $selected, '#default_value' => array_values(array_intersect($selected, array_keys($options))),
'#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.'),
]; ];
}
return parent::buildForm($form, $form_state); return parent::buildForm($form, $form_state);
} }
public function submitForm(array &$form, FormStateInterface $form_state): void { public function submitForm(array &$form, FormStateInterface $form_state): void {
$fields = array_values( // Keep the existing flat configuration format and deduplicate shared fields.
Checkboxes::getCheckedCheckboxes( $selected = [];
$form_state->getValue('fields') ?? [] foreach ($form_state->getValue('fields') ?? [] as $values) {
) if (is_array($values)) {
); foreach (Checkboxes::getCheckedCheckboxes($values) as $name) {
$this->config('dynamic_field_label.settings')->set('fields', $fields)->save(); $selected[$name] = $name;
}
}
}
ksort($selected);
$this->config('dynamic_field_label.settings')->set('fields', array_values($selected))->save();
parent::submitForm($form, $form_state); parent::submitForm($form, $form_state);
} }

Loading…
Cancel
Save