Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sortby.php
Go to the documentation of this file.
1 <?php
8 
17 class Sortby extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
18 {
24  protected $_scopeConfig;
25 
31  public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
32  {
33  $this->_scopeConfig = $scopeConfig;
34  }
35 
45  public function validate($object)
46  {
47  $attributeCode = $this->getAttribute()->getName();
48  $postDataConfig = $object->getData('use_post_data_config') ?: [];
49  $isUseConfig = in_array($attributeCode, $postDataConfig);
50 
51  if ($this->getAttribute()->getIsRequired()) {
52  $attributeValue = $object->getData($attributeCode);
53  if ($this->getAttribute()->isValueEmpty($attributeValue) && !$isUseConfig) {
54  return false;
55  }
56  }
57 
58  if ($this->getAttribute()->getIsUnique()) {
59  if (!$this->getAttribute()->getEntity()->checkAttributeUniqueValue($this->getAttribute(), $object)) {
60  $label = $this->getAttribute()->getFrontend()->getLabel();
61  throw new \Magento\Framework\Exception\LocalizedException(
62  __('The value of the "%1" attribute isn\'t unique. Set a unique value and try again.', $label)
63  );
64  }
65  }
66 
67  if ($attributeCode == 'default_sort_by') {
68  $available = $object->getData('available_sort_by') ?: [];
69  $available = is_array($available) ? $available : explode(',', $available);
70  $data = !in_array(
71  'default_sort_by',
72  $postDataConfig
73  ) ? $object->getData(
75  ) : $this->_scopeConfig->getValue(
76  "catalog/frontend/default_sort_by",
77  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
78  );
79  if (!in_array($data, $available) && !in_array('available_sort_by', $postDataConfig)) {
80  throw new \Magento\Framework\Exception\LocalizedException(
81  __('Default Product Listing Sort by does not exist in Available Product Listing Sort By.')
82  );
83  }
84  }
85 
86  return true;
87  }
88 
95  public function beforeSave($object)
96  {
97  $attributeCode = $this->getAttribute()->getName();
98  if ($attributeCode == 'available_sort_by') {
99  $data = $object->getData($attributeCode);
100  if (!is_array($data)) {
101  $data = [];
102  }
103  $object->setData($attributeCode, implode(',', $data) ?: null);
104  }
105  if (!$object->hasData($attributeCode)) {
106  $object->setData($attributeCode, null);
107  }
108  return $this;
109  }
110 
117  public function afterLoad($object)
118  {
119  $attributeCode = $this->getAttribute()->getName();
120  if ($attributeCode == 'available_sort_by') {
121  $data = $object->getData($attributeCode);
122  if ($data) {
123  if (!is_array($data)) {
124  $object->setData($attributeCode, explode(',', $data));
125  } else {
126  $object->setData($attributeCode, $data);
127  }
128  }
129  }
130  return $this;
131  }
132 }
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
Definition: Sortby.php:31
__()
Definition: __.php:13
$label
Definition: details.phtml:21
$attributeCode
Definition: extend.phtml:12