Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / modules / payment / src / Form / CustomFormsPaymentMethodForm.php
Size: Mime:
<?php

namespace Drupal\custom_forms_payment\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\custom_forms_payment\CustomFormsPaymentMethodManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * custom_forms_payment_method form.
 *
 * @property \Drupal\custom_forms_payment\CustomFormsPaymentMethodInterface $entity
 */
class CustomFormsPaymentMethodForm extends EntityForm {

  /**
   * The custom forms payment method manager.
   *
   * @var \Drupal\custom_forms_payment\CustomFormsPaymentMethodManager $paymentMethodManager
   */
  protected $paymentMethodManager;

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

  /**
   * {@inheritdoc}
   */
  public function __construct(CustomFormsPaymentMethodManager $paymentMethodManager) {
    $this->paymentMethodManager = $paymentMethodManager;
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $attributes = \Drupal::request()->attributes;
    $form = parent::form($form, $form_state);
    /** @var \Drupal\custom_forms_payment\Entity\CustomFormsPaymentMethod $payment_method */
    $payment_method = $attributes->get('custom_forms_payment_method', NULL);


    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $this->entity->label(),
      '#description' => $this->t('Label for the payment method.'),
      '#required' => TRUE,
    ];

    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $this->entity->id(),
      '#machine_name' => [
        'exists' => '\Drupal\custom_forms_payment\Entity\CustomFormsPaymentMethod::load',
      ],
      '#disabled' => !$this->entity->isNew(),
    ];

    $form['plugin'] = [
      '#type' => 'select',
      '#title' => $this->t('Payment method type'),
      '#description' => $this->t('The payment method type used for this payment method.'),
      '#required' => TRUE,
      '#default_value' => $this->entity->get('plugin'),
    ];

    $plugin_definitions = $this->paymentMethodManager->getDefinitions();
    foreach ($plugin_definitions as $id => $type) {
      $form['plugin']['#options'][$id] = $type['label'];
    }

    if ($payment_method !== NULL && $payment_method->getPaymentPluginId()) {
      /** @var \Drupal\custom_forms_payment\Plugin\CustomForms\PaymentMethod\CustomFormsPaymentMethodInterface $plugin */
      $plugin = $payment_method->getPaymentPlugin();

      if (!empty($plugin)) {
        $plugin->buildForm($form, $form_state);
      }
    }

    $form['test_mode'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Test mode'),
      '#default_value' => $this->entity->get('test_mode'),
    ];

    $form['status'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enabled'),
      '#default_value' => $this->entity->status(),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $values = $form_state->cleanValues()->getValues();

    $this->entity->set('plugin', $values['plugin']);
    $this->entity->set('test_mode', $values['test_mode']);

    // If any settings were defined, we make sure to save them too.
    if (!empty($values['settings'])) {
      $this->entity->set('settings', $values['settings']);
    }

    $result = parent::save($form, $form_state);
    $message_args = ['%label' => $this->entity->label()];
    $message = $result == SAVED_NEW
      ? $this->t('Created new payment method %label.', $message_args)
      : $this->t('Updated payment method %label.', $message_args);
    $this->messenger()->addStatus($message);
    $form_state->setRedirectUrl($this->entity->toUrl('collection'));
    return $result;
  }

}