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);
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | 3201864-8-pane-alter-hook.patch | 5.04 KB | dinazaur |
| #6 | interdiff.txt | 2.62 KB | strykaizer |
| #6 | 3201864-6.patch | 2.89 KB | strykaizer |
Issue fork commerce-3201864
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
mglamanComment #3
strykaizerPatch 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).
Comment #4
strykaizerSmall c/p fix
Comment #5
gravisrs commentedHi,
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
Comment #6
strykaizer@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);
Comment #7
jsacksick commentedhm... Probably a nice DX improvement.
A few comments though:
\Drupal::when dependency injection is possible.Comment #10
dinazaur commentedFixed 1 and 2 points from #7.
Yes, it is. But sometimes it's overkill when all you need is to simply change some labels.
Comment #11
introfini commentedVery useful patch. Thanks!
Comment #12
anybodyJust ran into this, would still make a lot of sense to finish this. #10 looks like it needs maintainer feedback for finishing?