Describe your request
I have a custom module with a hook_install that creates a product variation that includes a title. When I do an config import that installs both the custom module and commerce_product I get a failure:
Error: Call to a member function shouldGenerateTitle() on null in /var/www/html/docroot/modules/contrib/commerce/modules/product/src/Entity/ProductVariation.php on line 378 #0 /var/www/html/docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php(562): Drupal\commerce_product\Entity\ProductVariation->preSave(Object(Drupal\commerce_product\ProductVariationStorage))
I guess the "default" product variation type that I am using to create my entity on hook_install is not available yet.
Should the code check the variation type exists?
Something like this
if ($variation_type && $variation_type->shouldGenerateTitle()) {
Or should developers take care of these cases by ensuring the variation type is available before referencing it?
Comments
Comment #2
rodrigoaguileraComment #3
jsacksick commentedYou can't really open a bug report on a bug that's caused by custom code? Maybe a support request? You're probably simply not setting the variation bundle?
Comment #4
jsacksick commentedThis is... for sure.
Comment #5
rodrigoaguileraSorry, the category was indeed wrong.
I do set the bundle to be "default".
The variation type "default" is not available when the custom hook_install runs although the commerce_product module is installed. I guess it comes later in the process of the whole import (or maybe some cache needs to be refreshed forcibly).
I wonder if a code change like the one from the patch in the linked issue is reasonable. It solved my problem at least.
https://www.drupal.org/files/issues/2684429-fix-for-shouldGenerateTitle-...
Comment #6
jsacksick commentedTo me this isn't really a good change as the problem is due to your code creating the variation too early. This is happening on product variation presave, saving an entity with a bundle that doesn't exist is wrong.
Comment #7
jsacksick commentedMarking this as "won't fix", because somebody else doing the same wouldn't notice that the variation type doesn't exist yet meaning the title might not be generated properly (among other things).