Describe your bug or feature request.

Seems like this #2684429: shouldGenerateTitle() error during migration happened again.

\Drupal\commerce_product\Entity\ProductVariation::preSave have this code:

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    /** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type */
    $variation_type = $this->entityTypeManager()
      ->getStorage('commerce_product_variation_type')
      ->load($this->bundle());

    if ($variation_type->shouldGenerateTitle()) {
      $title = $this->generateTitle();
      $this->setTitle($title);
    }
  }

This will fail during migration where stub product variation created via migrate_lookup inside sub_process:

  # This is an 'entity_reference' field'.
  field_with_variation_references:
    plugin: sub_process
    # E.g. value:
    # @code
    # [
    #  ['product_id' => 1, 'quantity' => 1],
    #  ['product_id' => 2, 'quantity' => 2],
    # ]
    # @encode
    source: source_array
    process:
      target_id:
        plugin: migration_lookup
        migration: product_variations
        source: vid
      quantity: quantity

This problem in migration_lookup inside sub_process. At this point the reference most likely not created yet, in that case migration_lookup will create a stub entity. Considering that the plugin doesn't have any other information about the referenced product (like its type) it creates a stub entity with default bundle if none is provided: commerce_product_variation (same as entity type id - this is fallback). Then it tries to save it and… boom:

Error: Call to a member function shouldGenerateTitle() on null in /var/www/html/web/modules/contrib/commerce/modules/product/src/Entity/ProductVariation.php on line 378
#0 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(528): Drupal\commerce_product\Entity\ProductVariation->preSave()
#1 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(756): Drupal\Core\Entity\EntityStorageBase->doPreSave()

This is because $variation_type is NULL, there is no bundle commerce_product_variation if you not create one.

Proposed solution

Check that $variation_type is actually loaded bundle, if not, do nothing about title.

Comments

Niklan created an issue. See original summary.

niklan’s picture

Status: Active » Needs review
StatusFileSize
new1.13 KB
niklan’s picture

Issue summary: View changes
niklan’s picture

StatusFileSize
new1.31 KB
new2.2 KB

Attaching patch with failtest and with a test + fix.

The last submitted patch, 4: commerce-3342331-4-test-only.patch, failed testing. View results

  • jsacksick committed ca4ddd18 on 8.x-2.x authored by Niklan
    Issue #3342331 by Niklan: shouldGenerateTitle() on NULL during migration...

  • jsacksick committed 5105a94d on 3.0.x authored by Niklan
    Issue #3342331 by Niklan: shouldGenerateTitle() on NULL during migration...
jsacksick’s picture

Status: Needs review » Fixed

Committed, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.