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!?

Issue fork commerce-2995708

Command icon 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

Wisamx created an issue. See original summary.

Wisamx’s picture

Issue summary: View changes
Wisamx’s picture

Issue summary: View changes
Wisamx’s picture

I could finally write this code:

function mymodule_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  $fields = [];

  if ($entity_type->id() == 'commerce_product_variation' && $bundle == 'default' && !empty($base_field_definitions['title'])) {
    $field = $base_field_definitions['title'];
    $field->setRequired(TRUE);
    $field->setDisplayConfigurable('form', TRUE);
    $fields['title'] = $field;
  }

  return $fields;
}

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!?

Wisamx’s picture

Title: Product variant title textbox isn't shown after unchecking 'Generate variation titles based on attribute values' » Changing 'Generate variation titles based on attribute values' will not change the Title field definition
mglaman’s picture

Issue tags: +Needs tests

I think the problem is that we don't rebuild field caches if generateTitle changes.

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!)

Wisamx’s picture

Clearing 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.

mglaman’s picture

Status: Active » Needs review
StatusFileSize
new2.05 KB

Here'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.

Wisamx’s picture

In the commerce\modules\product\src\Entity\ProductVariation.php there's this function:

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;
  }

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.

mglaman’s picture

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.

The 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.

Wisamx’s picture

I 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:

  E:\wamp\www\katstores\modules\contrib\commerce\modules\log\tests\src\Kernel\CartIntegrationTest.php (1 hit)
  E:\wamp\www\katstores\modules\contrib\commerce\modules\log\tests\src\Kernel\OrderIntegrationTest.php (1 hit)
  E:\wamp\www\katstores\modules\contrib\commerce\modules\order\tests\src\Kernel\OrderReceiptTest.php (1 hit)
  E:\wamp\www\katstores\modules\contrib\commerce\modules\order\tests\src\Kernel\OrderRefreshTest.php (1 hit)
  E:\wamp\www\katstores\modules\contrib\commerce\modules\order\tests\src\Kernel\OrderTotalSummaryTest.php (1 hit)
  E:\wamp\www\katstores\modules\contrib\commerce\modules\product\tests\src\Functional\ProductVariationTitleGenerationTest.php (2 hits)
  E:\wamp\www\katstores\modules\contrib\commerce\modules\product\tests\src\Kernel\ProductMultilingualTest.php (3 hits)
  E:\wamp\www\katstores\modules\contrib\commerce\modules\product\tests\src\Kernel\ProductVariationFieldRendererTest.php (1 hit)

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

mglaman’s picture

Because 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.

creatile’s picture

Hello
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 !

Wisamx’s picture

Hello @creatile!
Yes, I am now using this solution (#4) until the bug been fixed.
It seems to be working.

bojanz’s picture

Status: Needs review » Active

Changing status, there is no patch here.

We definitely need a failing test.

devbanana’s picture

I'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:

    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

It never called bundleFieldDefinitions for some reason. drupal_flush_all_caches() does call it, but that seems way overkill.

Ideas appreciated.

mglaman’s picture

Status: Active » Needs review
Issue tags: -Needs tests
StatusFileSize
new8.44 KB

Here 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.

mglaman’s picture

StatusFileSize
new9.19 KB

Here'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.

oomphinc’s picture

Hi @mglaman, your fix unfortunately did not resolve the issue. I'm going to keep looking into this as well. Thanks for your help :)

devbanana’s picture

So 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:

  1. The cache has to be cleared (not just the field cache like in #18), in order to update the field bundle definition.
  2. But even if this is done, commerce_product_field_widget_form_alter() sees the field as not required and so hides it.
mglaman’s picture

Status: Needs review » Needs work

Moving 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.

mglaman’s picture

Assigned: Unassigned » mglaman

Trying to get a failing test.

mglaman’s picture

StatusFileSize
new12.86 KB

Well. Everything passes with Nightwatch as well. Something weird is going on. The field is marked required, as well.

mglaman’s picture

Status: Needs work » Needs review
mglaman’s picture

I just realized that I did not test this with existing product variations. Also, need to check with translation enabled.

mglaman’s picture

Issue summary: View changes
Status: Needs review » Needs work
StatusFileSize
new17.7 KB

Translations 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.

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new13.91 KB

I need to work on a proper test and fix, now. But this patch should have a workaround fix for the widget alter.

devbanana’s picture

Hi @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.

honza pobořil’s picture

Version: 8.x-2.8 » 8.x-2.16
Status: Needs review » Reviewed & tested by the community

Patch #27 works

honza pobořil’s picture

Version: 8.x-2.16 » 8.x-2.26

Patch #27 still works

mglaman’s picture

Version: 8.x-2.26 » 8.x-2.x-dev

Let's bump the version to 2.x-dev :)

4kant’s picture

Needed 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!

Thanos Ntelis made their first commit to this issue’s fork.

  • jsacksick committed 04f8dd88 on 8.x-2.x authored by mglaman
    Issue #2995708 by mglaman, devbanana, Wisamx: Changing 'Generate...

  • jsacksick committed b3bfb7aa on 3.0.x authored by mglaman
    Issue #2995708 by mglaman, devbanana, Wisamx: Changing 'Generate...
jsacksick’s picture

Status: Reviewed & tested by the community » Fixed

Committed the fix only for now. Thanks!!

Status: Fixed » Closed (fixed)

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