-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add scopehint on the category edit form #27
Open
thedotwriter
wants to merge
1
commit into
avstudnitz:master
Choose a base branch
from
thedotwriter:feature/scopehint-on-category-form
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace AvS\ScopeHint\Plugin; | ||
|
||
use Magento\Catalog\Api\CategoryRepositoryInterface; | ||
use Magento\Framework\Escaper; | ||
use Magento\Framework\Registry; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use Magento\Ui\Component\Form\Field as FieldComponent; | ||
|
||
class CategoryFieldPlugin | ||
{ | ||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
protected $storeManager; | ||
|
||
/** | ||
* @var Registry | ||
*/ | ||
protected $registry; | ||
|
||
/** | ||
* @var CategoryRepositoryInterface | ||
*/ | ||
protected $categoryRepository; | ||
|
||
/** | ||
* @var Escaper | ||
*/ | ||
protected $escaper; | ||
|
||
/** | ||
* @param StoreManagerInterface $storeManager | ||
* @param Registry $registry | ||
* @param CategoryRepositoryInterface $categoryRepository | ||
* @param Escaper $escaper | ||
*/ | ||
public function __construct( | ||
StoreManagerInterface $storeManager, | ||
Registry $registry, | ||
CategoryRepositoryInterface $categoryRepository, | ||
Escaper $escaper | ||
) { | ||
$this->storeManager = $storeManager; | ||
$this->registry = $registry; | ||
$this->categoryRepository = $categoryRepository; | ||
$this->escaper = $escaper; | ||
} | ||
|
||
/** | ||
* @param FieldComponent $subject | ||
* @param $config | ||
* @return array | ||
*/ | ||
public function afterGetConfiguration(FieldComponent $subject, $config) | ||
{ | ||
if ($subject->getContext()->getNamespace() == 'category_form' && | ||
thedotwriter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$config['formElement'] != 'hidden' && | ||
strpos($config['dataScope'], 'use_config.') === false | ||
) { | ||
$scopeHints = []; | ||
$attributeCode = $config['dataScope']; | ||
$storeViews = $this->storeManager->getStores(); | ||
thedotwriter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$category = $this->registry->registry('current_category'); | ||
|
||
if ($category === null || $category->getId() === null) { | ||
return $config; | ||
} | ||
|
||
foreach ($storeViews as $storeView) { | ||
$categoryByStoreCode = $this->getCategoryInStoreView($category->getId(), $storeView->getId()); | ||
$currentScopeValueForCode = $value = $categoryByStoreCode->getData($attributeCode); | ||
|
||
if ($config['dataScope'] == 'select' && !is_array($currentScopeValueForCode)) { | ||
$value = $categoryByStoreCode | ||
->getResource() | ||
->getAttribute($attributeCode) | ||
->getSource() | ||
->getOptionText($currentScopeValueForCode); | ||
} | ||
|
||
try { | ||
$valueAsString = (string) $value; | ||
} catch (\Throwable $ex) { | ||
$valueAsString = json_encode($value); | ||
if ($valueAsString === false) { | ||
$valueAsString = null; | ||
} | ||
} | ||
|
||
if ($valueAsString !== null && $category->getData($attributeCode) !== $currentScopeValueForCode) { | ||
$scopeHints[] = '<code>' . $storeView->getName() . '</code> : ' . $this->escaper->escapeHtml($valueAsString); | ||
} | ||
|
||
if (!empty($scopeHints)) { | ||
$config['tooltip']['description'] = implode('<br>', $scopeHints); | ||
} | ||
} | ||
} | ||
return $config; | ||
} | ||
|
||
/** | ||
* @param $categoryId | ||
* @param $storeViewId | ||
* @return \Magento\Catalog\Api\Data\CategoryInterface | ||
*/ | ||
private function getCategoryInStoreView($categoryId, $storeViewId) | ||
{ | ||
return $this->categoryRepository->get($categoryId, $storeViewId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi @thedotwriter
I would use the "constructor property promotion" introduced in PHP 8.0.
the properties are declared directly in the constructor which is a more concise and clean way to define class properties.
Example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks for checking this PR. This code is indeed quite old now. I will consider updating it if I ever get a feedback from the repo owner : p (@avstudnitz)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Well done btw!
Hope that this is approved and merged soon.