63 $this->_eavEntityType = $eavEntityType;
75 $this->
_init(
'eav_attribute',
'attribute_id');
86 $this->_uniqueFields = [
87 [
'field' => [
'attribute_code',
'entity_type_id'],
'title' =>
__(
'Attribute with the same code')],
122 if (intval($object->getAttributeGroupId()) > 0) {
125 ':attribute_set_id' => $object->getAttributeSetId(),
126 ':attribute_group_id' => $object->getAttributeGroupId(),
129 $this->
getTable(
'eav_entity_attribute'),
132 'attribute_set_id = :attribute_set_id' 134 'attribute_group_id = :attribute_group_id' 151 if (!$object->getEntityAttributeId()) {
156 $this->
getTable(
'eav_entity_attribute'),
157 [
'entity_attribute_id = ?' => $object->getEntityAttributeId()]
172 $frontendLabel = $object->getFrontendLabel();
173 if (is_array($frontendLabel)) {
174 $this->checkDefaultFrontendLabelExists($frontendLabel, $frontendLabel);
175 $object->setFrontendLabel($frontendLabel[0])->setStoreLabels($frontendLabel);
177 $this->setStoreLabels($object, $frontendLabel);
183 if (!$object->
getId()) {
184 if ($object->getFrontendInput() ==
'select') {
189 return parent::_beforeSave($object);
202 )->_saveAdditionalAttributeData(
204 )->saveInSetIncluding(
209 $this->getConfig()->clear();
210 return parent::_afterSave($object);
223 $this->getConfig()->clear();
231 private function getConfig()
233 if (!$this->config) {
236 return $this->config;
247 $storeLabels = $object->getStoreLabels();
248 if (is_array($storeLabels)) {
250 if ($object->
getId()) {
251 $condition = [
'attribute_id =?' => $object->
getId()];
275 if ($additionalTable) {
278 $bind = [
':attribute_id' => $object->
getId()];
283 'attribute_id = :attribute_id' 287 $where = [
'attribute_id = ?' => $object->
getId()];
309 $attributeEntityId =
null,
312 $attributeSortOrder =
null 314 $attributeId = $attributeEntityId ===
null ? (int)$object->
getId() : (int)$attributeEntityId;
317 $attributeSortOrder = $attributeSortOrder ===
null ? (int)$object->getSortOrder() : (int)$attributeSortOrder;
319 if ($setId &&
$groupId && $object->getEntityTypeId()) {
323 $sortOrder = $attributeSortOrder ?: $this->_getMaxSortOrder($object) + 1;
325 'entity_type_id' => $object->getEntityTypeId(),
326 'attribute_set_id' => $setId,
328 'attribute_id' => $attributeId,
329 'sort_order' => $sortOrder,
332 $where = [
'attribute_id =?' => $attributeId,
'attribute_set_id =?' => $setId];
348 $option = $object->getOption();
353 $defaultValue = $object->getDefault() ?: [];
355 if (!is_array($object->getDefault())) {
356 $object->setDefault([]);
377 if ($intOptionId ===
false) {
384 return $defaultValue;
397 throw new \Magento\Framework\Exception\LocalizedException(
398 __(
"The default option isn't defined. Set the option and try again.")
414 if (in_array(
$optionId, $object->getDefault())) {
415 $frontendInput = $object->getFrontendInput();
416 if ($frontendInput ===
'multiselect') {
417 $defaultValue[] = $intOptionId;
418 }
elseif ($frontendInput ===
'select') {
419 $defaultValue = [$intOptionId];
433 if ($defaultValue !==
null) {
434 $bind = [
'default_value' => implode(
',', $defaultValue)];
435 $where = [
'attribute_id = ?' => $object->
getId()];
464 $data = [
'attribute_id' => $object->
getId(),
'sort_order' => $sortOrder];
468 $data = [
'sort_order' => $sortOrder];
469 $where = [
'option_id = ?' => $intOptionId];
490 $stores = $this->_storeManager->getStores(
true);
512 [
'a' => $this->
getTable(
'eav_attribute')],
515 [
't' => $this->
getTable(
'eav_entity_type')],
516 'a.entity_type_id = t.entity_type_id',
519 't.entity_type_code = :entity_type_code' 521 'a.attribute_code = :attribute_code' 537 $this->
getTable(
'eav_entity_attribute')
539 'entity_attribute_id = ?',
540 (
int)$entityAttributeId
554 $bind = [
':frontend_input' => $frontendType];
559 'frontend_input = :frontend_input' 575 $joinConditionTemplate =
"%s.entity_id=%s.entity_id" .
576 " AND %s.entity_type_id = " .
578 " AND %s.attribute_id = " .
580 " AND %s.store_id = %d";
581 $joinCondition = sprintf(
582 $joinConditionTemplate,
591 $joinCondition .=
' AND e.child_id = t1.entity_id';
594 $valueExpr =
$connection->getCheckSql(
't2.value_id > 0',
't2.value',
't1.value');
598 [
't1' =>
$attribute->getBackend()->getTable()],
602 [
't2' =>
$attribute->getBackend()->getTable()],
603 sprintf($joinConditionTemplate,
't1',
't2',
't2',
't2',
't2',
$storeId),
604 [
$attribute->getAttributeCode() => $valueExpr]
607 $select->where(
"e.is_child = ?", 0);
634 return $this->_eavEntityType->getAdditionalAttributeTable(
$entityTypeId);
649 $additionalTable =
$entityType->getAdditionalAttributeTable();
654 if ($additionalTable) {
656 $bind = [
':attribute_id' => $object->
getId()];
660 'attribute_id = :attribute_id' 675 private $storeLabelsCache = [];
685 if (!isset($this->storeLabelsCache[$attributeId])) {
687 $bind = [
':attribute_id' => $attributeId];
689 $this->
getTable(
'eav_attribute_label'),
690 [
'store_id',
'value']
692 'attribute_id = :attribute_id' 697 return $this->storeLabelsCache[$attributeId];
713 'attribute_id IN (?)',
743 ->get(\
Magento\
Store\Model\StoreManagerInterface::class);
754 private function setStoreLabels(
AbstractModel $object, $frontendLabel)
757 $frontendLabels = $object->getFrontendLabels();
758 if (isset($frontendLabels[0])
761 foreach ($frontendLabels as
$label) {
762 $resultLabel[
$label->getStoreId()] =
$label->getLabel();
764 $this->checkDefaultFrontendLabelExists($frontendLabel, $resultLabel);
765 $object->setStoreLabels($resultLabel);
777 private function checkDefaultFrontendLabelExists($frontendLabel, $resultLabels)
779 $isAdminStoreLabel = (isset($resultLabels[0]) && !empty($resultLabels[0]));
780 if (empty($frontendLabel) && !$isAdminStoreLabel) {
781 throw new \Magento\Framework\Exception\LocalizedException(
__(
'The storefront label is not defined.'));
_saveOption(AbstractModel $object)
getAttributeCodesByFrontendType($frontendType)
_checkDefaultOptionValue($values)
setData($key, $value=null)
getAdditionalAttributeTable($entityTypeId)
_prepareDataForTable(DataObject $object, $table)
getData($key='', $index=null)
getValidAttributeIds($attributeIds)
elseif(isset( $params[ 'redirect_parent']))
_updateAttributeOptionValues($optionId, $values)
getIdByCode($entityType, $code)
_saveAdditionalAttributeData(AbstractModel $object)
_afterDelete(\Magento\Framework\Model\AbstractModel $object)
getStoreLabelsByAttributeId($attributeId)
_getLoadSelect($field, $value, $object)
_processAttributeOptions($object, $option)
_init($mainTable, $idFieldName)
_updateAttributeOption($object, $optionId, $option)
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, Type $eavEntityType, $connectionName=null)
_saveDefaultValue($object, $defaultValue)
_beforeSave(AbstractModel $object)
saveInSetIncluding(AbstractModel $object, $attributeEntityId=null, $attributeSetId=null, $attributeGroupId=null, $attributeSortOrder=null)
static $_entityAttributes
_saveStoreLabels(AbstractModel $object)
_updateDefaultValue($object, $optionId, $intOptionId, &$defaultValue)
loadByCode(AbstractModel $object, $entityTypeId, $code)
_afterLoad(\Magento\Framework\Model\AbstractModel $object)
_afterSave(AbstractModel $object)
deleteEntity(\Magento\Framework\Model\AbstractModel $object)
getEntityAttribute($entityAttributeId)