You are here

public function CheckoutSettingsForm::buildForm in Ubercart 8.4

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 ConfigFormBase::buildForm

File

uc_cart/src/Form/CheckoutSettingsForm.php, line 79

Class

CheckoutSettingsForm
Configure general checkout settings for this site.

Namespace

Drupal\uc_cart\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $cart_config = $this
    ->config('uc_cart.settings');
  $messages = $this
    ->config('uc_cart.messages');
  $form['checkout-settings'] = [
    '#type' => 'vertical_tabs',
    '#attached' => [
      'library' => [
        'uc_cart/uc_cart.admin.scripts',
      ],
    ],
  ];
  $form['checkout'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Basic settings'),
    '#group' => 'checkout-settings',
    '#weight' => -10,
  ];
  $form['checkout']['uc_checkout_enabled'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable checkout.'),
    '#description' => $this
      ->t('Disable this to use only third party checkout services, such as PayPal Express Checkout.'),
    '#default_value' => $cart_config
      ->get('checkout_enabled'),
  ];

  // @todo Uncomment this conditional when Rules actually works.
  // if (!$this->moduleHandler->moduleExists('rules')) {
  $form['checkout']['uc_checkout_email_customer'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Send e-mail invoice to customer after checkout.'),
    '#default_value' => $cart_config
      ->get('checkout_email_customer'),
  ];
  $form['checkout']['uc_checkout_email_admin'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Send e-mail order notification to admin after checkout.'),
    '#default_value' => $cart_config
      ->get('checkout_email_admin'),
  ];

  //}
  $form['anonymous'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Anonymous checkout'),
    '#group' => 'checkout-settings',
    '#weight' => -5,
  ];
  $form['anonymous']['uc_checkout_anonymous'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable anonymous checkout.'),
    '#description' => $this
      ->t('Disable this to force users to log in before the checkout page.'),
    '#default_value' => $cart_config
      ->get('checkout_anonymous'),
  ];
  $anon_state = [
    'visible' => [
      'input[name="uc_checkout_anonymous"]' => [
        'checked' => TRUE,
      ],
    ],
  ];
  $form['anonymous']['uc_cart_mail_existing'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t("Allow anonymous customers to use an existing account's email address."),
    '#default_value' => $cart_config
      ->get('mail_existing'),
    '#description' => $this
      ->t('If enabled, orders will be attached to the account matching the email address. If disabled, anonymous users using a registered email address must log in or use a different email address.'),
    '#states' => $anon_state,
  ];
  $form['anonymous']['uc_cart_email_validation'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Require e-mail confirmation for anonymous customers.'),
    '#default_value' => $cart_config
      ->get('email_validation'),
    '#states' => $anon_state,
  ];
  $form['anonymous']['uc_cart_new_account_name'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow new customers to specify a username.'),
    '#default_value' => $cart_config
      ->get('new_account_name'),
    '#states' => $anon_state,
  ];
  $form['anonymous']['uc_cart_new_account_password'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow new customers to specify a password.'),
    '#default_value' => $cart_config
      ->get('new_account_password'),
    '#states' => $anon_state,
  ];
  $form['anonymous']['uc_new_customer_email'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Send new customers a separate e-mail with their account details.'),
    '#default_value' => $cart_config
      ->get('new_customer_email'),
    '#states' => $anon_state,
  ];
  $form['anonymous']['uc_new_customer_login'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Log in new customers after checkout.'),
    '#default_value' => $cart_config
      ->get('new_customer_login'),
    '#states' => $anon_state,
  ];
  $form['anonymous']['uc_new_customer_status_active'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Set new customer accounts to active.'),
    '#description' => $this
      ->t('Uncheck to create new accounts but make them blocked.'),
    '#default_value' => $cart_config
      ->get('new_customer_status_active'),
    '#states' => $anon_state,
  ];
  $panes = $this->checkoutPaneManager
    ->getPanes();
  $form['checkout']['panes'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Pane'),
      [
        'data' => $this
          ->t('List postion'),
        'colspan' => 2,
        'class' => [
          RESPONSIVE_PRIORITY_LOW,
        ],
      ],
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'uc-checkout-pane-weight',
      ],
    ],
  ];
  $form['checkout']['pane_settings']['#tree'] = TRUE;
  foreach ($panes as $id => $pane) {
    $form['checkout']['panes'][$id]['#attributes']['class'][] = 'draggable';
    $form['checkout']['panes'][$id]['status'] = [
      '#type' => 'checkbox',
      '#title' => $pane
        ->getTitle(),
      '#default_value' => $pane
        ->isEnabled(),
    ];
    $form['checkout']['panes'][$id]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $pane
          ->getTitle(),
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $pane
        ->getWeight(),
      '#attributes' => [
        'class' => [
          'uc-checkout-pane-weight',
        ],
      ],
    ];
    $form['checkout']['panes'][$id]['id'] = [
      '#type' => 'hidden',
      '#value' => $id,
    ];
    $form['checkout']['panes'][$id]['#weight'] = $pane
      ->getWeight();

    // @todo Move settingsForm to an interface.
    $pane_settings = $pane
      ->settingsForm();
    if (!empty($pane_settings)) {
      $form['checkout']['pane_settings'][$id] = $pane_settings + [
        '#type' => 'details',
        '#title' => $this
          ->t('@pane pane', [
          '@pane' => $pane
            ->getTitle(),
        ]),
        '#group' => 'checkout-settings',
        '#parents' => [
          'panes',
          $id,
          'settings',
        ],
      ];
    }
  }
  $form['completion_messages'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Completion messages'),
    '#group' => 'checkout-settings',
  ];
  $form['completion_messages']['uc_msg_order_logged_in'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Logged in users'),
    '#description' => $this
      ->t('Message displayed upon checkout for a user who is logged in.'),
    '#default_value' => $messages
      ->get('logged_in'),
    '#rows' => 3,
  ];
  $form['completion_messages']['uc_msg_order_existing_user'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Existing users'),
    '#description' => $this
      ->t("Message displayed upon checkout for a user who has an account but wasn't logged in."),
    '#default_value' => $messages
      ->get('existing_user'),
    '#rows' => 3,
    '#states' => $anon_state,
  ];
  $form['completion_messages']['uc_msg_order_new_user'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('New users'),
    '#description' => $this
      ->t("Message displayed upon checkout for a new user whose account was just created. You may use the special tokens !new_username for the username of a newly created account and !new_password for that account's password."),
    '#default_value' => $messages
      ->get('new_user'),
    '#rows' => 3,
    '#states' => $anon_state,
  ];
  $form['completion_messages']['uc_msg_order_new_user_logged_in'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('New logged in users'),
    '#description' => $this
      ->t('Message displayed upon checkout for a new user whose account was just created and also <em>"Login users when new customer accounts are created at checkout."</em> is set on the <a href=":url">checkout settings</a>.', [
      ':url' => Url::fromRoute('uc_cart.checkout_settings')
        ->toString(),
    ]),
    '#default_value' => $messages
      ->get('new_user_logged_in'),
    '#rows' => 3,
    '#states' => $anon_state,
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['completion_messages']['token_tree'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'uc_order',
        'site',
        'store',
      ],
    ];
  }
  return parent::buildForm($form, $form_state);
}