Skip to content

Instantly share code, notes, and snippets.

@welly
Created September 8, 2025 13:20
Show Gist options
  • Select an option

  • Save welly/2597a0c739e16cbb4566643653e6179d to your computer and use it in GitHub Desktop.

Select an option

Save welly/2597a0c739e16cbb4566643653e6179d to your computer and use it in GitHub Desktop.
Service-ising hooks
<?php
function example_webform_element_alter(array &$element, FormStateInterface $form_state, array $context) {
if (empty($element['#webform_id'])) {
return;
}
$webform_id = (string) $element['#webform_id'];
$allowed_payment_webforms = [
'donations--donation_type',
'donations--payment',
];
if (!in_array($webform_id, $allowed_payment_webforms, TRUE)) {
return;
}
$processor = \Drupal::service('example.webform_element_processor');
$processor->process($webform_id, $element, $form_state, $context);
}
<?php
namespace Drupal\example;
use Drupal\Core\Form\FormStateInterface;
/**
* Central dispatcher for per-webform element processing.
*/
final class WebformElementProcessor {
/**
* Process the element based on the webform machine ID.
*/
public function process(string $webform_id, array &$element, FormStateInterface $form_state, array $context): void {
$handlers = [
'donations--donation_type' => 'handleDonationType',
'donations--payment' => 'handlePayment',
];
$method = $handlers[$webform_id] ?? 'handleDefault';
$this->{$method}($element, $form_state, $context);
}
/**
* Handle donations--payment webform.
*/
private function handlePayment(array &$element, FormStateInterface $form_state, array $context): void {
if ($form_state->get('current_page') !== 'payment') {
return;
}
$values = $form_state->getValues();
if (($values['donation_type'] ?? NULL) === 'single' && isset($values['donation_amount'])) {
$element['#attached']['library'][] = 'centrepoint_donations/stripe_scripts';
$config = \Drupal::config('stripe.settings');
$apikeySecret = $config->get('apikey.' . $config->get('environment') . '.secret');
$apikeyPublic = $config->get('apikey.' . $config->get('environment') . '.public');
$stripe = new StripeClient($apikeySecret);
/** @var \Drupal\webform\WebformSubmissionForm $webform */
$webform = $form_state->getFormObject();
/** @var \Drupal\webform\Entity\WebformSubmission $webform_submission */
$webform_submission = $webform->getEntity();
if (!$webform_submission->id()) {
$webform_submission->save();
}
$data = $webform_submission->getData();
$address = [
'line1' => $data['address']['address_line1'],
'line2' => $data['address']['address_line2'],
'state' => $data['address']['administrative_area'],
'city' => $data['address']['locality'],
'postal_code' => $data['address']['postal_code'],
'country' => $data['address']['country_code'] == 'United Kingdom' ? 'GB' : '',
];
$params = [
'name' => $data['name']['title'] . ' ' . $data['name']['first'] . ' ' . $data['name']['last'],
'email' => $data['your_email'],
'address' => $address,
];
$customer = $stripe->customers->create($params);
$paymentIntent = $stripe->paymentIntents->create([
// Stripe wants amounts in pence, so multiplying by 100.
'amount' => $values['donation_amount'] * 100,
'currency' => 'gbp',
'payment_method_types' => ['card', 'paypal'],
'payment_method_options' => ['paypal' => ['preferred_locale' => 'en-GB']],
'customer' => $customer->id,
'description' => $values['campaign_id'],
]);
$element['#attached']['drupalSettings']['form_values'] = $values;
$element['#attached']['drupalSettings']['payment_intent'] = $paymentIntent->id;
$element['#attached']['drupalSettings']['secret'] = $paymentIntent->client_secret;
$element['#attached']['drupalSettings']['public_key'] = $apikeyPublic;
$element['#attached']['drupalSettings']['sid'] = $webform_submission->id();
}
}
/**
* Handle donations--donation-type webform.
*/
private function handleDonationType(array &$element, FormStateInterface $form_state, array $context): void {
if ($form_state->get('current_page') !== 'personal_details') {
return;
}
// Get the current request.
$request = \Drupal::request();
// Get the 'donation_type' query parameter from the URL.
$donation_type = $request->query->get('donation_type');
if (empty($donation_type) && isset($element['#wrapper_attributes']['class']) &&
is_array($element['#wrapper_attributes']['class'])) {
$key = array_search('visually-hidden', $element['#wrapper_attributes']['class']);
if ($key !== FALSE) {
unset($element['#wrapper_attributes']['class'][$key]);
// Reindex to maintain numeric keys.
$element['#wrapper_attributes']['class'] = array_values($element['#wrapper_attributes']['class']);
}
}
}
/**
* Default no-op handler.
*/
private function handleDefault(array &$element, FormStateInterface $form_state, array $context): void {
// Intentionally empty.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment