Field values get swapped among different entities in a view

Created on 20 July 2024, 4 months ago

This amazing module opens up new possibilities in easily generating very useful UIs, but there appears to be an issue with keeping track of all the different instances of multiple fields on a page with multiple entities. This has happened to me multiple times including when I removed a join - which I thought could be the problem.

You should be able to reproduce this if the test involves all of the following criteria:
Multiple Form FIelds per entity
Multiple entities on the page
No Joins
Edit more than one instance of 2 or more different form fields

I've had to restore from backup each time this happens, so it's a show stopper. I believe that the swapping is not consistent. In other words some fields swap from entity a to b, but then another field might swap from entity a to c. For now, I'm limiting my usage to one instance of a form field on a page or single row views which I think prevents this from happening. All the fields involved were text fields and possibly a number field, but that shouldn't matter.

πŸ› Bug report
Status

Active

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States davemaxg

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • 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.

  • πŸ‡©πŸ‡ͺ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.

Production build 0.71.5 2024