diff --git a/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php b/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php index 731a2c7..a72972b 100644 --- a/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php +++ b/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php @@ -91,18 +91,36 @@ class ContentEntitySource extends SourcePluginBase implements SourcePreviewInter $translation = $entity->getTranslation($job_item->getJob()->getSourceLangcode()); $data = $this->extractTranslatableData($translation); - $default = count(Element::children($data)); $entity_form_display = entity_get_form_display($job_item->getItemType(), $entity->bundle(), 'default'); - foreach (Element::children($data) as $key) { - if ($entity_form_display->getComponent($key) && !is_null($entity_form_display->getComponent($key)['weight'])) { - $data[$key]['#weight'] = (int) $entity_form_display->getComponent($key)['weight']; + uksort($data, function ($a, $b) use ($entity_form_display) { + $a_weight = NULL; + $b_weight = NULL; + // Get the weights. + if ($entity_form_display->getComponent($a) && !is_null($entity_form_display->getComponent($a)['weight'])) { + $a_weight = (int) $entity_form_display->getComponent($a)['weight']; + } + if ($entity_form_display->getComponent($b) && !is_null($entity_form_display->getComponent($b)['weight'])) { + $b_weight = (int) $entity_form_display->getComponent($b)['weight']; + } + + // If neither field has a weight, sort alphabetically. + if ($a_weight === NULL && $b_weight === NULL) { + return ($a > $b) ? 1 : -1; + } + // If one of them has no weight, the other comes first. + elseif ($a_weight === NULL) { + return 1; + } + elseif ($b_weight === NULL) { + return -1; + } + // If both have a weight, sort by weight. + elseif ($a_weight == $b_weight) { + return 0; } else { - $data[$key]['#weight'] = ++$default; + return ($a_weight > $b_weight) ? 1 : -1; } - } - uasort($data, function ($a, $b) { - return $a['#weight'] > $b['#weight']; }); return $data; } diff --git a/translators/tmgmt_local/src/Form/LocalTaskItemForm.php b/translators/tmgmt_local/src/Form/LocalTaskItemForm.php index d3109a8..a06cb15 100644 --- a/translators/tmgmt_local/src/Form/LocalTaskItemForm.php +++ b/translators/tmgmt_local/src/Form/LocalTaskItemForm.php @@ -132,8 +132,7 @@ class LocalTaskItemForm extends ContentEntityForm { // Need to keep the first hierarchy. So flatten must take place inside // of the foreach loop. $zebra = 'even'; - // Reverse the order to get the correct order. - foreach (array_reverse(Element::children($data)) as $key) { + foreach (Element::children($data) as $key) { $flattened = \Drupal::service('tmgmt.data')->flatten($data[$key], $key); $form['translation'][$key] = $this->formElement($flattened, $task_item, $zebra); }