Logic to generate a new code is not avoiding collisions and isnot encapsulated

Created on 24 September 2025, about 1 month ago

Problem/Motivation

When creating a new wishlist, the code is generated as part of the preSave() method.

That logic is trying to avoid collisions, but in the end is not using the safe code.

Here is the code:

      /** @var \Drupal\commerce_wishlist\WishlistStorageInterface $storage */
      $storage = $this->entityTypeManager()->getStorage('commerce_wishlist');
      $random = new Random();
      $code = $random->word(13);
      // Ensure code uniqueness. Collisions are rare, but possible.
      while ($storage->loadByCode($code)) {
        $code = $random->word(13);
      }
      $this->setCode($random->word(13));

Even if $code is generated, a new code that might cause a collision is set.

We found it while trying to implement a custom pattern for this code, and would be great to encapsulate this logic to extend the parent class in a safer way.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇪🇸Spain plopesc Valladolid

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024