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

7786-Changed the code for afterSave function in adding-attributes.md file #8246

Conversation

chiranjeevi-cj
Copy link
Contributor

Purpose of this pull request

This pull request (PR)
https://devdocs.magento.com/guides/v2.4/extension-dev-guide/extension_attributes/adding-attributes.html

Directly following the general patterns described on this page - for an extension attribute on a product, with a plugin connected to the product repository - the afterSave function will not update the extension attribute. For example, making an API call with updated extension attribute will leave it unchanged.

Description

Essentially, the data in the product instance returned as the result of a save call (passed as the 2nd parameter to an 'after' plugin function) is not what was sent via the API.

After a little digging, the data that reaches the beforeSave function is correct. However, the final step in the product repository save process is a get call, presumably to refresh data, which correspondingly triggers the afterGet function to (re-)populate the extension attributes on the product returned as the result.

The code example on the page is using the ProductInterface that is the result returned from the function, due to the attribute sequence, hence why it can't / won't update the extension attribute.

However, it should also be noted that is specifically a peculiarity of the product repository and does not necessarily hold true for other repositories.

Affected DevDocs pages

Fixes #7786

Copy link
Contributor

@BarnyShergold BarnyShergold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution - just some formatting queries

@@ -106,14 +106,20 @@ Likewise, the `afterSave` plugin should manipulate the entity data before return
```php
public function afterSave
(
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $entity
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indenting on this line (and next 2) should have one less space

\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $entity
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $result, /** result from the save call **/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally in PHP single line comments are done with // e.g. // result from the save call
Is there a reason the /** .... **/ format is used?

Copy link
Contributor Author

@chiranjeevi-cj chiranjeevi-cj Nov 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in the site(https://devdocs.magento.com/guides/v2.4/extension-dev-guide/extension_attributes/adding-attributes.html), every comment was done with /** .... **/ that's why I followed it.
Should I need to change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - if it's being used elsewhere then you should stick to it for convention. It's not a must.

@chiranjeevi-cj
Copy link
Contributor Author

@BarnyShergold As in the site(https://devdocs.magento.com/guides/v2.4/extension-dev-guide/extension_attributes/adding-attributes.html), every comment was done with /** .... **/ that's why I followed it.
Should I need to change?

@chiranjeevi-cj
Copy link
Contributor Author

@BarnyShergold Is there anything that needs to be changed?

Copy link
Contributor

@BarnyShergold BarnyShergold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@dobooth dobooth added 2.4.x Magento 2.4.x related changes Technical Updates to the code or processes that alter the technical content of the doc labels Nov 24, 2020
@dobooth
Copy link
Contributor

dobooth commented Nov 24, 2020

running tests

@dobooth dobooth merged commit eff234c into magento:2.4.2-develop Nov 24, 2020
@ghost
Copy link

ghost commented Nov 24, 2020

Hi @chiranjeevi-cj, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
2.4.x Magento 2.4.x related changes Partner: EY partners-contribution PR created by Magento partner Progress: done Technical Updates to the code or processes that alter the technical content of the doc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants