Describe your bug or feature request.

If you want to alter the form of a checkout pane, you must implement a form alter for the entire checkout form and check if your pane exists. This is a bit messy. Normally I would just swap a checkout pane plugin's class and be done with it. But this might be a good developer experience additional.

Proposal:

Inject the module_handler service into CheckoutFlowWithPanesBase class and invoke an alter called checkout_pane_form and checkout_pane_PANE_ID_form. The checkout flow is passed as context.

In \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowWithPanesBase::buildForm:

      $form[$pane_id] = $pane->buildPaneForm($form[$pane_id], $form_state, $form);
      \Drupal::moduleHandler()->alter([
        'commerce_checkout_pane_form',
        "commerce_checkout_pane_{$pane_id}_form",
      ], $form[$pane_id], $this);

Add commerce_checkout.api.php to document the hook.

hook_commerce_checkout_pane_form_alter(array &$pane_form, CheckoutFlowWithPanesInterface $checkout);
hook_commerce_checkout_pane_PANE_ID_form_alter(array &$pane_form, CheckoutFlowWithPanesInterface $checkout);

Issue fork commerce-3201864

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

mglaman created an issue. See original summary.

mglaman’s picture

Issue summary: View changes
strykaizer’s picture

Status: Active » Needs review
StatusFileSize
new2.89 KB

Patch attached.

I changed to arguments to include pane, form_state and complete_form (instead of suggested "$checkout", which I think you can retrieve from the form_state object if needed).

strykaizer’s picture

StatusFileSize
new2.9 KB

Small c/p fix

gravisrs’s picture

Hi,

You sure you introduced
hook_commerce_checkout_pane_form_PLUGIN_ID_alter()
or is it
hook_commerce_checkout_pane_PLUGIN_ID_form_alter()?

Looks like information added to .api.php differs from actual hook name

strykaizer’s picture

StatusFileSize
new2.89 KB
new2.62 KB

@gravisrs: seems like you are right.

I adjusted the patch to be similar to how existing commerce hooks (e.g commerce_inline_form_alter) are structred.

New patch attached

Hooks are:

hook_commerce_checkout_pane_form_alter(array &$pane_form, \Drupal\Core\Form\FormStateInterface $form_state, array &$complete_form);
hook_commerce_checkout_pane_form_PANE_ID_alter(array &$pane_form, \Drupal\Core\Form\FormStateInterface $form_state, array &$complete_form);

jsacksick’s picture

Status: Needs review » Needs work

hm... Probably a nice DX improvement.

A few comments though:

  1. We aren't firing the alter hooks for the sidebar panes
  2. We aren't injecting the module handler... We shouldn't use \Drupal:: when dependency injection is possible.
  3. Also, swapping the checkout pane plugin is still an option.

dinazaur made their first commit to this issue’s fork.

dinazaur’s picture

Status: Needs work » Needs review
StatusFileSize
new5.04 KB

Fixed 1 and 2 points from #7.

> 3. Also, swapping the checkout pane plugin is still an option.

Yes, it is. But sometimes it's overkill when all you need is to simply change some labels.

introfini’s picture

Very useful patch. Thanks!

anybody’s picture

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

Just ran into this, would still make a lot of sense to finish this. #10 looks like it needs maintainer feedback for finishing?

omarlopesino made their first commit to this issue’s fork.