arch-8.x-1.x-dev/modules/cart/src/Form/CartConfigForm.php

modules/cart/src/Form/CartConfigForm.php
<?php

namespace Drupal\arch_cart\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Routing\RequestContext;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides settings for arch_cart module.
 */
class CartConfigForm extends ConfigFormBase {

  public function __construct(
    ConfigFactoryInterface $config_factory,
    protected PathValidatorInterface $pathValidator,
    protected RequestContext $requestContext,
    protected TypedConfigManagerInterface $typedConfigManager,
  ) {
    parent::__construct(
      $config_factory,
      $this->typedConfigManager,
    );
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('path.validator'),
      $container->get('router.request_context'),
      $container->get('config.typed'),
    );
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('arch_cart.settings');

    $form['arch_cart']['combine_items'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Combine items', [], ['context' => 'arch_cart_settings']),
      '#default_value' => $config->get('combine_items'),
      '#description' => $this->t('Combine same products within cart on add to cart event.', [], ['context' => 'arch_cart_settings']),
    ];

    $form['arch_cart']['ajax_addtocart'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Use ajax on Add to cart form', [], ['context' => 'arch_cart_settings']),
      '#default_value' => $config->get('ajax_addtocart'),
      '#description' => $this->t('Enable or disable ajaxified Add to cart form.', [], ['context' => 'arch_cart_settings']),
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('arch_cart.settings')
      ->set('combine_items', (bool) $form_state->getValue('combine_items'))
      ->set('ajax_addtocart', (bool) $form_state->getValue('ajax_addtocart'))
      ->save();

    parent::submitForm($form, $form_state);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc