View source
<?php
namespace Drupal\payment_reference\Plugin\Field\FieldType;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\currency\Entity\Currency;
use Drupal\payment\Element\PaymentLineItemsInput;
use Drupal\payment\Payment;
use Drupal\payment_reference\PaymentReference as PaymentReferenceServiceWrapper;
class PaymentReference extends EntityReferenceItem {
public static function defaultStorageSettings() {
return [
'target_type' => 'payment',
] + parent::defaultStorageSettings();
}
public static function defaultFieldSettings() {
return parent::defaultFieldSettings() + [
'currency_code' => '',
'line_items_data' => [],
];
}
public static function schema(FieldStorageDefinitionInterface $field_storage_definition) {
return [
'columns' => [
'target_id' => [
'type' => 'int',
'unsigned' => TRUE,
],
],
'indexes' => [
'target_id' => [
'target_id',
],
],
'foreign keys' => [
'target_id' => [
'table' => 'payment',
'columns' => [
'target_id' => 'id',
],
],
],
];
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$form_helper = \Drupal::service('currency.form_helper');
$element['#element_validate'] = [
get_class() . '::fieldSettingsFormValidate',
];
$element['currency_code'] = [
'#empty_value' => '',
'#type' => 'select',
'#title' => $this
->t('Payment currency'),
'#options' => $form_helper
->getCurrencyOptions(),
'#default_value' => $this
->getSetting('currency_code'),
'#required' => TRUE,
];
$line_items = [];
foreach ($this
->getSetting('line_items_data') as $line_item_data) {
$line_items[] = Payment::lineItemManager()
->createInstance($line_item_data['plugin_id'], $line_item_data['plugin_configuration']);
}
$element['line_items'] = [
'#type' => 'payment_line_items_input',
'#title' => $this
->t('Line items'),
'#default_value' => $line_items,
'#required' => TRUE,
'#currency_code' => '',
];
return $element;
}
public static function fieldSettingsFormValidate(array $element, FormStateInterface $form_state) {
$add_more_button_form_parents = array_merge($element['#array_parents'], [
'line_items',
'add_more',
'add',
]);
$triggering_element = $form_state
->getTriggeringElement();
if ($triggering_element['#array_parents'] != $add_more_button_form_parents) {
$values = $form_state
->getValues();
$values = NestedArray::getValue($values, $element['#array_parents']);
$line_items_data = [];
foreach (PaymentLineItemsInput::getLineItems($element['line_items'], $form_state) as $line_item) {
$line_items_data[] = [
'plugin_id' => $line_item
->getPluginId(),
'plugin_configuration' => $line_item
->getConfiguration(),
];
}
$value = [
'currency_code' => $values['currency_code'],
'line_items_data' => $line_items_data,
];
$form_state
->setValueForElement($element, $value);
}
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
return [];
}
public function preSave() {
$entity_type_id = $this
->getFieldDefinition()
->getFieldStorageDefinition()
->getTargetEntityTypeId();
$entity_storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
$current_entity = $this
->getEntity();
$unchanged_payment_id = NULL;
if ($current_entity
->id()) {
$unchanged_entity = $entity_storage
->loadUnchanged($current_entity
->id());
if ($unchanged_entity) {
$unchanged_field = $unchanged_entity
->get($this
->getFieldDefinition()
->getName());
if (!$unchanged_field
->isEmpty()) {
$unchanged_payment_id = $unchanged_field
->get($this->name)
->get('target_id')
->getValue();
}
}
}
$current_payment_id = $this
->get('target_id')
->getValue();
if (!$current_entity
->id() || $current_payment_id != $unchanged_payment_id) {
$queue = PaymentReferenceServiceWrapper::queue();
$acquisition_code = $queue
->claimPayment($current_payment_id);
if ($acquisition_code !== FALSE) {
$queue
->acquirePayment($current_payment_id, $acquisition_code);
}
else {
$this
->get('target_id')
->setValue(0);
}
}
}
}