diff --git a/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php b/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php index d0b025f..7e61ee1 100644 --- a/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php +++ b/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php @@ -91,21 +91,38 @@ class ContentEntitySource extends SourcePluginBase implements SourcePreviewInter $translation = $entity->getTranslation($job_item->getJob()->getSourceLangcode()); $data = $this->extractTranslatableData($translation); - // Add weights and sort item data elements. - $default = count(Element::children($data)); + return $data; + } + + /** + * Gets data items weights. + * + * @param \Drupal\tmgmt\JobItemInterface $job_item + * Job item. + * + * @return array + * Data items weights or bool if there is no entity. + * + * @throws \Drupal\tmgmt\TMGMTException + */ + public function getDataWeights(JobItemInterface $job_item) { + $entity = entity_load($job_item->getItemType(), $job_item->getItemId()); + if (!$entity) { + return FALSE; + } $entity_form_display = entity_get_form_display($job_item->getItemType(), $entity->bundle(), 'default'); + $data = $this->getData($job_item); + $default = count(Element::children($data)); + $weights = []; 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']; + $weights[$key] = (int) $entity_form_display->getComponent($key)['weight']; } else { - $data[$key]['#weight'] = ++$default; + $weights[$key] = ++$default; } } - uasort($data, function ($a, $b) { - return $a['#weight'] > $b['#weight']; - }); - return $data; + return $weights; } /** diff --git a/src/Entity/JobItem.php b/src/Entity/JobItem.php index 4b70177..6ab0144 100644 --- a/src/Entity/JobItem.php +++ b/src/Entity/JobItem.php @@ -19,6 +19,7 @@ use Drupal\Core\Language\Language; use Drupal\tmgmt\JobItemInterface; use Drupal\tmgmt\TMGMTException; use Drupal\Core\Render\Element; +use Drupal\tmgmt_content\Plugin\tmgmt\Source\ContentEntitySource; /** * Entity class for the tmgmt_job_item entity. @@ -372,6 +373,17 @@ class JobItem extends ContentEntityBase implements JobItemInterface { $this->save(); } if (empty($key)) { + // Write weights from source and order data. + $plugin = $this->getSourcePlugin(); + if ($plugin instanceof ContentEntitySource && $plugin->getDataWeights($this)) { + $weights = $plugin->getDataWeights($this); + foreach ($this->unserializedData as $field => $value) { + $this->unserializedData[$field]['#weight'] = $weights[$field]; + } + uasort($this->unserializedData, function ($a, $b) { + return $a['#weight'] > $b['#weight']; + }); + } return $this->unserializedData; } if ($index) { diff --git a/src/Form/JobItemForm.php b/src/Form/JobItemForm.php index d0030fa..fa83f94 100644 --- a/src/Form/JobItemForm.php +++ b/src/Form/JobItemForm.php @@ -119,8 +119,6 @@ class JobItemForm extends TmgmtFormBase { $form['review'] = array( '#type' => 'container', ); - // Reset item data in case we're manage fields and form display. - $item->resetData(); // Build the review form. $data = $item->getData(); // Need to keep the first hierarchy. So flatten must take place inside