diff --git a/src/Entity/Payment.php b/src/Entity/Payment.php index d7696c3..53cb25d 100644 --- a/src/Entity/Payment.php +++ b/src/Entity/Payment.php @@ -61,11 +61,16 @@ class Payment extends ContentEntityBase implements PaymentInterface { * {@inheritdoc} */ public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = array()) { - // Unset the values for fields that are stored in the base table. These - // values are not needed here, but because the entity storage skips - // unserialization, they cause errors. - unset($values['payment_method']); - unset($values['payment_type']); + // Unserialize the values for fields that are stored in the base table, + // because the entity storage does not do that. + $property_names = ['payment_method', 'payment_type']; + foreach ($property_names as $property_name) { + if (isset($values[$property_name])) { + foreach ($values[$property_name] as &$item_values) { + $item_values['plugin_configuration'] = unserialize($item_values['plugin_configuration']); + } + } + } parent::__construct($values, $entity_type, $bundle, $translations); } diff --git a/src/Plugin/DataType/PluginInstance.php b/src/Plugin/DataType/PluginInstance.php index 182f3f8..ad48381 100644 --- a/src/Plugin/DataType/PluginInstance.php +++ b/src/Plugin/DataType/PluginInstance.php @@ -38,4 +38,13 @@ class PluginInstance extends TypedData { parent::setValue($value, $notify); } + /** + * {@inheritdoc} + */ + public function __clone() { + if ($this->getValue()) { + $this->setValue(clone $this->value); + } + } + } diff --git a/src/Plugin/Field/FieldType/LineItemItem.php b/src/Plugin/Field/FieldType/LineItemItem.php index de1643b..585cf90 100644 --- a/src/Plugin/Field/FieldType/LineItemItem.php +++ b/src/Plugin/Field/FieldType/LineItemItem.php @@ -14,8 +14,7 @@ use Drupal\payment\Payment; * * @FieldType( * id = "payment_line_item", - * label = @Translation("Payment line item plugins"), - * list_class = "\Drupal\payment\Plugin\Field\FieldType\PluginBagItemList" + * label = @Translation("Payment line item plugins") * ) */ class LineItemItem extends PaymentAwarePluginBagItemBase { @@ -28,15 +27,4 @@ class LineItemItem extends PaymentAwarePluginBagItemBase { return Payment::lineItemManager(); } - /** - * {@inheritdoc} - */ - public function onChange($property_name, $notify = TRUE) { - if ($this->getParent() instanceof PaymentInterface) { - /** @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface $plugin_instance */ - $plugin_instance = $this->getContainedPluginInstance(); - $plugin_instance->setPayment($this->getParent()); - } - } - } diff --git a/src/Plugin/Field/FieldType/PaymentAwarePluginBagItemBase.php b/src/Plugin/Field/FieldType/PaymentAwarePluginBagItemBase.php index a057b18..abf1019 100644 --- a/src/Plugin/Field/FieldType/PaymentAwarePluginBagItemBase.php +++ b/src/Plugin/Field/FieldType/PaymentAwarePluginBagItemBase.php @@ -8,6 +8,7 @@ namespace Drupal\payment\Plugin\Field\FieldType; use Drupal\payment\Entity\PaymentInterface; +use Drupal\payment\PaymentAwareInterface; /** * Provides a plugin bag for payment-aware plugins. @@ -19,16 +20,18 @@ abstract class PaymentAwarePluginBagItemBase extends PluginBagItemBase { */ public function onChange($property_name, $notify = TRUE) { if ($property_name == 'plugin_configuration') { - $data = $this; - while ($data = $data->getParent()) { - if ($data instanceof PaymentInterface) { - /** @var \Drupal\payment\PaymentAwareInterface $plugin_instance */ - $plugin_instance = $this->getContainedPluginInstance(); - $plugin_instance->setPayment($data); - break; + $plugin_instance = $this->getContainedPluginInstance(); + if ($plugin_instance instanceof PaymentAwareInterface) { + $data = $this; + while ($data = $data->getParent()) { + if ($data instanceof PaymentInterface) { + $plugin_instance->setPayment($data); + break; + } } } } + parent::onChange($property_name, $notify); } } diff --git a/src/Plugin/Field/FieldType/PaymentMethodItem.php b/src/Plugin/Field/FieldType/PaymentMethodItem.php index eeaa0d3..4b0074d 100644 --- a/src/Plugin/Field/FieldType/PaymentMethodItem.php +++ b/src/Plugin/Field/FieldType/PaymentMethodItem.php @@ -7,7 +7,6 @@ namespace Drupal\payment\Plugin\Field\FieldType; -use Drupal\payment\Entity\PaymentInterface; use Drupal\payment\Payment; /** @@ -15,8 +14,7 @@ use Drupal\payment\Payment; * * @FieldType( * id = "payment_method", - * label = @Translation("Payment method plugins"), - * list_class = "\Drupal\payment\Plugin\Field\FieldType\PluginBagItemList" + * label = @Translation("Payment method plugins") * ) */ class PaymentMethodItem extends PaymentAwarePluginBagItemBase { diff --git a/src/Plugin/Field/FieldType/PaymentStatusItem.php b/src/Plugin/Field/FieldType/PaymentStatusItem.php index f321c2e..8bde494 100644 --- a/src/Plugin/Field/FieldType/PaymentStatusItem.php +++ b/src/Plugin/Field/FieldType/PaymentStatusItem.php @@ -14,8 +14,7 @@ use Drupal\payment\Payment; * * @FieldType( * id = "payment_status", - * label = @Translation("Payment status plugins"), - * list_class = "\Drupal\payment\Plugin\Field\FieldType\PluginBagItemList" + * label = @Translation("Payment status plugins") * ) */ class PaymentStatusItem extends PaymentAwarePluginBagItemBase { diff --git a/src/Plugin/Field/FieldType/PaymentTypeItem.php b/src/Plugin/Field/FieldType/PaymentTypeItem.php index 10069ea..bf7e53f 100644 --- a/src/Plugin/Field/FieldType/PaymentTypeItem.php +++ b/src/Plugin/Field/FieldType/PaymentTypeItem.php @@ -14,8 +14,7 @@ use Drupal\payment\Payment; * * @FieldType( * id = "payment_type", - * label = @Translation("Payment type plugins"), - * list_class = "\Drupal\payment\Plugin\Field\FieldType\PluginBagItemList" + * label = @Translation("Payment type plugins") * ) */ class PaymentTypeItem extends PaymentAwarePluginBagItemBase { diff --git a/src/Plugin/Field/FieldType/PluginBagItemBase.php b/src/Plugin/Field/FieldType/PluginBagItemBase.php index f3435f4..28e140d 100644 --- a/src/Plugin/Field/FieldType/PluginBagItemBase.php +++ b/src/Plugin/Field/FieldType/PluginBagItemBase.php @@ -16,7 +16,8 @@ use Drupal\Core\TypedData\MapDataDefinition; /** * Provides a base for plugin bag field items. */ -abstract class PluginBagItemBase extends FieldItemBase implements PluginBagItemInterface { +abstract class +PluginBagItemBase extends FieldItemBase implements PluginBagItemInterface { /** * {@inheritdoc} @@ -180,14 +181,4 @@ abstract class PluginBagItemBase extends FieldItemBase implements PluginBagItemI } } - /** - * {@inheritdoc} - */ - public function __clone() { - $plugin_instance = $this->get('plugin_instance')->getValue(); - if (is_object($plugin_instance)) { - $this->get('plugin_instance')->setValue(clone $plugin_instance, FALSE); - } - } - } diff --git a/src/Plugin/Field/FieldType/PluginBagItemList.php b/src/Plugin/Field/FieldType/PluginBagItemList.php deleted file mode 100644 index 1b73d7f..0000000 --- a/src/Plugin/Field/FieldType/PluginBagItemList.php +++ /dev/null @@ -1,28 +0,0 @@ -list as $delta => $item) { - $cloned_items[$delta] = clone $item; - } - $this->list = $cloned_items; - } - -}