Problem/Motivation
If uid field on product or product_variation is overriden (e.g. core.base_field_override.commerce_product_variation.default.uid.yml) long time ago it could contain:
default_value_callback: 'Drupal\commerce_product\Entity\ProductVariation::getCurrentUserId'
But getCurrentUserId is deprecated now and removed in current Commerce, so on entity form it prints:
Warning: call_user_func() expects parameter 1 to be a valid callback, class 'Drupal\commerce_product\Entity\ProductVariation' does not have a method 'getCurrentUserId' in Drupal\Core\Field\FieldConfigBase->getDefaultValue() (line 397 file /app/web/core/lib/Drupal/Core/Field/FieldConfigBase.php).
And when tried to save it:
Column 'uid' cannot be null: INSERT INTO {commerce_product_variation_field_data} …
Proposed resolution
There should be update hook converting default_value_callback to getDefaultEntityOwner.
Workaround
Just change:
default_value_callback: 'Drupal\commerce_product\Entity\ProductVariation::getCurrentUserId'
to:
default_value_callback: 'Drupal\commerce_product\Entity\ProductVariation::getDefaultEntityOwner'
In both core.base_field_override.commerce_product_variation.*.uid.yml and core.base_field_override.commerce_product.*.uid.yml.
Issue fork commerce-3153399
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
jwwj commentedcommerce_product_update_8210should do this already, at least according to the code there? Did you rundrupal update:execute(Drupal console) or the Drush equivalent?Comment #3
amateescu commentedComment #4
vasikei can confirm the issue for 3.x and the solution from related core ticket
move the 3.x so we can work on it ... and then, maybe, backport ...
Comment #6
vasikeMR created ...
Comment #7
vasikeMR updated ... as i missed Product variation
Comment #8
vasikeReminder: Config files needs to be updated too ... to avoid this issues ... for good
`
Comment #9
jsacksick commentedOther post update functions are named like the following:
commerce_product_post_update_7(), so let's do commerce_product_post_update_8 for consistency.
Also the comment can probably say, Fix the "uid" default_value callback for product & variations.
Comment #10
vasike@jsacksick update MR
- renamed to commerce_product_post_update_8()
- add your comment suggestion.
- "remove code duplicate"
Comment #12
vensiresI have merged commits from master 3.0.x branch. Caution though for anyone who used the MR in the past! My fix changes
commerce_product_post_update_8()tocommerce_product_post_update_9()becausecommerce_product_post_update_8()was already used in a later commit to fix #3173426: Layout builder does not work when there is a product attribute without values.PS: For anyone coming here due to a
Column 'uid' cannot be null: INSERT INTO {commerce_product_variation_field_data}error, check if the Author field is displayed in the variation form. If it is, hide it. It seems to cause issues when creating the variation from Inline Entity Form, like when editing the variation from inside a product's add/edit form.