After using Drupal Commerce with only the default product and default variation types.
Now I have decided to use multiple variants for products, by using the add-to-cart variant title select list.
So, in the default variant settings, I unchecked the 'Generate variation titles based on attribute values'.
But in the variant edit form, the title textbox is still hidden?
If I created a new product and a new variation type and unchecked the 'Generate variation titles based on attribute values', and create new product, then the title field is shown.
but if I try to create new product of the old product type, the title field will not be shown!
in commerce\modules\product\src\Entity\ProductVariation.php I found this code which seems relevant to the problem, but I couldn't do anything with.
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
$fields = [];
/** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type */
$variation_type = ProductVariationType::load($bundle);
// $variation_type could be NULL if the method is invoked during uninstall.
if ($variation_type && $variation_type->shouldGenerateTitle()) {
// The title is always generated, the field needs to be hidden.
// The widget is hidden in commerce_product_field_widget_form_alter()
// since setDisplayOptions() can't affect existing form displays.
$fields['title'] = clone $base_field_definitions['title'];
$fields['title']->setRequired(FALSE);
$fields['title']->setDisplayConfigurable('form', FALSE);
}
return $fields;
}I guess after title field has been created, the 'Generate variation titles based on attribute values' checkbox will not set the title field to be shown again!?
| Comment | File | Size | Author |
|---|---|---|---|
| #27 | 2995708-27.patch | 13.91 KB | mglaman |
| #26 | Screen Shot 2019-04-29 at 4.51.05 PM.png | 17.7 KB | mglaman |
| #23 | 2995708-23.patch | 12.86 KB | mglaman |
| #18 | 2995708-18-experimental-fix.patch | 9.19 KB | mglaman |
| #17 | 2995708-17.patch | 8.44 KB | mglaman |
Issue fork commerce-2995708
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
Wisamx commentedComment #3
Wisamx commentedComment #4
Wisamx commentedI could finally write this code:
It seems working and I could modify the Title field definition.
But I almost understand what I have done and I don't know if its a reliable code!?
Comment #5
Wisamx commentedComment #6
mglamanI think the problem is that we don't rebuild field caches if
generateTitlechanges.We should add a cache clear call in \Drupal\commerce_product\Form\ProductVariationTypeForm::save.
ProductType form does it via
$this->entityFieldManager->clearCachedFieldDefinitions();Can you check if a cache clear fixes the bug? If so, just need to add this (and a test!)
Comment #7
Wisamx commentedClearing drupal cache does't solve the problem.
The only way i could solve the problem is by modifying the field definition manually as i previously mentioned.
The solution i guess is that after generateTitle changes,title filed definition should be updated to hide or show title input box.
Comment #8
mglamanHere's a quick test I wrote. We'll want to test it more.
This should pass, showing the field definitions _should_ update on cache clear. But we need a Functional test against the form as well. This was a quick test before I catch my flight home.
Comment #9
Wisamx commentedIn the commerce\modules\product\src\Entity\ProductVariation.php there's this function:
and the whole problem is because the condition $variation_type->shouldGenerateTitle() doesn't change neither after changing the 'Generate variation titles based on attribute values' option nor clearing the site cache.
Comment #10
mglamanThe kernel test proves that this works as expected. However, it looks like we need a Functional test, because when submitting via the form it is not working as expected.
Comment #11
Wisamx commentedI still didn't learn about test and how to use them.
but I think $variation_type->setGenerateTitle sould be called when submitting the form.
but when searching for setGenerateTitle calls, I only get this list:
It's only called in tests.
but I miss the real setGenerateTitle call when submitting the product variation type editing form, and actually don't know where it should be called
Comment #12
mglamanBecause we don't need to. As the test shows, what needs to be done is a rebuild of bundle field definitions. See ProductVariationTypeForm (https://github.com/drupalcommerce/commerce/blob/8.x-2.x/modules/product/...)
WHICH! Should have a submitForm method that executes
$entity_field_manager->clearCachedFieldDefinitions();.So a Functional test would fail.
Comment #13
creatile commentedHello
I have the same problem
I unchecked the 'Generate variation titles based on attribute values'.
But in the variation edit form, the title input is still hidden
I tried #4 solution
And now the title input appears !
Comment #14
Wisamx commentedHello @creatile!
Yes, I am now using this solution (#4) until the bug been fixed.
It seems to be working.
Comment #15
bojanz commentedChanging status, there is no patch here.
We definitely need a failing test.
Comment #16
devbanana commentedI'm working on this issue as I'm experiencing the same bug. Basically if I have a variation type with generate title enabled, but then disable it, the variation title still doesn't appear.
I wrote a test that is exactly what I do in the UI, but it's not failing. If you can see anything I might be missing, that'd be helpful.
Also, I think the issue is because of Drupal\commerce_product\Entity\ProductVariation::bundleFieldDefinitions(). So I agree part of the solution is to clear the cache. But this doesn't work as expected, as it's not calling this method:
It never called bundleFieldDefinitions for some reason. drupal_flush_all_caches() does call it, but that seems way overkill.
Ideas appreciated.
Comment #17
mglamanHere is a reroll of #16 that is a little more explicit. Same results Cannot get this to fail. I almost wonder if we would need to write a Nightwatch.js test. The API is running Drupal and running the test - the test runner is interacting with the container and database. I don't know if there is something funky happening.
Comment #18
mglamanHere's a patch which includes the one-liner to bust bundle definitions. No injection, just for someone to test with the cache clear on form submit.
Comment #19
oomphinc commentedHi @mglaman, your fix unfortunately did not resolve the issue. I'm going to keep looking into this as well. Thanks for your help :)
Comment #20
devbanana commentedSo a few additional things:
Firstly, the hook in commerce_product_field_widget_form_alter() doesn't seem to respond to whether the title is required or not. That is, if I disable the title generation option, I can verify in ProductVariation::bundleFieldDefinitions() that $fields['title']->isRequired() is true, but still in commerce_product_field_widget_form_alter(), the field is listed as not required, and so it's hidden. Any idea why this might be?
I think there are two parts to this issue:
Comment #21
mglamanMoving to needs work. Thanks, @devbanana for items 1 & 2.
I'm going to try writing a Nightwatch test for this as it allows for better testing, hopefully.
Comment #22
mglamanTrying to get a failing test.
Comment #23
mglamanWell. Everything passes with Nightwatch as well. Something weird is going on. The field is marked required, as well.
Comment #24
mglamanComment #25
mglamanI just realized that I did not test this with existing product variations. Also, need to check with translation enabled.
Comment #26
mglamanTranslations it is!
Translations create a \Drupal\Core\Field\Entity\BaseFieldOverride. We don't update these, because they're config. They are not dynamic. Therefore, they are always false.
Comment #27
mglamanI need to work on a proper test and fix, now. But this patch should have a workaround fix for the widget alter.
Comment #28
devbanana commentedHi @mglaman, thanks for the patch. We've verified it does work. But also we found that if we remove the base field override for the variation type title field, then that also causes the title to display.
Comment #29
honza pobořil commentedPatch #27 works
Comment #30
honza pobořil commentedPatch #27 still works
Comment #31
mglamanLet's bump the version to 2.x-dev :)
Comment #32
4kant commentedNeeded this today because we had to translate all variation titles (but no title field anywhere, no matter what we tried). Glad I found this issue.
Patch #27 didn´t apply to latest 2.x-dev, so I did that manually.
Works for me actually.
It would be good to have this in the future too.
Thanks!
Comment #37
jsacksick commentedCommitted the fix only for now. Thanks!!