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.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | commerce-3342331-4.patch | 2.2 KB | niklan |
| #4 | commerce-3342331-4-test-only.patch | 1.31 KB | niklan |
| #2 | commerce-3342331-2.patch | 1.13 KB | niklan |
Comments
Comment #2
niklanComment #3
niklanComment #4
niklanAttaching patch with failtest and with a test + fix.
Comment #8
jsacksick commentedCommitted, thanks!