Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeOptionProvider.php
Go to the documentation of this file.
1 <?php
8 
14 
19 {
23  private $scopeResolver;
24 
28  private $attributeResource;
29 
33  private $optionSelectBuilder;
34 
40  public function __construct(
41  Attribute $attributeResource,
42  ScopeResolverInterface $scopeResolver,
43  OptionSelectBuilderInterface $optionSelectBuilder
44  ) {
45  $this->attributeResource = $attributeResource;
46  $this->scopeResolver = $scopeResolver;
47  $this->optionSelectBuilder = $optionSelectBuilder;
48  }
49 
53  public function getAttributeOptions(AbstractAttribute $superAttribute, $productId)
54  {
55  $scope = $this->scopeResolver->getScope();
56  $select = $this->optionSelectBuilder->getSelect($superAttribute, $productId, $scope);
57  $data = $this->attributeResource->getConnection()->fetchAll($select);
58 
59  if ($superAttribute->getSourceModel()) {
60  $options = $superAttribute->getSource()->getAllOptions(false);
61 
62  $optionLabels = [];
63  foreach ($options as $option) {
64  $optionLabels[$option['value']] = $option['label'];
65  }
66 
67  foreach ($data as $key => $value) {
68  $optionText = isset($optionLabels[$value['value_index']])
69  ? $optionLabels[$value['value_index']]
70  : false;
71  $data[$key]['default_title'] = $optionText;
72  $data[$key]['option_title'] = $optionText;
73  }
74  }
75 
76  return $data;
77  }
78 }
getAttributeOptions(AbstractAttribute $superAttribute, $productId)
__construct(Attribute $attributeResource, ScopeResolverInterface $scopeResolver, OptionSelectBuilderInterface $optionSelectBuilder)
$value
Definition: gender.phtml:16