class BillingScheduleForm in Commerce Recurring Framework 8
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\commerce_recurring\Form\BillingScheduleForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of BillingScheduleForm
File
- src/
Form/ BillingScheduleForm.php, line 15
Namespace
Drupal\commerce_recurring\FormView source
class BillingScheduleForm extends EntityForm {
/**
* The billing schedule plugin manager.
*
* @var \Drupal\commerce_recurring\BillingScheduleManager
*/
protected $billingSchedulePluginManager;
/**
* The prorater plugin manager.
*
* @var \Drupal\commerce_recurring\ProraterManager
*/
protected $proraterPluginManager;
/**
* Constructs a new BillingScheduleForm object.
*
* @param \Drupal\commerce_recurring\BillingScheduleManager $billing_schedule_plugin_manager
* The billing schedule plugin manager.
* @param \Drupal\commerce_recurring\ProraterManager $prorater_manager
* The prorater plugin manager.
*/
public function __construct(BillingScheduleManager $billing_schedule_plugin_manager, ProraterManager $prorater_manager) {
$this->billingSchedulePluginManager = $billing_schedule_plugin_manager;
$this->proraterPluginManager = $prorater_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.commerce_billing_schedule'), $container
->get('plugin.manager.commerce_prorater'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\commerce_recurring\Entity\BillingScheduleInterface $billing_schedule */
$billing_schedule = $this->entity;
$plugins = array_column($this->billingSchedulePluginManager
->getDefinitions(), 'label', 'id');
asort($plugins);
$proraters = array_column($this->proraterPluginManager
->getDefinitions(), 'label', 'id');
asort($proraters);
// Use the first available plugin as the default value.
if (!$billing_schedule
->getPluginId()) {
$plugin_ids = array_keys($plugins);
$plugin = reset($plugin_ids);
$billing_schedule
->setPluginId($plugin);
}
// The form state will have a plugin value if #ajax was used.
$plugin = $form_state
->getValue('plugin', $billing_schedule
->getPluginId());
$prorater = $form_state
->getValue('prorater', $billing_schedule
->getProraterId());
// Pass the configuration only if the plugin hasn't been changed via #ajax.
$plugin_configuration = $billing_schedule
->getPluginId() == $plugin ? $billing_schedule
->getPluginConfiguration() : [];
$prorater_configuration = $billing_schedule
->getProraterId() == $prorater ? $billing_schedule
->getProraterConfiguration() : [];
$wrapper_id = Html::getUniqueId('billing-schedule-form');
$form['#tree'] = TRUE;
$form['#prefix'] = '<div id="' . $wrapper_id . '">';
$form['#suffix'] = '</div>';
$form['label'] = [
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => $billing_schedule
->label(),
'#required' => TRUE,
'#size' => 30,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $billing_schedule
->id(),
'#machine_name' => [
'exists' => [
BillingSchedule::class,
'load',
],
'source' => [
'label',
],
],
'#disabled' => !$billing_schedule
->isNew(),
];
$form['displayLabel'] = [
'#type' => 'textfield',
'#title' => t('Display label'),
'#description' => t('Used to identify the billing schedule on the frontend.'),
'#default_value' => $billing_schedule
->getDisplayLabel(),
'#required' => TRUE,
];
$form['billingType'] = [
'#type' => 'radios',
'#title' => $this
->t('Billing type'),
'#options' => [
BillingScheduleInterface::BILLING_TYPE_PREPAID => $this
->t('Prepaid'),
BillingScheduleInterface::BILLING_TYPE_POSTPAID => $this
->t('Postpaid'),
],
'#default_value' => $billing_schedule
->getBillingType(),
];
$form['plugin'] = [
'#type' => 'radios',
'#title' => $this
->t('Plugin'),
'#options' => $plugins,
'#default_value' => $plugin,
'#required' => TRUE,
'#ajax' => [
'callback' => '::ajaxRefresh',
'wrapper' => $wrapper_id,
],
];
$form['configuration'] = [
'#type' => 'commerce_plugin_configuration',
'#plugin_type' => 'commerce_billing_schedule',
'#plugin_id' => $plugin,
'#default_value' => $plugin_configuration,
];
$form['prorater'] = [
'#type' => 'radios',
'#title' => $this
->t('Prorater'),
'#description' => $this
->t('Modifies unit prices to reflect partial billing periods.'),
'#options' => $proraters,
'#default_value' => $prorater,
'#required' => TRUE,
'#ajax' => [
'callback' => '::ajaxRefresh',
'wrapper' => $wrapper_id,
],
];
$form['prorater_configuration'] = [
'#type' => 'commerce_plugin_configuration',
'#plugin_type' => 'commerce_prorater',
'#plugin_id' => $prorater,
'#default_value' => $prorater_configuration,
];
$retry_schedule = $billing_schedule
->getRetrySchedule();
$retries = range(1, 8);
$retry_labels = [
$this
->t('If the initial attempt fails, retry after'),
$this
->t('If the first retry fails, retry after'),
$this
->t('If the second retry fails, retry after'),
$this
->t('If the third retry fails, retry after'),
$this
->t('If the fourth retry fails, retry after'),
$this
->t('If the fifth retry fails, retry after'),
$this
->t('If the sixth retry fails, retry after'),
$this
->t('If the seventh retry fails, retry after'),
];
$num_retries = $form_state
->getValue([
'dunning',
'num_retries',
], count($retry_schedule));
$form['dunning'] = [
'#type' => 'details',
'#title' => $this
->t('Dunning'),
'#open' => TRUE,
];
$form['dunning']['help'] = [
'#plain_text' => $this
->t("Defines what should happen when a recurring order's payment fails."),
];
$form['dunning']['num_retries'] = [
'#type' => 'select',
'#title' => $this
->t('Number of retries'),
'#options' => array_combine($retries, $retries),
'#default_value' => $num_retries,
'#ajax' => [
'callback' => '::ajaxRefresh',
'wrapper' => $wrapper_id,
],
];
for ($i = 0; $i < $num_retries; $i++) {
$form['dunning']['retry'][$i] = [
'#type' => 'number',
'#title' => $retry_labels[$i],
'#field_suffix' => $this
->t('days'),
'#default_value' => isset($retry_schedule[$i]) ? $retry_schedule[$i] : 2,
'#min' => 1,
];
}
$form['dunning']['unpaid_subscription_state'] = [
'#type' => 'radios',
'#title' => $this
->t('After the final retry'),
'#weight' => 1000,
'#options' => [
'active' => $this
->t('Keep the subscription active'),
'canceled' => $this
->t('Cancel the subscription (non-reversible)'),
],
'#default_value' => $billing_schedule
->getUnpaidSubscriptionState(),
];
$form['status'] = [
'#type' => 'radios',
'#title' => $this
->t('Status'),
'#options' => [
FALSE => $this
->t('Disabled'),
TRUE => $this
->t('Enabled'),
],
'#default_value' => $billing_schedule
->status(),
];
return $form;
}
/**
* Ajax callback.
*/
public static function ajaxRefresh(array $form, FormStateInterface $form_state) {
return $form;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
/** @var \Drupal\commerce_recurring\Entity\BillingScheduleInterface $billing_schedule */
$billing_schedule = $this->entity;
if ($billing_schedule
->isNew()) {
return $form;
}
$is_referenced = (bool) $this->entityTypeManager
->getStorage('commerce_subscription')
->getQuery()
->accessCheck(FALSE)
->condition('billing_schedule', $billing_schedule
->id())
->count()
->execute();
if ($is_referenced) {
// Disable some fields when the billing shedule is already in use by subscriptions.
$form['billingType']['#disabled'] = TRUE;
$form['plugin']['#disabled'] = TRUE;
$form['configuration']['#disabled'] = TRUE;
$form['prorater']['#disabled'] = TRUE;
$form['prorater_configuration']['#disabled'] = TRUE;
if (empty($form_state
->getUserInput())) {
// The form is not submitted, set a message explaining why some of the
// fields are disabled.
$link = Link::createFromRoute('subscriptions page', 'entity.commerce_subscription.collection');
$this
->messenger()
->addWarning($this
->t('Some fields are disabled since the %billing_schedule billing schedule is used by subscriptions. Check the @link.', [
'%billing_schedule' => $billing_schedule
->label(),
'@link' => $link
->toString(),
]));
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$values = $form_state
->getValues();
/** @var \Drupal\commerce_recurring\Entity\BillingScheduleInterface $billing_schedule */
$billing_schedule = $this->entity;
$billing_schedule
->setPluginConfiguration($values['configuration']);
$billing_schedule
->setProraterConfiguration($values['prorater_configuration']);
$billing_schedule
->setRetrySchedule($values['dunning']['retry']);
$billing_schedule
->setUnpaidSubscriptionState($values['dunning']['unpaid_subscription_state']);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity
->save();
$this
->messenger()
->addMessage($this
->t('Saved the @label billing schedule.', [
'@label' => $this->entity
->label(),
]));
$form_state
->setRedirect('entity.commerce_billing_schedule.collection');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BillingScheduleForm:: |
protected | property | The billing schedule plugin manager. | |
BillingScheduleForm:: |
protected | property | The prorater plugin manager. | |
BillingScheduleForm:: |
public static | function | Ajax callback. | |
BillingScheduleForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
BillingScheduleForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
BillingScheduleForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
BillingScheduleForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
BillingScheduleForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
|
BillingScheduleForm:: |
public | function | Constructs a new BillingScheduleForm object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |