class SubscriptionForm 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\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\commerce_recurring\Form\SubscriptionForm
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of SubscriptionForm
File
- src/
Form/ SubscriptionForm.php, line 15
Namespace
Drupal\commerce_recurring\FormView source
class SubscriptionForm extends ContentEntityForm {
/**
* The date formatter.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a new SubscriptionForm object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter.
*/
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, DateFormatterInterface $date_formatter) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('date.formatter'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
$subscription = $this->entity;
$form = parent::form($form, $form_state);
$form['#theme'] = [
'commerce_subscription_form',
];
$form['#attached']['library'][] = 'commerce_recurring/subscription_form';
$form['advanced'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'entity-meta',
],
],
'#weight' => 99,
];
$form['meta'] = [
'#attributes' => [
'class' => [
'entity-meta__header',
],
],
'#type' => 'container',
'#group' => 'advanced',
'#weight' => -100,
'state' => [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => $subscription
->getState()
->getLabel(),
'#attributes' => [
'class' => [
'entity-meta__title',
],
],
],
];
$form['trial_date_details'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Trial dates'),
'#group' => 'advanced',
];
$form['date_details'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Dates'),
'#group' => 'advanced',
];
$field_details_mapping = [
'trial_starts' => 'trial_date_details',
'trial_ends' => 'trial_date_details',
'starts' => 'date_details',
'ends' => 'date_details',
];
foreach ($field_details_mapping as $field => $group) {
if (isset($form[$field])) {
$form[$field]['#group'] = $group;
}
}
// The trial date field should be editable only when in trial mode, or
// when the subscription is new.
if (!$subscription
->isNew() && $subscription
->getState()
->getId() != 'trial') {
$form['trial_date_details']['#access'] = FALSE;
}
if ($trial_starts = $subscription
->getTrialStartTime()) {
$trial_starts = $this->dateFormatter
->format($trial_starts, 'short');
$form['meta']['trial_starts'] = $this
->fieldAsReadOnly($this
->t('Trial starts'), $trial_starts);
}
if ($trial_ends = $subscription
->getTrialEndTime()) {
$trial_ends = $this->dateFormatter
->format($trial_ends, 'short');
$form['meta']['trial_ends'] = $this
->fieldAsReadOnly($this
->t('Trial ends'), $trial_ends);
}
// Hide the dates if the subscription is canceled and show read-only dates
// instead.
if ($subscription
->getState()
->getId() == 'canceled') {
$form['date_details']['#access'] = FALSE;
if ($starts = $subscription
->getStartTime()) {
$starts = $this->dateFormatter
->format($starts, 'short');
$form['meta']['starts'] = $this
->fieldAsReadOnly($this
->t('Starts'), $starts);
}
if ($ends = $subscription
->getEndTime()) {
$ends = $this->dateFormatter
->format($ends, 'short');
$form['meta']['ends'] = $this
->fieldAsReadOnly($this
->t('Ends'), $ends);
}
}
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$subscription = $this->entity;
assert($subscription instanceof SubscriptionInterface);
// No need for a Cancel button when creating a subscription.
if (!$subscription
->isNew()) {
$actions['delete']['#weight'] = 50;
$actions['cancel'] = [
'#type' => 'submit',
'#button_type' => 'danger',
'#value' => t('Cancel subscription'),
'#submit' => [
'::cancelSubscription',
],
'#access' => $subscription
->getState()
->getId() !== 'canceled' && $subscription
->access('cancel'),
];
}
return $actions;
}
/**
* Builds a read-only form element for a field.
*
* @param string $label
* The element label.
* @param string $value
* The element value.
*
* @return array
* The form element.
*/
protected function fieldAsReadOnly($label, $value) {
return [
'#type' => 'item',
'#wrapper_attributes' => [
'class' => [
Html::cleanCssIdentifier(strtolower($label)),
'container-inline',
],
],
'#markup' => '<h4 class="label inline">' . $label . '</h4> ' . $value,
];
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
// If the "trial_starts" value submitted is empty, but the trial start field
// on the subscription isn't, this means a default value (the current date)
// has been set by the "datetime_timestamp" widget.
if ($form_state
->getValue([
'trial_starts',
0,
'value',
]) === NULL && !empty($this->entity
->getTrialStartTime())) {
$this->entity
->set('trial_starts', NULL);
}
$this->entity
->save();
$this
->messenger()
->addMessage($this
->t('A subscription been successfully saved.'));
$form_state
->setRedirect('entity.commerce_subscription.collection');
}
/**
* Submit handler for canceling a subscription.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public function cancelSubscription(array $form, FormStateInterface $form_state) {
// Prevent ?destination from overriding our redirect.
// @todo remove after https://www.drupal.org/project/drupal/issues/2950883
$this
->getRequest()->query
->remove('destination');
$form_state
->setRedirect('entity.commerce_subscription.cancel_form', [
'commerce_subscription' => $this->entity
->id(),
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentEntityForm:: |
protected | property |
The entity being used by this form. Overrides EntityForm:: |
9 |
ContentEntityForm:: |
protected | property | The entity repository service. | |
ContentEntityForm:: |
protected | property | The entity type bundle info service. | |
ContentEntityForm:: |
protected | property | The time service. | |
ContentEntityForm:: |
protected | function | Add revision form fields if the entity enabled the UI. | |
ContentEntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityForm:: |
3 |
ContentEntityForm:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityForm:: |
|
ContentEntityForm:: |
protected | function | Flags violations for the current form. | 4 |
ContentEntityForm:: |
protected | function | Returns the bundle entity of the entity, or NULL if there is none. | |
ContentEntityForm:: |
protected | function | Gets the names of all fields edited in the form. | 4 |
ContentEntityForm:: |
public | function |
Gets the form display. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
public | function |
Gets the code identifying the active form language. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Should new revisions created on default. | |
ContentEntityForm:: |
protected | function |
Initializes the form state and the entity before the first form build. Overrides EntityForm:: |
1 |
ContentEntityForm:: |
protected | function | Initializes form language code values. | |
ContentEntityForm:: |
public | function |
Checks whether the current form language matches the entity one. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function |
Prepares the entity object before the form is built first. Overrides EntityForm:: |
1 |
ContentEntityForm:: |
public | function |
Sets the form display. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Checks whether the revision form fields should be added to the form. | |
ContentEntityForm:: |
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:: |
3 |
ContentEntityForm:: |
public | function | Updates the changed time of the entity. | |
ContentEntityForm:: |
public | function | Updates the form language to reflect any change to the entity language. | |
ContentEntityForm:: |
public | function |
Button-level validation handlers are highly discouraged for entity forms,
as they will prevent entity validation from running. If the entity is going
to be saved during the form submission, this method should be manually
invoked from the button-level… Overrides FormBase:: |
3 |
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 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 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 |
Form constructor. Overrides FormInterface:: |
10 |
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 | 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. | |
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. | |
SubscriptionForm:: |
protected | property | The date formatter. | |
SubscriptionForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
SubscriptionForm:: |
public | function | Submit handler for canceling a subscription. | |
SubscriptionForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ContentEntityForm:: |
|
SubscriptionForm:: |
protected | function | Builds a read-only form element for a field. | |
SubscriptionForm:: |
public | function |
Gets the actual form array to be built. Overrides ContentEntityForm:: |
|
SubscriptionForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
SubscriptionForm:: |
public | function |
Constructs a new SubscriptionForm object. Overrides ContentEntityForm:: |
|
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. |