Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractBackend.php
Go to the documentation of this file.
1 <?php
8 
10 
18 abstract class AbstractBackend implements \Magento\Eav\Model\Entity\Attribute\Backend\BackendInterface
19 {
25  protected $_attribute;
26 
32  protected $_valueId;
33 
39  protected $_valueIds = [];
40 
46  protected $_table;
47 
53  protected $_entityIdField;
54 
60  protected $_defaultValue = null;
61 
69  public function setAttribute($attribute)
70  {
71  $this->_attribute = $attribute;
72  return $this;
73  }
74 
81  public function getAttribute()
82  {
83  return $this->_attribute;
84  }
85 
92  public function getType()
93  {
94  return $this->getAttribute()->getBackendType();
95  }
96 
103  public function isStatic()
104  {
105  return $this->getAttribute()->isStatic();
106  }
107 
113  public function getTable()
114  {
115  if (empty($this->_table)) {
116  if ($this->isStatic()) {
117  $this->_table = $this->getAttribute()->getEntityType()->getValueTablePrefix();
118  } elseif ($this->getAttribute()->getBackendTable()) {
119  $this->_table = $this->getAttribute()->getBackendTable();
120  } else {
121  $entity = $this->getAttribute()->getEntity();
122  $tableName = sprintf('%s_%s', $entity->getValueTablePrefix(), $this->getType());
123  $this->_table = $tableName;
124  }
125  }
126 
127  return $this->_table;
128  }
129 
135  public function getEntityIdField()
136  {
137  if (empty($this->_entityIdField)) {
138  if ($this->getAttribute()->getEntityIdField()) {
139  $this->_entityIdField = $this->getAttribute()->getEntityIdField();
140  } else {
141  $this->_entityIdField = $this->getAttribute()->getEntityType()->getValueEntityIdField();
142  }
143  }
144 
145  return $this->_entityIdField;
146  }
147 
155  public function setValueId($valueId)
156  {
157  $this->_valueId = $valueId;
158  return $this;
159  }
160 
168  public function setEntityValueId($entity, $valueId)
169  {
170  if (!$entity || !$entity->getId()) {
171  return $this->setValueId($valueId);
172  }
173 
174  $this->_valueIds[$entity->getId()] = $valueId;
175  return $this;
176  }
177 
184  public function getValueId()
185  {
186  return $this->_valueId;
187  }
188 
195  public function getEntityValueId($entity)
196  {
197  if (!$entity || !$entity->getId() || !array_key_exists($entity->getId(), $this->_valueIds)) {
198  return $this->getValueId();
199  }
200 
201  return $this->_valueIds[$entity->getId()];
202  }
203 
209  public function getDefaultValue()
210  {
211  if ($this->_defaultValue === null) {
212  if ($this->getAttribute()->getDefaultValue()) {
213  $this->_defaultValue = $this->getAttribute()->getDefaultValue();
214  } else {
215  $this->_defaultValue = "";
216  }
217  }
218 
219  return $this->_defaultValue;
220  }
221 
230  public function validate($object)
231  {
232  $attribute = $this->getAttribute();
233  $attrCode = $attribute->getAttributeCode();
234  $value = $object->getData($attrCode);
235 
236  if ($attribute->getIsVisible()
237  && $attribute->getIsRequired()
238  && $attribute->isValueEmpty($value)
239  && $attribute->isValueEmpty($attribute->getDefaultValue())
240  ) {
241  $label = $attribute->getFrontend()->getLabel();
242  throw new LocalizedException(
243  __('The "%1" attribute value is empty. Set the attribute and try again.', $label)
244  );
245  }
246 
247  if ($attribute->getIsUnique()
248  && !$attribute->getIsRequired()
249  && ($value == '' || $attribute->isValueEmpty($value))
250  ) {
251  return true;
252  }
253 
254  if ($attribute->getIsUnique()) {
255  if (!$attribute->getEntity()->checkAttributeUniqueValue($attribute, $object)) {
256  $label = $attribute->getFrontend()->getLabel();
257  throw new LocalizedException(
258  __('The value of the "%1" attribute isn\'t unique. Set a unique value and try again.', $label)
259  );
260  }
261  }
262 
263  return true;
264  }
265 
274  public function afterLoad($object)
275  {
276  return $this;
277  }
278 
285  public function beforeSave($object)
286  {
287  $attrCode = $this->getAttribute()->getAttributeCode();
288  if (!$object->hasData($attrCode) && $this->getDefaultValue()) {
289  $object->setData($attrCode, $this->getDefaultValue());
290  }
291 
292  return $this;
293  }
294 
303  public function afterSave($object)
304  {
305  return $this;
306  }
307 
316  public function beforeDelete($object)
317  {
318  return $this;
319  }
320 
329  public function afterDelete($object)
330  {
331  return $this;
332  }
333 
340  public function getAffectedFields($object)
341  {
342  $data = [];
343  $data[$this->getTable()][] = [
344  'attribute_id' => $this->getAttribute()->getAttributeId(),
345  'value_id' => $this->getEntityValueId($object),
346  ];
347  return $data;
348  }
349 
356  public function isScalar()
357  {
358  return true;
359  }
360 }
$tableName
Definition: trigger.php:13
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$entity
Definition: element.phtml:22