Skip to content

Commit

Permalink
Issue #408: Sort columns by weight in BcDcAddColumnsForm::getDataSetF…
Browse files Browse the repository at this point in the history
…ields()
  • Loading branch information
lkmorlan committed Apr 4, 2024
1 parent d0c38ce commit 19225d0
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions html/modules/custom/bc_dc/src/Form/BcDcAddColumnsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,20 @@ public static function getDataSetFieldDefinitions(): array {
* Return an array of data_column field names that start with "field_".
*
* @return string[]
* An array of field names with "field_" removed.
* An array of field names with "field_" removed sorted by weight and name.
*/
public static function getDataSetFields(): array {
// Get the fields in the default display of data_column.
$displayFields = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load('paragraph.data_column.default')->getComponents();

// Get the keys of $displayFields sorted by weight and then by key name.
$weights = array_column($displayFields, 'weight');
$names = array_keys($displayFields);
array_multisort($weights, $names);

// Return only those starting with "field_" and remove that.
$fields = [];
$field_definitions = static::getDataSetFieldDefinitions();
foreach (array_keys($field_definitions) as $name) {
foreach ($names as $name) {
if (str_starts_with($name, 'field_')) {
$fields[] = substr($name, 6);
}
Expand Down

0 comments on commit 19225d0

Please sign in to comment.