My team and I ran against something very odd regarding the exported configuration files of dynamic entity reference fields.

Saving the field settings form on my computer resulted in the following config export:

...
settings:
  ...
  comment_type:
    handler: 'default:comment_type'
    handler_settings: {  }
  comment:
    handler: 'default:comment'
    handler_settings: {  }
  ...
...

When my colleagues saved the same form on their computer, their config export resulted in the following:

...
settings:
  ...
  comment:
    handler: 'default:comment'
    handler_settings: {  }
  comment_type:
    handler: 'default:comment_type'
    handler_settings: {  }
  ...
...

As you can see, the ordering between comment and comment_type is different.

I decided to dig into this a little deeper and it appears \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem::defaultFieldSettings() is responsible for making sure all the entity types are added to the settings array. It basically retrieves all the entity types using \Drupal::service('entity_type.repository')->getEntityTypeLabels(), which on its turn uses \Drupal::entityTypeManager()->getDefinitions().

Entity types (at least the ones that are not dynamically added) are plugins that live in their own file (for example core/modules/comment/src/Entity/Comment.php or web/core/modules/node/src/Entity/Node.php). These files are retrieved using the \RecursiveDirectoryIterator class, which extends the \FilesystemIterator class. These iterators do not enforce any sorting in their return value - they basically return files in the same order the filesystem gave them to it. See, for example, https://www.php.net/manual/en/class.recursivedirectoryiterator.php#120971 or https://stackoverflow.com/questions/29102983/order-in-filesystemiterator....

Because the order of files (and thus entity type definitions) can vary based on your filesystem, the exported config can differ. To prevent this from happening I would like to propose to store the settings in alphabetical order. This way the order of settings is consistent across all possible file systems.

Comments

rp7 created an issue. See original summary.

rp7’s picture

Here's a possible solution. I do believe we need an update hook though, to update existing config.

ptmkenny’s picture

Status: Active » Needs review