You are here

class CommerceCartRedirectionConfigForm in Commerce Cart Redirection 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/CommerceCartRedirectionConfigForm.php \Drupal\commerce_cart_redirection\Form\CommerceCartRedirectionConfigForm

Class CommerceCartRedirectionConfigForm.

Hierarchy

Expanded class hierarchy of CommerceCartRedirectionConfigForm

1 string reference to 'CommerceCartRedirectionConfigForm'
commerce_cart_redirection.routing.yml in ./commerce_cart_redirection.routing.yml
commerce_cart_redirection.routing.yml

File

src/Form/CommerceCartRedirectionConfigForm.php, line 16

Namespace

Drupal\commerce_cart_redirection\Form
View source
class CommerceCartRedirectionConfigForm extends ConfigFormBase {

  /**
   * Drupal\Core\Entity\EntityTypeManagerInterface definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Drupal\Core\Config\ConfigManagerInterface definition.
   *
   * @var \Drupal\Core\Config\ConfigManagerInterface
   */
  protected $configManager;

  /**
   * Drupal\Core\Entity\EntityTypeBundleInfo.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfo
   */
  protected $entityTypeBundleInfo;

  /**
   * CommerceCartRedirectionConfigForm constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   EntityTypeManagerInterface.
   * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
   *   ConfigManagerInterface.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfo $entity_type_bundle_info
   *   EntityTypeBundleInfo.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   */
  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);
  }

  /**
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *
   * @return \Drupal\commerce_cart_redirection\Form\CommerceCartRedirectionConfigForm|\Drupal\Core\Form\FormBase
   */
  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'));
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // @NOTE due to an error in using commerce_product bundles in place of
    // commerce_product_variation bundles the config values are now misnamed
    // and should be fixed to be product_variation_bundles etc at some point.
    // Load all bundle types for commerce_product_variation.
    // @TODO This means the redirect won't work for entities that implement
    // purchasable but aren't commerce_product_variation(s) Fix?
    $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);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // If 'other' has been selected then we need to ensure that the redirection path was filled out.
    parent::validateForm($form, $form_state);
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceCartRedirectionConfigForm::$configManager protected property Drupal\Core\Config\ConfigManagerInterface definition.
CommerceCartRedirectionConfigForm::$entityTypeBundleInfo protected property Drupal\Core\Entity\EntityTypeBundleInfo.
CommerceCartRedirectionConfigForm::$entityTypeManager protected property Drupal\Core\Entity\EntityTypeManagerInterface definition.
CommerceCartRedirectionConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
CommerceCartRedirectionConfigForm::create public static function Overrides ConfigFormBase::create
CommerceCartRedirectionConfigForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
CommerceCartRedirectionConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
CommerceCartRedirectionConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
CommerceCartRedirectionConfigForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
CommerceCartRedirectionConfigForm::__construct public function CommerceCartRedirectionConfigForm constructor. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
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. 3
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.
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.
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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. 4
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.