You are here

public function SubscriptionForm::form in Commerce Recurring Framework 8

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/SubscriptionForm.php, line 57

Class

SubscriptionForm

Namespace

Drupal\commerce_recurring\Form

Code

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;
}