Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
113 changes: 113 additions & 0 deletions Plugin/CategoryFieldPlugin.php
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;
}
Copy link

@rubenromao rubenromao Mar 11, 2024

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:

class CategoryFieldPlugin
{
    /**
     * @param StoreManagerInterface $storeManager
     * @param Registry $registry
     * @param CategoryRepositoryInterface $categoryRepository
     * @param Escaper $escaper
     */
    public function __construct(
        public readonly StoreManagerInterface $storeManager,
        public readonly Registry $registry,
        public readonly CategoryRepositoryInterface $categoryRepository,
        public readonly Escaper $escaper,
    ) {}

// rest of code

Copy link
Author

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)

Copy link

@rubenromao rubenromao Mar 12, 2024

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.


/**
* @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);
}
}
6 changes: 5 additions & 1 deletion etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<plugin name="avs_scopehint_plugin_add_tooltip_to_product_field" type="AvS\ScopeHint\Plugin\ConfigFieldPlugin" sortOrder="1" disabled="false" />
</type>

<type name="Magento\Ui\Component\Form\Field">
<plugin name="avs_scopehint_plugin_add_tooltip_to_category_field" type="AvS\ScopeHint\Plugin\CategoryFieldPlugin" sortOrder="1" disabled="false" />
</type>

<type name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav">
<plugin name="avs_scopehint_plugin_field" type="AvS\ScopeHint\Plugin\ProductEavDataProviderPlugin" sortOrder="1" disabled="false" />
</type>
</config>
</config>