Problem
When creating a new form (through Contact Forms), the user can define the form display and view display. Both are working fine when viewing a form or a submission (using the Contact Messages module). However, when the visitor clicks on preview before submitting, the preview shows the individual form field values in an apparently random order.

Inspecting the HTML, it seems, all previewed fields are sorted by their machine name. I am not sure, how the preview is built, in MessageForm.php it says
$form['preview']['message'] = $this->entityManager->getViewBuilder('contact_message')->view($message, 'full');
but I can't find the correct implementation of that function to take a look.

I think, the correct viewBuilder is in the Contact Storage module which is currently Third-Party but seems to be on its way to become part of Drupal 8.3.x (see #2750629: Add view builder for contact module's Message entity). Then I get lost where to look next in order to change the sorting to use weights.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Paul Broon created an issue. See original summary.

Berdir’s picture

Component: entity system » contact.module

Moving to contact.module, which probably simply needs to expose preview as an extra field to fix this in contact_entity_extra_field_info()

Berdir’s picture

Status: Active » Closed (duplicate)
broon’s picture

Status: Closed (duplicate) » Active

Sorry, but I disagree with this being a duplicate. The linked issue is about the position of the preview in the form (as in having the preview as a whole above or below the form). My issue is that the individual values inside the preview are in wrong order. For example, assume you have two fields in your form: (1) Field 1, name "username", weight -10 and (2) Field 2, name "age", weight 0.

With the weight, the form will show the fields in the order [username, age]. However, if you click preview, the order will be [age, username] as the fields are sorted by machine name instead of the field's weight.

Berdir’s picture

which weight, the form or the view display weight?

View respects the *view* weights.

broon’s picture

Neither weight is respected in the preview.

By setting the form display weight, I can arrange fields within the form (working).
By setting the form display weight, I can arrange fields within the form (working, see below the order of the fields in the form: [username, age]). I also have the same order set up on the view display page.


But when the user hits the "Preview" button, the fields within the preview do not have the same order as the fields in the form, they are in order [age, username].

Berdir’s picture

That screenshot is the form display, what's your formatter configuration?

broon’s picture

FileSize
17.87 KB

Formatter configuration for what? The Preview "field" has no formatter options. Are you talking about the "Manage display" tab? It has no effect here afaik, but as said above it has the same order.

View display

Berdir’s picture

Status: Active » Needs review
FileSize
675 bytes

Yes, I'm talking about "Manage display". That's exactly what *should* affect that order, and based on your explanations I wasn't sure if that was clear to you.

Based on your feedback, tried it myself and can confirm that problem now. It's a fun conflict between the form system (which has some logic to avoid sorting elements with elements that have no children with an explicit #weight in FormBuilder::doBuildForm()) and the entity render cache process (which pre-creates empty render arrays with just a #pre_render, that then get marked as #sorted). Combined with the fact that most entity types actually have a preprocess where they copy all rendered fields into a new structure and drop any properties set on the container, like #sorted and are therefore not affected by this.

Here's a dirty fix, we probably want something deeper in the renderer, for example something that unsets #sorted if we run a #pre_render or similar function that might add/change elements and their weight.

tstoeckler’s picture

Wow, I've actually hit this before, too, but didn't realize it was due to the #sorted thing, that's amazing!

broon’s picture

Thanks for debugging the problem, sometimes it's just a single line. ;) Good job!

It works for me now but I am not deep enough into D8 core to actually RBTC this and as you said, this might have some implications on other behaviour.

Berdir’s picture

Issue tags: +Needs tests

yeah, this is not meant to be committed, we need tests and likely a very different fix.

It would actually also conflict with #2756689: Label of contact form message field is not hidden as that issue removes the whole method.

Wim Leers’s picture

Great find :)

+++ b/core/modules/contact/src/MessageViewBuilder.php
@@ -30,6 +30,7 @@ public function buildComponents(array &$build, array $entities, array $displays,
+      unset($build[0]['#sorted']);

This would need a comment for sure :)

Berdir’s picture

Status: Needs review » Needs work

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Ruuds’s picture

The patch of #9 doesn't work on 8.9.x anymore. I solved the problem by rendering the preview message as markup. It seems the form rendering interferences with the rendering of the nested contact message preview.

See the attached patch.

KapilV’s picture

Assigned: Unassigned » KapilV
KapilV’s picture

Status: Needs work » Needs review
FileSize
3.11 KB
427 bytes

fixed Custom Commands Failed.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

For tests requested in #12

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.