Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DynamicField.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15  as FieldTypeConverterInterface;
17  as IndexTypeConverterInterface;
19  as FieldNameResolver;
21 
26 {
32  private $categoryList;
33 
39  private $groupRepository;
40 
46  private $searchCriteriaBuilder;
47 
51  private $fieldTypeConverter;
52 
56  private $indexTypeConverter;
57 
61  private $attributeAdapterProvider;
62 
66  private $fieldNameResolver;
67 
77  public function __construct(
78  FieldTypeConverterInterface $fieldTypeConverter,
79  IndexTypeConverterInterface $indexTypeConverter,
80  GroupRepositoryInterface $groupRepository,
81  SearchCriteriaBuilder $searchCriteriaBuilder,
82  CategoryListInterface $categoryList,
83  FieldNameResolver $fieldNameResolver,
84  AttributeProvider $attributeAdapterProvider
85  ) {
86  $this->groupRepository = $groupRepository;
87  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
88  $this->fieldTypeConverter = $fieldTypeConverter;
89  $this->indexTypeConverter = $indexTypeConverter;
90  $this->categoryList = $categoryList;
91  $this->fieldNameResolver = $fieldNameResolver;
92  $this->attributeAdapterProvider = $attributeAdapterProvider;
93  }
94 
98  public function getFields(array $context = []): array
99  {
100  $allAttributes = [];
101  $searchCriteria = $this->searchCriteriaBuilder->create();
102  $categories = $this->categoryList->getList($searchCriteria)->getItems();
103  $positionAttribute = $this->attributeAdapterProvider->getByAttributeCode('position');
104  $categoryNameAttribute = $this->attributeAdapterProvider->getByAttributeCode('category_name');
105  foreach ($categories as $category) {
106  $categoryPositionKey = $this->fieldNameResolver->getFieldName(
107  $positionAttribute,
108  ['categoryId' => $category->getId()]
109  );
110  $categoryNameKey = $this->fieldNameResolver->getFieldName(
111  $categoryNameAttribute,
112  ['categoryId' => $category->getId()]
113  );
114  $allAttributes[$categoryPositionKey] = [
115  'type' => $this->fieldTypeConverter->convert(FieldTypeConverterInterface::INTERNAL_DATA_TYPE_STRING),
116  'index' => $this->indexTypeConverter->convert(IndexTypeConverterInterface::INTERNAL_NO_INDEX_VALUE)
117  ];
118  $allAttributes[$categoryNameKey] = [
119  'type' => $this->fieldTypeConverter->convert(FieldTypeConverterInterface::INTERNAL_DATA_TYPE_STRING),
120  'index' => $this->indexTypeConverter->convert(IndexTypeConverterInterface::INTERNAL_NO_INDEX_VALUE)
121  ];
122  }
123 
124  $groups = $this->groupRepository->getList($searchCriteria)->getItems();
125  $priceAttribute = $this->attributeAdapterProvider->getByAttributeCode('price');
126  foreach ($groups as $group) {
127  $groupPriceKey = $this->fieldNameResolver->getFieldName(
128  $priceAttribute,
129  ['customerGroupId' => $group->getId(), 'websiteId' => $context['websiteId']]
130  );
131  $allAttributes[$groupPriceKey] = [
132  'type' => $this->fieldTypeConverter->convert(FieldTypeConverterInterface::INTERNAL_DATA_TYPE_FLOAT),
133  'store' => true
134  ];
135  }
136 
137  return $allAttributes;
138  }
139 }
$groupRepository
$group
Definition: sections.phtml:16
$searchCriteria
__construct(FieldTypeConverterInterface $fieldTypeConverter, IndexTypeConverterInterface $indexTypeConverter, GroupRepositoryInterface $groupRepository, SearchCriteriaBuilder $searchCriteriaBuilder, CategoryListInterface $categoryList, FieldNameResolver $fieldNameResolver, AttributeProvider $attributeAdapterProvider)
$searchCriteriaBuilder
$categories