Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Select.php
Go to the documentation of this file.
1 <?php
7 
9 
15 class Select extends \Magento\Eav\Model\Attribute\Data\AbstractData
16 {
24  {
25  return $this->_getRequestValue($request);
26  }
27 
35  public function validateValue($value)
36  {
37  $errors = [];
38  $attribute = $this->getAttribute();
39 
40  if ($value === false) {
41  // try to load original value and validate it
42  $value = $this->getEntity()->getData($attribute->getAttributeCode());
43  }
44 
45  if ($attribute->getIsRequired() && empty($value) && $value != '0') {
46  $label = __($attribute->getStoreLabel());
47  $errors[] = __('"%1" is a required value.', $label);
48  }
49 
50  if (!$errors && !$attribute->getIsRequired() && empty($value)) {
51  return true;
52  }
53 
54  if (count($errors) == 0) {
55  return true;
56  }
57 
58  return $errors;
59  }
60 
67  public function compactValue($value)
68  {
69  if ($value !== false) {
70  $this->getEntity()->setData($this->getAttribute()->getAttributeCode(), $value);
71  }
72  return $this;
73  }
74 
82  public function restoreValue($value)
83  {
84  return $this->compactValue($value);
85  }
86 
93  protected function _getOptionText($value)
94  {
95  return $this->getAttribute()->getSource()->getOptionText($value);
96  }
97 
105  {
106  $value = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
107  switch ($format) {
108  case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON:
109  $output = $value;
110  break;
111  default:
112  if ($value != '') {
113  $output = $this->_getOptionText($value);
114  } else {
115  $output = '';
116  }
117  break;
118  }
119 
120  return $output;
121  }
122 }
_getRequestValue(RequestInterface $request)
__()
Definition: __.php:13
extractValue(RequestInterface $request)
Definition: Select.php:23
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
outputValue($format=\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
Definition: Select.php:104
$errors
Definition: overview.phtml:9