- Issue created by @davemaxg
- πΈπ°Slovakia coaston
Hello,
Let me understand you better. It is not clear. I am using this awesome module for more >4 years and i have used a lot of different use cases.
However I am not sure about your issue.
Can you provide step by step to reproduce the issue and better describe it + add some printscreens? 1. create a views page for a entity type
2. add Form field:some field in this entity type and sort by 'Created Date' desc
3.open this views page in a browser tab
4.open another tab and save a entity fo this entity type
5.go to step 3 page and save this views list form
6.value will swapped
reason is: $this->getView()->result is have a new record in first record
- π©πͺGermany mxh Offenburg
I think the originating problem of this issue is covered in π Fields are incorrectly saved if new entites are added Fixed
That issue is fixed but there was no new release created so you might still encounter the problem. You could try with the latest dev version and look whether the problem still persists. - π©πͺGermany mxh Offenburg
Turns out this issue is not yet fixed with the linked issue from #4. This is actually a bug cuased by a different place in code.
Problem is located at
Drupal\views_entity_form_field\Plugin\views\field\EntityFormField::buildEntities
:<?php protected function buildEntities(array &$form, FormStateInterface $form_state, $validate = FALSE) { $field_name = $this->definition['field_name']; // Set this value back to it's relevant entity from each row. foreach ($this->getView()->result as $row_index => $row) { // Check to make sure that this entity has a relevant field. $entity = $this->getEntity($row); if ($entity && $entity->hasField($field_name) && $this->getBundleFieldDefinition($entity->bundle())->isDisplayConfigurable('form')) { // Get current entity field values. $items = $entity->get($field_name)->filterEmptyItems(); // Extract values. $this->getPluginInstance($entity->bundle())->extractFormValues($items, $form[$this->options['id']][$row_index], $form_state); // Validate entity and add violations to field widget. if ($validate) { $violations = $items->validate(); if ($violations->count() > 0) { $this->getPluginInstance($entity->bundle())->flagErrors($items, $violations, $form[$this->options['id']][$row_index], $form_state); } } } } } ?>
Here the mapping for building the entity from submitted values is still done by the views's row index ($row_index variable). It can be related to a different entity when a new one got added in the meantime.
Additional note to reproduce this problem: It particularly appears when using an Ajax field, such as a file upload field:
1. Open a view using a file upload field as Views Entity Form Field. Upload a new file or remove an existing one. Don't click on save yet.
2. Open another tab, create new entity of same type and save it
3. Switch to the first tab (no refreshed page) and click on the save button of the Views form. - Merge request !12Issue #3462770 by davemaxg, mxh, coaston, linwuhenl: Field values get swapped... β (Open) created by mxh
- π©πͺGermany mxh Offenburg
Created a merge request containing a possible fix. It works for me locally but more testing is necessary.
- π©πͺGermany mxh Offenburg
Patch attached that is the same as the current merge request.