Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Fixing the documentation on how to correctly copy the fieldsets with custom attributes #3992

Merged
merged 6 commits into from
Apr 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 56 additions & 37 deletions guides/v2.1/ext-best-practices/tutorials/copy-fieldsets.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ In this tutorial, you will learn to copy custom data from a {% glossarytooltip 7

The following code defines a simple [extension attribute][1] named `demo` for the Cart and Order objects.

**extension_attributes.xml**
**etc/extension_attributes.xml**

{% highlight xml %}
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Api/etc/extension_attributes.xsd">
Expand All @@ -36,9 +36,9 @@ The following code defines a simple [extension attribute][1] named `demo` for th
The following code adds the `demo` field to the `sales_convert_quote` fieldset with the `to_order` aspect.
The code snippet in the next step uses the name of the fieldset and aspect to specify which fields to copy.

**fieldset.xml**
**etc/fieldset.xml**

{% highlight xml %}
```xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="DataObject/etc/fieldset.xsd">
<scope id="global">
<fieldset id="sales_convert_quote">
Expand All @@ -48,51 +48,70 @@ The code snippet in the next step uses the name of the fieldset and aspect to sp
</fieldset>
</scope>
</config>
{% endhighlight %}
```

## Step 3: Copy the fieldset
{:#step-3}

For copying the fieldset, we'll observe the `sales_model_service_quote_submit_before` event by using the following code in our `etc/events.xml`:

```xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_model_service_quote_submit_before">
<observer name="[vendor]_[module]_sales_model_service_quote_submit_before" instance="Vendor\Module\Observer\SaveOrderBeforeSalesModelQuoteObserver" />
</event>
</config>
```

The following code snippets highlight the code pieces needed to copy a fieldset using the `\Magento\Framework\DataObject\Copy` class.

{% highlight php startinline %}
...

/**
* @var \Magento\Framework\DataObject\Copy
*/
protected $objectCopyService;

...

/**
* @param \Magento\Framework\DataObject\Copy $objectCopyService
...
*/
public function __construct(
\Magento\Framework\DataObject\Copy $objectCopyService,
...
) {
$this->objectCopyService = $objectCopyService;
...
}
```php
<?php
namespace Vendor\Module\Observer;

...
use Magento\Framework\Event\ObserverInterface;

/**
* @param $quote \Magento\Quote\Api\Data\CartInterface
* @param $order \Magento\Sales\Api\Data\Order
*/
private function copyQuoteToOrder($quote, $order)
class SaveOrderBeforeSalesModelQuoteObserver implements ObserverInterface
{
...
$copy->copyFieldsetToTarget('sales_convert_quote', 'to_order', $quote, $order);
...
}
...

/**
* @var \Magento\Framework\DataObject\Copy
*/
protected $objectCopyService;

...
{% endhighlight %}
...

/**
* @param \Magento\Framework\DataObject\Copy $objectCopyService
* ...
*/
public function __construct(
\Magento\Framework\DataObject\Copy $objectCopyService,
...
) {
$this->objectCopyService = $objectCopyService;
...
}

/**
* @param \Magento\Framework\Event\Observer $observer
*/
private function execute(\Magento\Framework\Event\Observer $observer)
{
/* @var Magento\Sales\Model\Order $order */
$order = $observer->getEvent()->getData('order');
/* @var Magento\Quote\Model\Quote $quote */
$quote = $observer->getEvent()->getData('quote');

$this->objectCopyService->copyFieldsetToTarget('sales_convert_quote', 'to_order', $quote, $order);
...

return $this;
}
}

```

In the code, an instance of the `Copy` class is obtained from the constructor using [dependency injection][2].
The `copyFieldsetToTarget` function call with the `$quote` and `$order` parameters copies the fieldset for the two objects..
Expand Down