Related #1963934: Add field sequence weight to gengo job

All fields have a clear sequence in Drupal. This needs to be preserved.

CommentFileSizeAuthor
#35 consider-field-sequences-35-interdiff.txt5.28 KBBerdir
#35 consider-field-sequences-35.patch8.9 KBBerdir
#35 consider-field-sequences-35-test-only.patch6.34 KBBerdir
#34 consider-field-sequences-34-interdiff.txt2.99 KBBerdir
#34 consider-field-sequences-34.patch6.18 KBBerdir
#27 Screen Shot 2016-03-18 at 10.35.57 AM.png239.46 KBCTaPByK
#27 Screen Shot 2016-03-18 at 10.35.24 AM.png201.58 KBCTaPByK
#27 consider-field-sequences.patch4.78 KBCTaPByK
#25 consider-field-sequences.patch4.89 KBCTaPByK
#25 interdiff-1971646-23-25.txt7.76 KBCTaPByK
#23 consider-field-sequnces.patch4.83 KBCTaPByK
#23 interdiff-215839-21-23.txt3.56 KBCTaPByK
#21 consider-field-sequnces.patch3.88 KBCTaPByK
#21 interdiff-1971646-19-20.txt3.22 KBCTaPByK
#19 Screen Shot 2016-03-16 at 2.55.37 PM.png284.35 KBCTaPByK
#19 Screen Shot 2016-03-16 at 2.55.24 PM.png226.08 KBCTaPByK
#19 consider-field-sequences.patch4.72 KBCTaPByK
#19 interdiff-1971646-17-19.txt7.18 KBCTaPByK
#17 consider-field-sequences.patch4.37 KBCTaPByK
#17 interdiff-1971646-13-17.txt5.87 KBCTaPByK
#13 Screen Shot 2016-03-11 at 1.55.42 AM.png269.07 KBCTaPByK
#13 Screen Shot 2016-03-11 at 1.55.14 AM.png381.1 KBCTaPByK
#13 consider-field-sequences.patch1.49 KBCTaPByK
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir’s picture

As discussed, "All fields have a clear sequence in Drupal. This needs to be preserved." is unfortunately not completely true.

There is $field_instance['widget']['weight'] but there also things like field_groups and or http://drupal.org/project/rel, which allow to nest fields, recursively.

So if we just implement the widget weight then it will quite likely be more confusing than it is now :)

miro_dietiker’s picture

Still, not having a clear sequence at all is even something that i consider a bug.

Here's my fancy real crazy idea ;-)
Pseudo-Form-Approach
We render the entity form and use element_children to traverse it.
Then, if we find a form element that we can map to a field, we know the sequence.
Problem: I don't know if we can map them reliable here...

Pseudo-Render-Approach
We Create an entity with some special values, trigger rendering (e.g. also with tweaked field theme functions...) and then check in the output where things went.
Problem: Well, even more crazy

Pseudo-View-Mode-Approach
Introduce a special view mode.
Ask the developer to enable this view mode to define the field sequence.
Add a note to the view mode that developers shall NOT use any grouping mechanism.
These weights we can use reliable.

The last one seems feasible to me. Minimum-intrusive as the view modes need to be enabled per content type. Although they're helpers only.

miro_dietiker’s picture

Assigning to blueminds as it is a fundamental bug / limit of the core now.

Gengo defined this a critical problem of their customers. However now that i understand - we have grouped jobs on their side... I'm confused as things are one single job per data item anyway - do they support weighting accross grouping? ;-) *confused*

Berdir’s picture

Assigned: Unassigned » blueminds

Form and rendering doesn't work. The grouping happens in #pre_render and preprocess functions, intercepting there would be really hard. And DS for example directly renders things into $left/right strings. And I think that it's easier to support field groups directly by reading out their children directly anyway.

A separate view mode would be relatively easy to implement but requires manual interaction. I would see that more as an option if the field order in the manage fields/widgets order should for some reason *not* be used.

miro_dietiker’s picture

So we try to order by weight... no own view mode reqired as long as no recursive fancyness applies.

Then we can decide if we try to implement the special cases in recursion...
Or if we support a view mode as a helper if weighted sort (due to grouping) goes wrong. Then, manual interaction required.

Berdir’s picture

There's one more complexity to consider here.

We're flattening/unflattening data items like crazy ourself. So a weight would need to be preserved, possibly similar to how to preserve labels now.

miro_dietiker’s picture

We can also flatten / unflatten weight by flatten the data like the OID in X.500 Schemas with the IANA registry, used for LDAP and SNMP:
1.3.6.1.4.1.24535 - Which is the global identifier for our company MD Systems. ;-)
(The structure is left-to-right hierarchical.)

miro_dietiker’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
Priority: Normal » Major
Issue summary: View changes

We are about to introduce multilingual paragraphs support.
#2462263: Support paragraphs translation.

Even more it is important that the fields and their values are guaranteed to be displayed in the proper order.

miro_dietiker’s picture

Assigned: blueminds » Unassigned

Unassigning.

miro_dietiker’s picture

What is the status of this?

I think we should order the fields if possibly by the default form (display) mode.

miro_dietiker’s picture

Issue tags: +Usability

Seen again strangely odd sequenced fields such as title field last.

CTaPByK’s picture

Assigned: Unassigned » CTaPByK

Ok, i start with this by reading discussion here and also reading related discussions about gengo job and paragraphs.

CTaPByK’s picture

Thanks to @Berdir and his clear instructions i make this very basic patch without test for now. I worked on JobItemForm, next steps are review and translate forms.

Status: Needs review » Needs work

The last submitted patch, 13: consider-field-sequences.patch, failed testing.

miro_dietiker’s picture

Some thoughts.

  1. +++ b/src/Form/JobItemForm.php
    @@ -25,6 +25,11 @@ use Drupal\Core\Render\Element;
    +  const WEIGHT = 1000;
    
    @@ -112,6 +127,9 @@ class JobItemForm extends TmgmtFormBase {
    +          $form['review'][$key]['#weight'] = $content->getComponent($key)['weight'] ?: $default_weight++;
    

    Not sure about this 1000+ strategy.

    I would have expected to more define to start with the elements in the sequence of the form display and append the remaining ones?

  2. +++ b/src/Form/JobItemForm.php
    @@ -112,6 +127,9 @@ class JobItemForm extends TmgmtFormBase {
    +        if ($key !== 'title') {
    

    Why do we need a special case for title?
    Title is also a field that is managed in manage form display weight.

miro_dietiker’s picture

I think the best is if you write a test that has a bunch of elements...
Then add the item, check the sequence.
Then change sequence.
Readd and recheck.

+++ b/src/Form/JobItemForm.php
@@ -25,6 +25,11 @@ use Drupal\Core\Render\Element;
@@ -105,6 +110,16 @@ class JobItemForm extends TmgmtFormBase {

@@ -105,6 +110,16 @@ class JobItemForm extends TmgmtFormBase {
+    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $content */
...
+      ->load($item->getItemType() . '.' . $entity->bundle() . '.' . 'default');

Now you are changing the sequence when showing the form.

Instead you should fix the sequence when addint the item + fields to the job!

Also the code is specific to the source. This lookup does not work for locale and other sources!

Any translator should receive the items in the correct sequence. Not just our form...

CTaPByK’s picture

Status: Needs work » Needs review
FileSize
5.87 KB
4.37 KB

I make this new patch by following suggestions #15 and #16, i modify getData method from ContentEntitySource to reorder job item data fields. In this case we have to reset job item data before every translation request. I'm not sure is this a good way, maybe just return weights from ContentEntitySource and then reorder in review and translation forms. I start some test for this but not finished yet. Screen shoots are the same as previous.

miro_dietiker’s picture

Status: Needs review » Needs work

In this case we have to reset job item data before every translation request.

Unsure. On one hand i would say this change could require that the item needs to be dropped / readded to the job. Fine.
But this could be an indicator that the field sequence wouldn't change until a manual cache clear is triggered? Please test and fix - or confirm it's just a test issue (thus reset workarounds are fine).

CTaPByK’s picture

Ok, here is the new patch with test coverage and with some changes from #17.

Status: Needs review » Needs work

The last submitted patch, 19: consider-field-sequences.patch, failed testing.

CTaPByK’s picture

Status: Needs work » Needs review
FileSize
3.22 KB
3.88 KB

I bring back logic from patch #17 with test. I reset item data before we build review form. For new created jobs data fields will be ordered as we menage them, this reset data is for 'old' jobs to make them consider field sequence too.

Status: Needs review » Needs work

The last submitted patch, 21: consider-field-sequnces.patch, failed testing.

CTaPByK’s picture

Status: Needs work » Needs review
FileSize
3.56 KB
4.83 KB

Ok i try to fix some failing tests, also move part of logic to job item getData() method, screen shots are the same as previous.

Berdir’s picture

Status: Needs review » Needs work
+++ b/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php
@@ -95,6 +95,37 @@ class ContentEntitySource extends SourcePluginBase implements SourcePreviewInter
+   */
+  public function getDataWeights(JobItemInterface $job_item) {
+    $entity = entity_load($job_item->getItemType(), $job_item->getItemId());
+    if (!$entity) {

This information should be part of $data and set inside getData().

e.g. $data['body']['#weight'] => 5

CTaPByK’s picture

Status: Needs work » Needs review
FileSize
7.76 KB
4.89 KB

I move logic to ContentEntitySource getData() method, also i make better test with more fields.

Status: Needs review » Needs work

The last submitted patch, 25: consider-field-sequences.patch, failed testing.

CTaPByK’s picture

Forget to pull, sorry for that...

Berdir’s picture

+++ b/sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php
@@ -91,6 +91,19 @@ class ContentEntitySource extends SourcePluginBase implements SourcePreviewInter
+    uasort($data, function ($a, $b) {
+      return $a['#weight'] > $b['#weight'];
+    });

I'm not sure if this is enough to make it reliable. Did you check in the local translator too?

miro_dietiker’s picture

Status: Needs review » Needs work

Switching back. Please check input from Berdir.

CTaPByK’s picture

Status: Needs work » Needs review

I spend some time on manually testing this patch for various cases and i didn't find problem. Yesterday i discuss with @Berdir about that and he will test this patch also to eventually find error in logic for some special cases.

miro_dietiker’s picture

Status: Needs review » Needs work

I tested this manually with a content type from tmgmt_demo and added a third field.
It was first after the body and later between title and body to do a second test run and compare changes.

The job item review form displayed the proper sequence.

The local / drupal user translator displayed the sequence reliably INVERSE.

miro_dietiker’s picture

Found in LocalTaskItemForm

    // 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) {
      $flattened = \Drupal::service('tmgmt.data')->flatten($data[$key], $key);
      $form['translation'][$key] = $this->formElement($flattened, $task_item, $zebra);
    }

No idea why it thinks it needs to reverse the order "to get the correct order"...

Works perfectly after removing the array_reverse().

Berdir’s picture

Assigned: CTaPByK » Berdir

Working a bit on this.

Berdir’s picture

Per discussion between me and miro, refactored the sort to directly use the entity form display and not introduce #weight. Based on how it works now, it would be weird to have that when it's not actually used.

Also removed the bogus array_reverse(), which is actually the main problem here. Looking into adding test coverage for that now.

Berdir’s picture

Ok, this should do, added similar test coverage as for the content source to the local translator. I like the approach, but simplified it a bit to look for the actual element directly in the xpath instead of going for the children then in PHP.

Final test only patch with both tests, interdiff and complete patch attached.

The last submitted patch, 35: consider-field-sequences-35-test-only.patch, failed testing.

  • Berdir committed d767bdd on 8.x-1.x
    Issue #1971646 by CTaPByK, Berdir: Core and all Source plugins should...
Berdir’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.