How can I alter bundles of commerce_order_item entity type using custom module I am trying to alter bundles of commerce_order_item -

core.entity_form_mode.commerce_order_item.add_to_cart_confirmation.yml

langcode: en
status: true
dependencies:
  module:
    - commerce_order
  enforced:
    module:
      - commerce_add_to_cart_confirmation
id: commerce_order_item.add_to_cart_confirmation
label: 'Add to Cart Confirmation'
targetEntityType: commerce_order_item
cache: true


.module file

function commerce_add_to_cart_confirmation_entity_bundle_info_alter(&amp;$bundles){
  $storage = \Drupal::entityTypeManager()-&gt;getStorage('entity_form_display');
  foreach ($bundles['commerce_order_item'] as $bundle_key =&gt; $bundle_value) {
    if($bundle_key !== 'default'){
      $form_display = $storage-&gt;load('commerce_order_item.' . $bundle_key . '.add_to_cart_confirmation');
      if (!$form_display) {
        /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
        $form_display = $storage-&gt;create([
          'targetEntityType' =&gt; 'commerce_order_item',
          'bundle' =&gt; $bundle_key,
          'mode' =&gt; 'add_to_cart_confirmation',
          'status' =&gt; TRUE,
        ]);
        // Hide the unit price by default.
        $form_display-&gt;removeComponent('unit_price');
        // Hide the quantity by default.
        $form_display-&gt;removeComponent('quantity');
        $form_display-&gt;save();
        $form_display-&gt;enable();
      }
    }
  }
}


But at $form_display-&gt;save(); is am getting below error -

Error: Call to a member function getConfigDependencyName() on null in /var/www/drupal/core/lib/Drupal/Core/Entity/EntityDisplayBase.php on line 314 #0 /var/www/drupal/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php(346): Drupal\\Core\\Entity\\EntityDisplayBase-&gt;calculateDependencies()\n#1 /var/www/drupal/core/lib/Drupal/Core/Entity/EntityDisplayBase.php(266): Drupal\\Core\\Config\\Entity\\ConfigEntityBase-&gt;preSave(Object(Drupal\\Core\\Config\\Entity\\ConfigEntityStorage), true)\n#2 /var/www/drupal/core/lib/Drupal/Core/Entity/EntityStorageBase.php(434): Drupal\\Core\\Entity\\EntityDisplayBase-&gt;preSave(Object(Drupal\\Core\\Config\\Entity\\ConfigEntityStorage))\n#3 /var/www/drupal/core/lib/Drupal/Core/Entity/EntityStorageBase.php(389): Drupal\\Core\\Entity\\EntityStorageBase-&gt;doPreSave(Object(Drupal\\Core\\Entity\\Entity\\EntityFormDisplay))\n#4 /var/www/drupal/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php(259): Drupal\\Core\\Entity\\EntityStorageBase-&gt;save(Object(Drupal\\Core\\Entity\\Entity\\EntityFormDisplay))\n#5 /var/www/drupal/core/lib/Drupal/Core/Entity/Entity.php(377): Drupal\\Core\\Config\\Entity\\ConfigEntityStorage-&gt;save(Object(Drupal\\Core\\Entity\\Entity\\EntityFormDisplay))\n#6 /var/www/drupal/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php(637): Drupal\\Core\\Entity\\Entity-&gt;save()\n#7 /var/www/drupal/modules/custom/commerce_add_to_cart_confirmation/commerce_add_to_cart_confirmation.module(116)


This is annoying because same code is running for hook_confirmation_entity_bundle_create($entity_type_id, $bundle). Please advice what should I do to save the bundle.If there is another way to achieve what I am trying to achieve, please tell me.

Thanks Guys
add_to_cart_confirmation needs to be a a defined entity form mode, that's most likely the error in your case. Provide it as default configuration in your module or check its existence or create it if necessary.

See how node defines the teaser view mode as an example, or user the register form mode.