@ -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 nam e.
* Selects configurable node text fields, grouped by content typ e.
*/
*/
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;
}
}
$bundle = $field->getTargetBundle();
if (!isset($bundles[$bundle])) {
continue;
}
$name = $field->getName();
$name = $field->getName();
$locations[$name][] = $field->getLabel() . ' (' . $field->getTargetEntityTypeId() . ':' . $field->getTargetBundle() . ')';
$groups[$bundle][$name] = $field->getLabel() . ' (' . $name . ')';
}
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') . ')';
}
}
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' => 'checkboxes',
'#type' => 'container',
'#title' => $this->t('Text fields') ,
'#tree' => TRUE,
'#options' => $options,
];
'#default_value' => $selected,
$form['fields_help'] = [
'#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 .'),
'#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);
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);
}
}