View source
<?php
namespace Drupal\commerce_cart_redirection\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Config\ConfigManagerInterface;
class CommerceCartRedirectionConfigForm extends ConfigFormBase {
protected $entityTypeManager;
protected $configManager;
protected $entityTypeBundleInfo;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigManagerInterface $config_manager, EntityTypeBundleInfo $entity_type_bundle_info, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->configManager = $config_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
parent::__construct($config_factory);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('config.manager'), $container
->get('entity_type.bundle.info'), $container
->get('config.factory'));
}
public function getFormId() {
return 'commerce_cart_redirection_config_form';
}
protected function getEditableConfigNames() {
return [
'commerce_cart_redirection.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('commerce_cart_redirection.settings');
$bundle_options = [];
$bundles = $this->entityTypeBundleInfo
->getBundleInfo('commerce_product_variation');
foreach ($bundles as $key => $bundle) {
$bundle_options[$key] = $bundle['label'];
}
$form['product_bundles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Product Variation Bundles'),
'#description' => $this
->t('Select the product variation bundles you want to redirect to Checkout on Add To Cart'),
'#weight' => '0',
'#options' => $bundle_options,
'#default_value' => $config
->get('product_bundles') ? $config
->get('product_bundles') : [],
];
$form['negate_product_bundles'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Negate the Bundles condition'),
'#default_value' => $config
->get('negate_product_bundles') ? $config
->get('negate_product_bundles') : FALSE,
'#weight' => '0',
];
$path_options = [
'checkout' => 'Checkout',
'cart' => 'Cart',
'other' => 'Other',
];
$form['redirection_route_path'] = [
'#type' => 'radios',
'#title' => $this
->t('Select the redirection route'),
'#weight' => '10',
'#options' => $path_options,
'#default_value' => $config
->get('redirection_route_path') ?? 'checkout',
];
$form['redirection_route_path_other'] = [
'#type' => 'textfield',
'#description' => $this
->t('You can enter any url, local or remote. When entering your own you need to ensure it is valid & safe, there is no checking done by this module.'),
'#weight' => '15',
'#default_value' => $config
->get('redirection_route_path_other') ? $config
->get('redirection_route_path_other') : '',
'#states' => [
'visible' => [
'[name="redirection_route_path"]' => [
'value' => 'other',
],
],
'required' => [
'[name="redirection_route_path"]' => [
'value' => 'other',
],
],
],
];
$form['add_to_cart_replacement_text'] = [
'#type' => 'textfield',
'#title' => 'Replacement "Add To Cart" button text.',
'#description' => $this
->t('Any text you enter here will replace the text on the default "Add to cart" button for Variations that will be redirected by this module.<br>Leave empty to use the default text.'),
'#weight' => '20',
'#default_value' => $config
->get('add_to_cart_replacement_text') ? $config
->get('add_to_cart_replacement_text') : '',
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$values = $form_state
->getValues();
$this
->config('commerce_cart_redirection.settings')
->set('product_bundles', $values['product_bundles'])
->set('negate_product_bundles', $values['negate_product_bundles'])
->set('redirection_route_path', $values['redirection_route_path'])
->set('redirection_route_path_other', $values['redirection_route_path_other'])
->set('add_to_cart_replacement_text', $values['add_to_cart_replacement_text'])
->save();
}
}