You are here

class CheckoutSettingsForm in Ubercart 8.4

Configure general checkout settings for this site.

Hierarchy

Expanded class hierarchy of CheckoutSettingsForm

1 string reference to 'CheckoutSettingsForm'
uc_cart.routing.yml in uc_cart/uc_cart.routing.yml
uc_cart/uc_cart.routing.yml

File

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

Namespace

Drupal\uc_cart\Form
View source
class CheckoutSettingsForm extends ConfigFormBase {

  /**
   * The checkout pane manager.
   *
   * @var \Drupal\uc_cart\Plugin\CheckoutPaneManager
   */
  protected $checkoutPaneManager;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a CheckoutSettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\uc_cart\Plugin\CheckoutPaneManager $checkout_pane_manager
   *   The checkout pane plugin manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(ConfigFactory $config_factory, CheckoutPaneManager $checkout_pane_manager, ModuleHandlerInterface $module_handler) {
    parent::__construct($config_factory);
    $this->checkoutPaneManager = $checkout_pane_manager;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('plugin.manager.uc_cart.checkout_pane'), $container
      ->get('module_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'uc_cart_checkout_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'uc_cart.settings',
      'uc_cart.messages',
    ];
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $cart_config = $this
      ->config('uc_cart.settings');
    $cart_config
      ->set('checkout_enabled', $form_state
      ->getValue('uc_checkout_enabled'));

    // @todo Uncomment this conditional when Rules actually works.
    // if (!$this->moduleHandler->moduleExists('rules')) {
    $cart_config
      ->set('checkout_email_customer', $form_state
      ->getValue('uc_checkout_email_customer'))
      ->set('checkout_email_admin', $form_state
      ->getValue('uc_checkout_email_admin'));

    //}
    $cart_config
      ->set('checkout_anonymous', $form_state
      ->getValue('uc_checkout_anonymous'))
      ->set('mail_existing', $form_state
      ->getValue('uc_cart_mail_existing'))
      ->set('email_validation', $form_state
      ->getValue('uc_cart_email_validation'))
      ->set('new_account_name', $form_state
      ->getValue('uc_cart_new_account_name'))
      ->set('new_account_password', $form_state
      ->getValue('uc_cart_new_account_password'))
      ->set('new_customer_email', $form_state
      ->getValue('uc_new_customer_email'))
      ->set('new_customer_login', $form_state
      ->getValue('uc_new_customer_login'))
      ->set('new_customer_status_active', $form_state
      ->getValue('uc_new_customer_status_active'))
      ->set('panes', $form_state
      ->getValue('panes'))
      ->save();
    $this
      ->config('uc_cart.messages')
      ->set('logged_in', $form_state
      ->getValue('uc_msg_order_logged_in'))
      ->set('existing_user', $form_state
      ->getValue('uc_msg_order_existing_user'))
      ->set('new_user', $form_state
      ->getValue('uc_msg_order_new_user'))
      ->set('new_user_logged_in', $form_state
      ->getValue('uc_msg_order_new_user_logged_in'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CheckoutSettingsForm::$checkoutPaneManager protected property The checkout pane manager.
CheckoutSettingsForm::$moduleHandler protected property The module handler.
CheckoutSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
CheckoutSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
CheckoutSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
CheckoutSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
CheckoutSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
CheckoutSettingsForm::__construct public function Constructs a CheckoutSettingsForm object. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.