Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Repository Class Reference
Inheritance diagram for Repository:
ProductAttributeRepositoryInterface MetadataServiceInterface

Public Member Functions

 __construct (\Magento\Catalog\Model\ResourceModel\Attribute $attributeResource, \Magento\Catalog\Helper\Product $productHelper, \Magento\Framework\Filter\FilterManager $filterManager, \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepository, \Magento\Eav\Model\Config $eavConfig, \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory $validatorFactory, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder)
 
 get ($attributeCode)
 
 getList (\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
 
 save (\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute)
 
 delete (\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute)
 
 deleteById ($attributeCode)
 
 getCustomAttributesMetadata ($dataObjectClassName=null)
 
- Public Member Functions inherited from ProductAttributeRepositoryInterface
 getList (\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
 

Protected Member Functions

 generateCode ($label)
 
 validateCode ($code)
 

Protected Attributes

 $attributeResource
 
 $eavAttributeRepository
 
 $eavConfig
 
 $inputtypeValidatorFactory
 
 $productHelper
 
 $filterManager
 
 $searchCriteriaBuilder
 

Detailed Description

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 16 of file Repository.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Catalog\Model\ResourceModel\Attribute  $attributeResource,
\Magento\Catalog\Helper\Product  $productHelper,
\Magento\Framework\Filter\FilterManager  $filterManager,
\Magento\Eav\Api\AttributeRepositoryInterface  $eavAttributeRepository,
\Magento\Eav\Model\Config  $eavConfig,
\Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory  $validatorFactory,
\Magento\Framework\Api\SearchCriteriaBuilder  $searchCriteriaBuilder 
)
Parameters
\Magento\Catalog\Model\ResourceModel\Attribute$attributeResource
\Magento\Catalog\Helper\Product$productHelper
\Magento\Framework\Filter\FilterManager$filterManager
\Magento\Eav\Api\AttributeRepositoryInterface$eavAttributeRepository
\Magento\Eav\Model\Config$eavConfig
\Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory$validatorFactory
\Magento\Framework\Api\SearchCriteriaBuilder$searchCriteriaBuilder

Definition at line 62 of file Repository.php.

70  {
71  $this->attributeResource = $attributeResource;
72  $this->productHelper = $productHelper;
73  $this->filterManager = $filterManager;
74  $this->eavAttributeRepository = $eavAttributeRepository;
75  $this->eavConfig = $eavConfig;
76  $this->inputtypeValidatorFactory = $validatorFactory;
77  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
78  }

Member Function Documentation

◆ delete()

{Delete Attribute

Parameters
\Magento\Catalog\Api\Data\ProductAttributeInterface$attribute
Returns
bool True if the entity was deleted (always true)
Exceptions
}

Implements ProductAttributeRepositoryInterface.

Definition at line 185 of file Repository.php.

186  {
187  $this->attributeResource->delete($attribute);
188  return true;
189  }

◆ deleteById()

deleteById (   $attributeCode)

{Delete Attribute by id

Parameters
string$attributeCode
Returns
bool
Exceptions
}

Implements ProductAttributeRepositoryInterface.

Definition at line 194 of file Repository.php.

195  {
196  $this->delete(
197  $this->get($attributeCode)
198  );
199  return true;
200  }
$attributeCode
Definition: extend.phtml:12

◆ generateCode()

generateCode (   $label)
protected

Generate code from label

Parameters
string$label
Returns
string

Definition at line 217 of file Repository.php.

218  {
219  $code = substr(preg_replace('/[^a-z_0-9]/', '_', $this->filterManager->translitUrl($label)), 0, 30);
220  $validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,29}[a-z0-9]$/']);
221  if (!$validatorAttrCode->isValid($code)) {
222  $code = 'attr_' . ($code ?: substr(md5(time()), 0, 8));
223  }
224  return $code;
225  }
$label
Definition: details.phtml:21
$code
Definition: info.phtml:12

◆ get()

get (   $attributeCode)

{Retrieve specific attribute

Parameters
string$attributeCode
Returns
\Magento\Catalog\Api\Data\ProductAttributeInterface
Exceptions
}

Implements ProductAttributeRepositoryInterface.

Definition at line 83 of file Repository.php.

84  {
85  return $this->eavAttributeRepository->get(
86  \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
88  );
89  }
$attributeCode
Definition: extend.phtml:12

◆ getCustomAttributesMetadata()

getCustomAttributesMetadata (   $dataObjectClassName = null)

{Get custom attribute metadata for the given class or interfaces it implements.

Parameters
string | null$dataObjectClassNameData object class name
Returns
\Magento\Framework\Api\MetadataObjectInterface[]
} @SuppressWarnings(PHPMD.UnusedFormalParameter)

Implements MetadataServiceInterface.

Definition at line 206 of file Repository.php.

207  {
208  return $this->getList($this->searchCriteriaBuilder->create())->getItems();
209  }
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
Definition: Repository.php:94

◆ getList()

getList ( \Magento\Framework\Api\SearchCriteriaInterface  $searchCriteria)

{}

Definition at line 94 of file Repository.php.

95  {
96  return $this->eavAttributeRepository->getList(
97  \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
99  );
100  }
$searchCriteria

◆ save()

{Save attribute data

Parameters
\Magento\Catalog\Api\Data\ProductAttributeInterface$attribute
Returns
\Magento\Catalog\Api\Data\ProductAttributeInterface
Exceptions
} @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Implements ProductAttributeRepositoryInterface.

Definition at line 107 of file Repository.php.

108  {
109  if ($attribute->getAttributeId()) {
110  $existingModel = $this->get($attribute->getAttributeCode());
111 
112  if (!$existingModel->getAttributeId()) {
113  throw NoSuchEntityException::singleField('attribute_code', $existingModel->getAttributeCode());
114  }
115 
116  // Attribute code must not be changed after attribute creation
117  $attribute->setAttributeCode($existingModel->getAttributeCode());
118  $attribute->setAttributeId($existingModel->getAttributeId());
119  $attribute->setIsUserDefined($existingModel->getIsUserDefined());
120  $attribute->setFrontendInput($existingModel->getFrontendInput());
121 
122  $this->updateDefaultFrontendLabel($attribute, $existingModel);
123  } else {
124  $attribute->setAttributeId(null);
125 
126  if (!$attribute->getFrontendLabels() && !$attribute->getDefaultFrontendLabel()) {
127  throw InputException::requiredField('frontend_label');
128  }
129 
130  $frontendLabel = $this->updateDefaultFrontendLabel($attribute, null);
131 
132  $attribute->setAttributeCode(
133  $attribute->getAttributeCode() ?: $this->generateCode($frontendLabel)
134  );
135  $this->validateCode($attribute->getAttributeCode());
136  $this->validateFrontendInput($attribute->getFrontendInput());
137 
138  $attribute->setBackendType(
139  $attribute->getBackendTypeByInput($attribute->getFrontendInput())
140  );
141  $attribute->setSourceModel(
142  $this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput())
143  );
144  $attribute->setBackendModel(
145  $this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput())
146  );
147  $attribute->setEntityTypeId(
148  $this->eavConfig
149  ->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
150  ->getId()
151  );
152  $attribute->setIsUserDefined(1);
153  }
154  if (!empty($attribute->getData(AttributeInterface::OPTIONS))) {
155  $options = [];
156  $sortOrder = 0;
157  $default = [];
158  $optionIndex = 0;
159  foreach ($attribute->getOptions() as $option) {
160  $optionIndex++;
161  $optionId = $option->getValue() ?: 'option_' . $optionIndex;
162  $options['value'][$optionId][0] = $option->getLabel();
163  $options['order'][$optionId] = $option->getSortOrder() ?: $sortOrder++;
164  if (is_array($option->getStoreLabels())) {
165  foreach ($option->getStoreLabels() as $label) {
166  $options['value'][$optionId][$label->getStoreId()] = $label->getLabel();
167  }
168  }
169  if ($option->getIsDefault()) {
170  $default[] = $optionId;
171  }
172  }
173  $attribute->setDefault($default);
174  if (count($options)) {
175  $attribute->setOption($options);
176  }
177  }
178  $this->attributeResource->save($attribute);
179  return $this->get($attribute->getAttributeCode());
180  }
$label
Definition: details.phtml:21

◆ validateCode()

validateCode (   $code)
protected

Validate attribute code

Parameters
string$code
Returns
void
Exceptions

Definition at line 234 of file Repository.php.

235  {
236  $validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,30}$/']);
237  if (!$validatorAttrCode->isValid($code)) {
238  throw InputException::invalidFieldValue('attribute_code', $code);
239  }
240  }
static invalidFieldValue($fieldName, $fieldValue, \Exception $cause=null)
$code
Definition: info.phtml:12

Field Documentation

◆ $attributeResource

$attributeResource
protected

Definition at line 21 of file Repository.php.

◆ $eavAttributeRepository

$eavAttributeRepository
protected

Definition at line 26 of file Repository.php.

◆ $eavConfig

$eavConfig
protected

Definition at line 31 of file Repository.php.

◆ $filterManager

$filterManager
protected

Definition at line 46 of file Repository.php.

◆ $inputtypeValidatorFactory

$inputtypeValidatorFactory
protected

Definition at line 36 of file Repository.php.

◆ $productHelper

$productHelper
protected

Definition at line 41 of file Repository.php.

◆ $searchCriteriaBuilder

$searchCriteriaBuilder
protected

Definition at line 51 of file Repository.php.


The documentation for this class was generated from the following file: