uc_gc_client-8.x-1.x-dev/src/Form/AdjustmentAction.php

src/Form/AdjustmentAction.php
<?php

namespace Drupal\uc_gc_client\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Defines a confirmation form for performing actions on scheduled adjustments.
 */
class AdjustmentAction extends ConfirmFormBase {
  /**
   * The action to perform on the item.
   *
   * @var string
   */
  protected $action;

  /**
   * The commerce order ID of the item to action.
   *
   * @var int
   */
  protected $ucid;

  /**
   * The Schedule ID of the item to action.
   *
   * @var int
   */
  protected $sid;

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

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t(
        'Are you sure you want to @action the scheduled adjustment?', [
          '@action' => $this->action,
        ]
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url(
        'uc_gc_client.payments_form', [
          'uc_order' => $this->ucid,
        ]
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t(
        '@action scheduled adjustment', [
          '@action' => ucfirst($this->action),
        ]
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelText() {
    return $this->t('Go back');
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $ucid = NULL, $action = NULL, $sid = NULL) {
    $this->action = $action;
    $this->ucid = $ucid;
    $this->sid = $sid;
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $db = \Drupal::database();
    if ($this->action == 'cancel') {
      $db->update('uc_gc_client_schedules')
        ->fields([
          'status' => 0,
        ])
        ->condition('sid', $this->sid)
        ->execute();
    }
    if ($this->action == 'delete') {
      $db->delete('uc_gc_client_schedules')
        ->condition('sid', $this->sid)
        ->execute();
    }
    $this->action == 'cancel' ? $action = 'cancelled' : $action = 'deleted';
    drupal_set_message($this->t('Scheduled adjustment @action.', [
      '@action' => $action,
    ]));

    $form_state->setRedirect(
        'uc_gc_client.payments_form', [
          'uc_order' => $this->ucid,
        ]
    );
  }

}

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

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