Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryName.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 
20 {
24  private $storeManager;
25 
29  private $coreRegistry;
30 
35  public function __construct(
36  StoreManager $storeManager = null,
37  Registry $coreRegistry = null
38  ) {
39  $this->storeManager = $storeManager ?: ObjectManager::getInstance()
40  ->get(StoreManager::class);
41  $this->coreRegistry = $coreRegistry ?: ObjectManager::getInstance()
42  ->get(Registry::class);
43  }
44 
52  public function getFieldName(AttributeAdapter $attribute, $context = []): ?string
53  {
54  if ($attribute->getAttributeCode() === 'category_name') {
55  return 'name_category_' . $this->resolveCategoryId($context);
56  }
57 
58  return null;
59  }
60 
64  private function resolveCategoryId($context): int
65  {
66  if (isset($context['categoryId'])) {
67  $id = $context['categoryId'];
68  } else {
69  $id = $this->coreRegistry->registry('current_category')
70  ? $this->coreRegistry->registry('current_category')->getId()
71  : $this->storeManager->getStore()->getRootCategoryId();
72  }
73 
74  return (int)$id;
75  }
76 }
$id
Definition: fieldset.phtml:14