commerce_gc_client-8.x-1.9/src/PluginForm/RecurrenceRulesForm.php

src/PluginForm/RecurrenceRulesForm.php
<?php

/**
 * @file
 * Contains \Drupal\commerce_gc_client\PluginForm\RecurrenceRulesForm.
 */

namespace Drupal\commerce_gc_client\PluginForm;

use Drupal\Core\Form\FormStateInterface;

/**
 * Defines the RecurrenceRulesForm class.
 */
class RecurrenceRulesForm extends RecurrenceRulesFormBase {
  
  /**
   * {@inheritdoc}.
   */
  public function getFormId() {
    return 'commerce_gc_client_recurrence_rules_form';
  }

  /**
   * {@inheritdoc}.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (isset($form_state->getStorage()['gocardless_variation_type'])) {
      $price_currency_code = $form_state->getValue('price')[0]['currency_code'];
      $gc_enabled_currencies = commerce_gc_client_enabled_currencies();
  
      if (array_search($price_currency_code, $gc_enabled_currencies) === FALSE) {
        $form_state->setErrorByName('price', t("The variation cannot be saved because you are trying to use a GoCardless enabled variation type with a currency that is not active/enabled for GoCardless."));
      }
  
      if ($list_price = $form_state->getValue(['list_price'])) {
        if ($list_price[0]['has_value']) {
          $list_price_currency_code = $form_state->getValue('list_price')[0]['value']['currency_code'];
          if (array_search($list_price_currency_code, $gc_enabled_currencies) === FALSE) {
            $form_state->setErrorByName('list_price', t("List price currency must be enabled for GoCardless."));
          }
        }
      }
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $variation = $this->variation;
    $gc = $form_state->getValue(['gc']);
    // Flatten the array.
    $data = [];
    array_walk_recursive($gc['gc_use_container'], function ($item, $key) use (&$data){$data[$key] = $item;});
    $db = \Drupal::database();
    $data = $db->merge('commerce_gc_client_variation')
      ->keys(['variation_id' => $variation->id()])
      ->fields([
        'variation_id' => $variation->id(),
        'gc_use' => $gc['gc_use'],
        'data' => serialize($data),
      ])
      ->execute();
  }
      
}

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

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