You are here

public function BillingScheduleForm::buildForm in Commerce Recurring Framework 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/BillingScheduleForm.php, line 227

Class

BillingScheduleForm

Namespace

Drupal\commerce_recurring\Form

Code

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