Bug : sample entity generator in layout builder with drupal commerce variations and referenced entities

Created on 16 April 2025, 6 days ago

Problem/Motivation

I just managed to sort out a strange bug.

In layout builder UI for a drupal commerce variation bundle type,
i am traversing referenced entities :
Commerce Variation -> Commerce product -> entity reference field -> ...

I am very surprised that the sample entity used inside layout builder seems to be in fact a real entity with an ID.
Therefore, the traversal is not working well when the real entity used (instead of a sample one) does not have a value for each and every entity reference field.

The test bellow in EntityReferencedDerivableContext::getEntities is thus not working as expected, because the entity has a real id...

if ((count($referenced_entities) === 0) && !$entity->id()) {
🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇫🇷France just_like_good_vibes PARIS

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

Merge Requests

Comments & Activities

  • Issue created by @just_like_good_vibes
  • 🇫🇷France just_like_good_vibes PARIS

    More investigations : the buggy entity ( that is supposed to be a random entity but it's not) is already there in layout builder.
    We got it from LayoutBuilderContextEntityResolver::guessLayoutBuilderEntity.

  • 🇫🇷France just_like_good_vibes PARIS

    The problem is in this code from the SampleEntityGenerator.

    $entity = $tempstore->get("$entity_type_id.$bundle_id")

    the entity in there is not the supposed random entity.

  • 🇫🇷France just_like_good_vibes PARIS

    Ok, now i think i have it .

    We have a serious bug inside our code, because we were supposing sample entities to be really sample, without an id.
    and this is false in the case of some entity reference fields, because of that code in \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::generateSampleValue

       // Select a random number of references between the last 50 referenceable
        // entities created.
        if ($referenceable = $selection_handler->getReferenceableEntities(NULL, 'CONTAINS', 50)) {
          $group = array_rand($referenceable);
          $values['target_id'] = array_rand($referenceable[$group]);
          return $values;
        }
    
  • Merge request !371Resolve #3519543 "Bug sample" → (Open) created by just_like_good_vibes
  • Pipeline finished with Canceled
    5 days ago
    Total: 400s
    #476217
  • 🇫🇷France just_like_good_vibes PARIS

    @christian, as we discussed today, here is a better implementation of sample entity generator,
    getting rid of peace of real contents in entity reference fields.
    Please note :
    - i carefully cleaned in the case of layout builder generated entity or when we generate an entity with our generator
    - we clean recursively, because yes it's required... that's why we only traverse some fields, and the configurable ones to be more precise. When i let the base fields, the traversal goes crazy everywhere... especially the author fields, picture..etc and in cascade.
    - it fixes the bug i managed to reproduce in the lab today :)

  • Pipeline finished with Failed
    5 days ago
    Total: 785s
    #476221
  • 🇫🇷France just_like_good_vibes PARIS

    the fix here is correcting the reproduction of the bug i made in lab, but after some testing, it does not solve yet my real world initial problem.
    those “bad”
    sample values are quite a big problem compared the assumptions we made. maybe we are not taking the problem in the right sense. the real problem here, is that sometimes, some sample entities do not have values in referenced entities, because those samples are not real samples, they are real entities borrowed. to cover our needs here and fill the gap, we only need random entities where they are missing. if we could identify, through a conext or whatever, that the current entity in the context is a sample, then we could fill that gap more easily than trying to patch every sample entity.

Production build 0.71.5 2024