View source
<?php
namespace Drupal\payment_form\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class PaymentForm extends FormatterBase implements ContainerFactoryPluginInterface {
protected $entityFormBuilder;
protected $paymentLineItemManager;
protected $paymentStorage;
protected $requestStack;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, RequestStack $request_stack, EntityFormBuilderInterface $entity_form_builder, EntityStorageInterface $payment_storage, PaymentLineItemManagerInterface $payment_line_item_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->entityFormBuilder = $entity_form_builder;
$this->paymentLineItemManager = $payment_line_item_manager;
$this->paymentStorage = $payment_storage;
$this->requestStack = $request_stack;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('request_stack'), $container
->get('entity.form_builder'), $container
->get('entity_type.manager')
->getStorage('payment'), $container
->get('plugin.manager.payment.line_item'));
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$entity_type_id = $items
->getEntity()
->getEntityTypeId();
$bundle = $items
->getEntity()
->bundle();
$field_name = $this->fieldDefinition
->getName();
$payment = $this->paymentStorage
->create([
'bundle' => 'payment_form',
]);
$payment
->setCurrencyCode($this->fieldDefinition
->getSetting('currency_code'));
$payment_type = $payment
->getPaymentType();
$payment_type
->setDestinationUrl($this->requestStack
->getCurrentRequest()
->getUri());
$payment_type
->setEntityTypeId($entity_type_id);
$payment_type
->setBundle($bundle);
$payment_type
->setFieldName($field_name);
foreach ($items as $item) {
$plugin_id = $item
->get('plugin_id')
->getValue();
if ($plugin_id) {
$payment
->setLineItem($this->paymentLineItemManager
->createInstance($plugin_id, $item
->get('plugin_configuration')
->getValue()));
}
}
return $this->entityFormBuilder
->getForm($payment, 'payment_form');
}
}