You are here

class CommerceAutoSkuForm in Commerce AutoSKU 8.2

Class CommerceAutoSkuForm.

@property \Drupal\Core\Config\ConfigFactoryInterface config_factory @property \Drupal\Core\Entity\EntityTypeManagerInterface entity_manager @property String entity_type_parameter @property String entity_type_id @property \Drupal\commerce_autosku\CommerceAutoSkuManager auto_entity_label_manager @package Drupal\commerce_autosku\Controller

Hierarchy

Expanded class hierarchy of CommerceAutoSkuForm

File

src/Form/CommerceAutoSkuForm.php, line 28
Contains \Drupal\commerce_autosku\Controller\CommerceAutoSkuForm.

Namespace

Drupal\commerce_autosku\Form
View source
class CommerceAutoSkuForm extends FormBase {

  /**
   * The commerce autoSku generator plugin manager.
   *
   * @var \Drupal\commerce_payment\PaymentGatewayManager
   */
  protected $pluginManager;

  /**
   * The commerce autoSku generator plugin manager.
   *
   * @var ProductVariationTypeInterface
   */
  protected $entity;

  /**
   * AutoEntityLabelController constructor.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   * @param \Drupal\commerce_autosku\CommerceAutoSkuManager $plugin_manager
   */
  public function __construct(ContainerInterface $container, CommerceAutoSkuGeneratorManager $plugin_manager) {
    $this->pluginManager = $plugin_manager;
  }

  /**
   * Returns a unique string identifying the form.
   *
   * @return string
   *   The unique string identifying the form.
   */
  public function getFormId() {
    return 'commerce_autosku_settings_form';
  }

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

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

    /** @var ProductVariationTypeInterface entity */
    $this->entity = $commerce_product_variation_type;
    $configuration = $this->entity
      ->getThirdPartySettings('commerce_autosku');
    $form['mode'] = [
      '#type' => 'radios',
      '#default_value' => isset($configuration['mode']) ? $configuration['mode'] : CommerceAutoSkuManager::DISABLED,
      '#options' => CommerceAutoSkuManager::commerce_autosku_options(),
    ];
    $definition = $this->pluginManager
      ->getDefinitions();
    $plugins = array_column($definition, 'label', 'id');
    asort($plugins);

    //    $plugin = $this->getget('');
    //
    //    // Use the first available plugin as the default value.
    //    if (!$gateway->getPluginId()) {
    //      $plugin_ids = array_keys($plugins);
    //      $plugin = reset($plugin_ids);
    //      $gateway->setPluginId($plugin);
    //    }
    // The form state will have a plugin value if #ajax was used.
    //    $plugin = $form_state->getValue('plugin');
    // Pass the plugin configuration only if the plugin hasn't been changed via #ajax.
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save configuration'),
      '#button_type' => 'primary',
    ];

    // By default, render the form using system-config-form.html.twig.
    $form['#theme'] = 'system_config_form';
    if (!empty($configuration['plugin']) && !isset($plugins[$configuration['plugin']])) {
      return $form;
    }
    $wrapper_id = Html::getUniqueId('commerce-autosku-plugin-form');
    $form['plugin'] = [
      '#type' => 'radios',
      '#title' => t('Plugin'),
      '#options' => $plugins,
      '#required' => TRUE,
      '#ajax' => [
        'callback' => '::ajaxRefresh',
        'wrapper' => $wrapper_id,
      ],
    ];
    if (!empty($configuration['plugin']) && isset($plugins[$configuration['plugin']])) {
      $form['plugin']['#default_value'] = $configuration['plugin'];
      $form['configuration'] = [
        '#type' => 'commerce_plugin_configuration',
        '#plugin_type' => 'commerce_autosku_generator',
        '#plugin_id' => $configuration['plugin'],
      ];
      if (!is_null($configuration['configuration'])) {
        $form['configuration']['#default_value'] = $configuration['configuration'];
      }
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $mode = $form_state
      ->getValue('mode');
    $plugin = $form_state
      ->getValue('plugin');
    $configuration = $form_state
      ->getValue('configuration');
    $this->entity
      ->setThirdPartySetting('commerce_autosku', 'mode', $mode);
    $this->entity
      ->setThirdPartySetting('commerce_autosku', 'plugin', $plugin);
    $this->entity
      ->setThirdPartySetting('commerce_autosku', 'configuration', $configuration);
    $this->entity
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceAutoSkuForm::$entity protected property The commerce autoSku generator plugin manager.
CommerceAutoSkuForm::$pluginManager protected property The commerce autoSku generator plugin manager.
CommerceAutoSkuForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
CommerceAutoSkuForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
CommerceAutoSkuForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
CommerceAutoSkuForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
CommerceAutoSkuForm::__construct public function AutoEntityLabelController constructor.
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::config protected function Retrieves a configuration object.
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.