mutual_credit-5.0.x-dev/src/Form/TransactionForm.php

src/Form/TransactionForm.php
<?php

namespace Drupal\mcapi\Form;

use Drupal\mcapi\Entity\Transaction;
use Drupal\mcapi\Entity\Storage\WalletStorage;
use Drupal\user\Entity\User;
use Drupal\Core\Logger\LoggerChannel;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxy;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Form for 3rdParty transactions, and base form for 1st party transactions.
 */
class TransactionForm extends ContentEntityForm {

  const DIR_INCOMING = 1;
  const DIR_OUTGOING = 0;

  /**
   * The tempStore service.
   *
   * @var Drupal\Core\TempStore\PrivateTempStore
   */
  protected $tempStore;

  /**
   * The request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The current user.
   *
   * @var \Drupal\user\Entity\User
   */
  protected $currentUser;

  /**
   * The logger channel.
   *
   * @var Drupal\Core\Logger\LoggerChannel;
   */
  protected $logger;

  /**
   *
   */
  protected $dir = NULL;

  /**
   * @var array
   */
  public $transactionChildren;

  /**
   *
   * @param $entity_repository
   * @param $entity_type_bundle_info
   * @param $time
   * @param PrivateTempStoreFactory $tempstoreFactory
   * @param Request $current_request
   * @param AccountProxy $current_user
   * @param LoggerChannelFactory $logger_channel
   */
  public function __construct($entity_repository, $entity_type_bundle_info, $time, PrivateTempStoreFactory $tempstoreFactory, Request $current_request, AccountProxy $current_user, LoggerChannel $logger_channel) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
    $this->tempStore = $tempstoreFactory->get('TransactionForm');
    $this->request = $current_request;
    $this->currentUser = User::load($current_user->id());
    $this->logger = $logger_channel;
  }

  /**
   * {@inheritDoc}
   *
   * @todo update to entity_type.manager
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.repository'),
      $container->get('entity_type.bundle.info'),
      $container->get('datetime.time'),
      $container->get('tempstore.private'),
      $container->get('request_stack')->getCurrentRequest(),
      $container->get('current_user'),
      $container->get('logger.channel.mcapi')
    );
  }

  // Runs under buildform()
  function init(FormStateInterface $form_state) {
    parent::init($form_state); // Sets form display
    $mode = $this->getFormDisplay($form_state)->getOriginalMode();
    if (str_contains($mode, 'bill')) {
      $this->dir = static::DIR_INCOMING;
    }
    if (str_contains($mode, 'credit')) {
      $this->dir = static::DIR_OUTGOING;
    }
  }

  /**
   * {@inheritDoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $display = $this->getFormDisplay($form_state);
    // Forms designer module has its own modes and must set the transaction direction itself.
    $my_wallet_settings = [
      'type' => 'my_wallet',
      'settings' => ['hide_one_wallet' => TRUE]
    ];
    if ($this->dir === static::DIR_INCOMING) {
      $display->setComponent('payee', $my_wallet_settings);
    }
    elseif ($this->dir === static::DIR_OUTGOING) {
      $display->setComponent('payer', $my_wallet_settings);
    }

    $display->buildForm($this->entity, $form, $form_state);
    return $form;
  }

  /**
   * {@inheritDoc}
   *
   * @note we are overriding here because this form is neither for saving nor
   * deleting and because previewing is not optional.
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    return [
      'submit' => [
        '#type' => 'submit',
        '#value' => $this->t('Preview'),
        '#submit' => ['::submitForm'], // NOT ::save
      ],
    ];
  }

  /**
   * {@inheritDoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    // Put the whole transaction in the userTempStore.
    $this->tempStore->set('mcapi_transaction', $this->entity);
    if (!$form_state->getRedirect()) {
      $form_state->setRedirect('entity.mcapi_transaction.transaction_save', ['mcapi_transaction' => 0]);
    }
  }


  /**
   * {@inheritDoc}
   */
  public function buildEntity(array $form, FormStateInterface $form_state) {
    // If the wallets aren't populated, use the first wallet of the current user.
    $firstwallet_id = WalletStorage::firstWalletOfEntity($this->currentUser);
    if (!$form_state->getValue('payee')) {
      $form_state->setValue('payee', [['target_id' => $firstwallet_id]]);
    }
    elseif (!$form_state->getValue('payee')) {
      $form_state->setValue('payer', [['target_id' => $firstwallet_id]]);
    }

    $entity = parent::buildEntity($form, $form_state);

    if (!$form_state->getValue('created')) {
      $form_state->setValue('created', $this->time->getRequestTime());
    }
    $entity->assemble(); // Optionally add children
    return $entity;
  }

  /**
   * {@inheritDoc}
   */
  public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
    // Assumes that any query params will be for the transaction.
    $props = $this->request->query->all()+ ['type' => 'default'];
    return Transaction::create($props);
  }

}

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

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