View source
<?php
namespace Drupal\uc_payment\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
class PaymentMethodForm extends EntityForm {
protected $entity;
protected $plugin;
public function buildForm(array $form, FormStateInterface $form_state) {
$this->plugin = $this->entity
->getPlugin();
return parent::buildForm($form, $form_state);
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$definition = $this->plugin
->getPluginDefinition();
$form['type'] = [
'#type' => 'item',
'#title' => $this
->t('Type'),
'#markup' => $definition['name'],
];
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $this->entity
->label(),
'#description' => $this
->t('The name shown to customers when they choose this payment method at checkout.'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $this->entity
->id(),
'#maxlength' => 32,
'#machine_name' => [
'exists' => '\\Drupal\\uc_payment\\Entity\\PaymentMethod::load',
],
'#disabled' => !$this->entity
->isNew(),
];
$form['settings'] = $this->plugin
->buildConfigurationForm([], $form_state);
$form['settings']['#tree'] = TRUE;
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$this->plugin
->validateConfigurationForm($form['settings'], $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->plugin
->submitConfigurationForm($form['settings'], $form_state);
}
public function save(array $form, FormStateInterface $form_state) {
$this->entity
->save();
$this
->messenger()
->addMessage($this
->t('Saved the %label payment method.', [
'%label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this->entity
->toUrl('collection'));
}
}