本文整理汇总了PHP中Magento\Catalog\Helper\Product::getAttributeBackendModelByInputType方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::getAttributeBackendModelByInputType方法的具体用法?PHP Product::getAttributeBackendModelByInputType怎么用?PHP Product::getAttributeBackendModelByInputType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Catalog\Helper\Product
的用法示例。
在下文中一共展示了Product::getAttributeBackendModelByInputType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create
/**
* {@inheritdoc}
*/
public function create(\Magento\Catalog\Service\V1\Data\Eav\AttributeMetadata $attributeMetadata)
{
/**
* @var $model \Magento\Catalog\Model\Resource\Eav\Attribute
*/
$model = $this->attributeFactory->create();
$data = $attributeMetadata->__toArray();
// unset attribute id because we create new attribute (does not rewrite existing one)
unset($data[AttributeMetadata::ATTRIBUTE_ID]);
// define frontend label
if (!$attributeMetadata->getFrontendLabel()) {
throw InputException::requiredField(AttributeMetadata::FRONTEND_LABEL);
}
$data[AttributeMetadata::FRONTEND_LABEL] = [];
foreach ($attributeMetadata->getFrontendLabel() as $label) {
$data[AttributeMetadata::FRONTEND_LABEL][$label->getStoreId()] = $label->getLabel();
}
if (!isset($data[AttributeMetadata::FRONTEND_LABEL][0]) || !$data[AttributeMetadata::FRONTEND_LABEL][0]) {
throw InputException::invalidFieldValue(AttributeMetadata::FRONTEND_LABEL, null);
}
$data[AttributeMetadata::ATTRIBUTE_CODE] = $attributeMetadata->getAttributeCode() ?: $this->generateCode($data[AttributeMetadata::FRONTEND_LABEL][0]);
$this->validateCode($data[AttributeMetadata::ATTRIBUTE_CODE]);
$this->validateFrontendInput($attributeMetadata->getFrontendInput());
$data[AttributeMetadata::BACKEND_TYPE] = $model->getBackendTypeByInput($attributeMetadata->getFrontendInput());
$data[AttributeMetadata::SOURCE_MODEL] = $this->helper->getAttributeSourceModelByInputType($attributeMetadata->getFrontendInput());
$data[AttributeMetadata::BACKEND_MODEL] = $this->helper->getAttributeBackendModelByInputType($attributeMetadata->getFrontendInput());
$model->addData($data);
$model->setEntityTypeId($this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId())->setIsUserDefined(1);
return $model->save()->getAttributeCode();
}
示例2: save
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute)
{
if ($attribute->getAttributeId()) {
$existingModel = $this->get($attribute->getAttributeCode());
if (!$existingModel->getAttributeId()) {
throw NoSuchEntityException::singleField('attribute_code', $existingModel->getAttributeCode());
}
$attribute->setAttributeId($existingModel->getAttributeId());
$attribute->setIsUserDefined($existingModel->getIsUserDefined());
$attribute->setFrontendInput($existingModel->getFrontendInput());
if (is_array($attribute->getFrontendLabels())) {
$frontendLabel[0] = $existingModel->getDefaultFrontendLabel();
foreach ($attribute->getFrontendLabels() as $item) {
$frontendLabel[$item->getStoreId()] = $item->getLabel();
}
$attribute->setDefaultFrontendLabel($frontendLabel);
}
if (!$attribute->getIsUserDefined()) {
// Unset attribute field for system attributes
$attribute->setApplyTo(null);
}
} else {
$attribute->setAttributeId(null);
if (!$attribute->getFrontendLabels() && !$attribute->getDefaultFrontendLabel()) {
throw InputException::requiredField('frontend_label');
}
$frontendLabels = [];
if ($attribute->getDefaultFrontendLabel()) {
$frontendLabels[0] = $attribute->getDefaultFrontendLabel();
}
if ($attribute->getFrontendLabels() && is_array($attribute->getFrontendLabels())) {
foreach ($attribute->getFrontendLabels() as $label) {
$frontendLabels[$label->getStoreId()] = $label->getLabel();
}
if (!isset($frontendLabels[0]) || !$frontendLabels[0]) {
throw InputException::invalidFieldValue('frontend_label', null);
}
$attribute->setDefaultFrontendLabel($frontendLabels);
}
$attribute->setAttributeCode($attribute->getAttributeCode() ?: $this->generateCode($frontendLabels[0]));
$this->validateCode($attribute->getAttributeCode());
$this->validateFrontendInput($attribute->getFrontendInput());
$attribute->setBackendType($attribute->getBackendTypeByInput($attribute->getFrontendInput()));
$attribute->setSourceModel($this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput()));
$attribute->setBackendModel($this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput()));
$attribute->setEntityTypeId($this->eavConfig->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)->getId());
$attribute->setIsUserDefined(1);
}
$this->attributeResource->save($attribute);
foreach ($attribute->getOptions() as $option) {
$this->optionManagement->add($attribute->getAttributeCode(), $option);
}
return $this->get($attribute->getAttributeCode());
}
示例3: install
/**
* @param array $fixtures
* @throws \Exception
*/
public function install(array $fixtures)
{
$attributeCount = 0;
foreach ($fixtures as $fileName) {
$fileName = $this->fixtureManager->getFixture($fileName);
if (!file_exists($fileName)) {
continue;
}
$rows = $this->csvReader->getData($fileName);
$header = array_shift($rows);
foreach ($rows as $row) {
$data = [];
foreach ($row as $key => $value) {
$data[$header[$key]] = $value;
}
$data['attribute_set'] = explode("\n", $data['attribute_set']);
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
$attribute = $this->eavConfig->getAttribute('catalog_product', $data['attribute_code']);
if (!$attribute) {
$attribute = $this->attributeFactory->create();
}
$frontendLabel = explode("\n", $data['frontend_label']);
if (count($frontendLabel) > 1) {
$data['frontend_label'] = [];
$data['frontend_label'][\Magento\Store\Model\Store::DEFAULT_STORE_ID] = $frontendLabel[0];
$data['frontend_label'][$this->storeManager->getDefaultStoreView()->getStoreId()] = $frontendLabel[1];
}
$data['option'] = $this->getOption($attribute, $data);
$data['source_model'] = $this->productHelper->getAttributeSourceModelByInputType($data['frontend_input']);
$data['backend_model'] = $this->productHelper->getAttributeBackendModelByInputType($data['frontend_input']);
$data += ['is_filterable' => 0, 'is_filterable_in_search' => 0, 'apply_to' => []];
$data['backend_type'] = $attribute->getBackendTypeByInput($data['frontend_input']);
$attribute->addData($data);
$attribute->setIsUserDefined(1);
$attribute->setEntityTypeId($this->getEntityTypeId());
$attribute->save();
$attributeId = $attribute->getId();
if (is_array($data['attribute_set'])) {
foreach ($data['attribute_set'] as $setName) {
$setName = trim($setName);
$attributeCount++;
$attributeSet = $this->processAttributeSet($setName);
$attributeGroupId = $attributeSet->getDefaultGroupId();
$attribute = $this->attributeFactory->create();
$attribute->setId($attributeId)->setAttributeGroupId($attributeGroupId)->setAttributeSetId($attributeSet->getId())->setEntityTypeId($this->getEntityTypeId())->setSortOrder($attributeCount + 999)->save();
}
}
}
}
$this->eavConfig->clear();
}
示例4: run
/**
* {@inheritdoc}
*/
public function run()
{
$this->logger->log('Installing catalog attributes:');
$attributeCount = 0;
foreach ($this->moduleList->getNames() as $moduleName) {
$fileName = substr($moduleName, strpos($moduleName, "_") + 1) . '/attributes.csv';
$fileName = $this->fixtureHelper->getPath($fileName);
if (!$fileName) {
continue;
}
$csvReader = $this->csvReaderFactory->create(['fileName' => $fileName, 'mode' => 'r']);
foreach ($csvReader as $data) {
$data['attribute_set'] = explode("\n", $data['attribute_set']);
/** @var \Magento\Catalog\Model\Resource\Eav\Attribute $attribute */
$attribute = $this->eavConfig->getAttribute('catalog_product', $data['attribute_code']);
if (!$attribute) {
$attribute = $this->attributeFactory->create();
}
$frontendLabel = explode("\n", $data['frontend_label']);
if (count($frontendLabel) > 1) {
$data['frontend_label'] = [];
$data['frontend_label'][\Magento\Store\Model\Store::DEFAULT_STORE_ID] = $frontendLabel[0];
$data['frontend_label'][$this->storeManager->getStoreId()] = $frontendLabel[1];
}
$data['option'] = $this->getOption($attribute, $data);
$data['source_model'] = $this->productHelper->getAttributeSourceModelByInputType($data['frontend_input']);
$data['backend_model'] = $this->productHelper->getAttributeBackendModelByInputType($data['frontend_input']);
$data += ['is_filterable' => 0, 'is_filterable_in_search' => 0, 'apply_to' => []];
$data['backend_type'] = $attribute->getBackendTypeByInput($data['frontend_input']);
$attribute->addData($data);
$attribute->setIsUserDefined(1);
$attribute->setEntityTypeId($this->getEntityTypeId());
$attribute->save();
$attributeId = $attribute->getId();
if (is_array($data['attribute_set'])) {
foreach ($data['attribute_set'] as $setName) {
$setName = trim($setName);
$attributeCount++;
$attributeSet = $this->processAttributeSet($setName);
$attributeGroupId = $attributeSet->getDefaultGroupId();
$attribute = $this->attributeFactory->create();
$attribute->setId($attributeId)->setAttributeGroupId($attributeGroupId)->setAttributeSetId($attributeSet->getId())->setEntityTypeId($this->getEntityTypeId())->setSortOrder($attributeCount + 999)->save();
}
}
$this->logger->logInline('.');
}
}
$this->eavConfig->clear();
}
示例5: execute
/**
* Add new product attrubute into all attribute sets.
* If attribute is already exists - skip.
*
* @param array $data Array of slider data arrays
* <pre>
* [
* attribute_code
* is_global
* frontend_input [text|boolean|textarea|select|price|media_image|etc]
* default_value_text
* is_searchable
* is_visible_in_advanced_search
* is_comparable
* frontend_label array
* sort_order Set 0 to use MaxSortOrder
* ]
* </pre>
* @return void
*/
public function execute($data)
{
$defaults = array('is_global' => 0, 'frontend_input' => 'boolean', 'is_configurable' => 0, 'is_filterable' => 0, 'is_filterable_in_search' => 0, 'sort_order' => 1);
$entityTypeId = $this->objectManager->create('Magento\\Eav\\Model\\Entity')->setType(\Magento\Catalog\Model\Product::ENTITY)->getTypeId();
$attributeSets = $this->objectManager->create('Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Set\\Collection')->setEntityTypeFilter($entityTypeId);
foreach ($data as $itemData) {
/* @var $model \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
$model = $this->attributeFactory->create()->load($itemData['attribute_code'], 'attribute_code');
if ($model->getId()) {
continue;
}
$itemData = array_merge($itemData, $defaults);
$itemData['source_model'] = $this->productHelper->getAttributeSourceModelByInputType($itemData['frontend_input']);
$itemData['backend_model'] = $this->productHelper->getAttributeBackendModelByInputType($itemData['frontend_input']);
$itemData['backend_type'] = $model->getBackendTypeByInput($itemData['frontend_input']);
$model->addData($itemData);
$model->setEntityTypeId($entityTypeId);
$model->setIsUserDefined(1);
foreach ($attributeSets as $set) {
$model->setAttributeSetId($set->getId());
$model->setAttributeGroupId($set->getDefaultGroupId());
try {
$model->save();
} catch (\Exception $e) {
$this->fault('product_attribute_save', $e);
}
}
if (!$attributeSets->count()) {
try {
$model->save();
} catch (\Exception $e) {
$this->fault('product_attribute_save', $e);
}
}
}
}
示例6: testGetAttributeBackendModelByInputType
public function testGetAttributeBackendModelByInputType()
{
$this->assertNotEmpty($this->_helper->getAttributeBackendModelByInputType('multiselect'));
$this->assertNull($this->_helper->getAttributeBackendModelByInputType('boolean'));
}
示例7: execute
/**
* @return \Magento\Backend\Model\View\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function execute()
{
$data = $this->getRequest()->getPostValue();
$resultRedirect = $this->resultRedirectFactory->create();
if ($data) {
$setId = $this->getRequest()->getParam('set');
$attributeSet = null;
if (!empty($data['new_attribute_set_name'])) {
$name = $this->filterManager->stripTags($data['new_attribute_set_name']);
$name = trim($name);
try {
/** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */
$attributeSet = $this->buildFactory->create()->setEntityTypeId($this->_entityTypeId)->setSkeletonId($setId)->setName($name)->getAttributeSet();
} catch (AlreadyExistsException $alreadyExists) {
$this->messageManager->addError(__('An attribute set named \'%1\' already exists.', $name));
$this->messageManager->setAttributeData($data);
return $resultRedirect->setPath('catalog/*/edit', ['_current' => true]);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while saving the attribute.'));
}
}
$redirectBack = $this->getRequest()->getParam('back', false);
/* @var $model \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
$model = $this->attributeFactory->create();
$attributeId = $this->getRequest()->getParam('attribute_id');
$attributeCode = $this->getRequest()->getParam('attribute_code');
$frontendLabel = $this->getRequest()->getParam('frontend_label');
$attributeCode = $attributeCode ?: $this->generateCode($frontendLabel[0]);
if (strlen($this->getRequest()->getParam('attribute_code')) > 0) {
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,30}$/']);
if (!$validatorAttrCode->isValid($attributeCode)) {
$this->messageManager->addError(__('Attribute code "%1" is invalid. Please use only letters (a-z), ' . 'numbers (0-9) or underscore(_) in this field, first character should be a letter.', $attributeCode));
return $resultRedirect->setPath('catalog/*/edit', ['attribute_id' => $attributeId, '_current' => true]);
}
}
$data['attribute_code'] = $attributeCode;
//validate frontend_input
if (isset($data['frontend_input'])) {
/** @var $inputType \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator */
$inputType = $this->validatorFactory->create();
if (!$inputType->isValid($data['frontend_input'])) {
foreach ($inputType->getMessages() as $message) {
$this->messageManager->addError($message);
}
return $resultRedirect->setPath('catalog/*/edit', ['attribute_id' => $attributeId, '_current' => true]);
}
}
if ($attributeId) {
$model->load($attributeId);
if (!$model->getId()) {
$this->messageManager->addError(__('This attribute no longer exists.'));
return $resultRedirect->setPath('catalog/*/');
}
// entity type check
if ($model->getEntityTypeId() != $this->_entityTypeId) {
$this->messageManager->addError(__('We can\'t update the attribute.'));
$this->_session->setAttributeData($data);
return $resultRedirect->setPath('catalog/*/');
}
$data['attribute_code'] = $model->getAttributeCode();
$data['is_user_defined'] = $model->getIsUserDefined();
$data['frontend_input'] = $model->getFrontendInput();
} else {
/**
* @todo add to helper and specify all relations for properties
*/
$data['source_model'] = $this->productHelper->getAttributeSourceModelByInputType($data['frontend_input']);
$data['backend_model'] = $this->productHelper->getAttributeBackendModelByInputType($data['frontend_input']);
}
$data += ['is_filterable' => 0, 'is_filterable_in_search' => 0, 'apply_to' => []];
if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
$data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
}
$defaultValueField = $model->getDefaultValueByInput($data['frontend_input']);
if ($defaultValueField) {
$data['default_value'] = $this->getRequest()->getParam($defaultValueField);
}
if (!$model->getIsUserDefined() && $model->getId()) {
// Unset attribute field for system attributes
unset($data['apply_to']);
}
$model->addData($data);
if (!$attributeId) {
$model->setEntityTypeId($this->_entityTypeId);
$model->setIsUserDefined(1);
}
$groupCode = $this->getRequest()->getParam('group');
if ($setId && $groupCode) {
// For creating product attribute on product page we need specify attribute set and group
$attributeSetId = $attributeSet ? $attributeSet->getId() : $setId;
$groupCollection = $attributeSet ? $attributeSet->getGroups() : $this->groupCollectionFactory->create()->setAttributeSetFilter($attributeSetId)->load();
foreach ($groupCollection as $group) {
//.........这里部分代码省略.........