View source
<?php
namespace Drupal\payment_reference\Plugin\Payment\Type;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\payment\EventDispatcherInterface;
use Drupal\payment\Plugin\Payment\Type\PaymentTypeBase;
use Drupal\payment\Response\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaymentReference extends PaymentTypeBase implements ContainerFactoryPluginInterface {
protected $entityFieldManager;
protected $urlGenerator;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EventDispatcherInterface $event_dispatcher, UrlGeneratorInterface $url_generator, EntityFieldManagerInterface $entity_field_manager, TranslationInterface $string_translation) {
$configuration += $this
->defaultConfiguration();
parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher);
$this->urlGenerator = $url_generator;
$this->entityFieldManager = $entity_field_manager;
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('payment.event_dispatcher'), $container
->get('url_generator'), $container
->get('entity_field.manager'), $container
->get('string_translation'));
}
public function defaultConfiguration() {
return array(
'bundle' => NULL,
'entity_type_id' => NULL,
'field_name' => NULL,
) + parent::defaultConfiguration();
}
protected function doGetResumeContextResponse() {
return new Response(new Url('payment_reference.resume_context', array(
'payment' => $this
->getPayment()
->id(),
), array(
'absolute' => TRUE,
)));
}
public function resumeContextAccess(AccountInterface $account) {
return AccessResult::allowedIf($this
->getPayment()
->getOwnerId() == $account
->id());
}
public function getPaymentDescription() {
$field_definitions = $this->entityFieldManager
->getFieldDefinitions($this
->getEntityTypeId(), $this
->getBundle());
return isset($field_definitions[$this
->getFieldName()]) ? $field_definitions[$this
->getFieldName()]
->getLabel() : new TranslatableMarkup('Unavailable');
}
public function setEntityTypeId($entity_type_id) {
$this->configuration['entity_type_id'] = $entity_type_id;
return $this;
}
public function getEntityTypeId() {
return $this->configuration['entity_type_id'];
}
public function setBundle($bundle) {
$this->configuration['bundle'] = $bundle;
return $this;
}
public function getBundle() {
return $this->configuration['bundle'];
}
public function setFieldName($field_name) {
$this->configuration['field_name'] = $field_name;
return $this;
}
public function getFieldName() {
return $this->configuration['field_name'];
}
}