public function testToOptionArray() { $expect = [['label' => __('-- Please Select a Category --'), 'value' => ''], ['label' => 'name', 'value' => 3]]; $this->categoryCollection->expects($this->once())->method('addAttributeToSelect')->with($this->equalTo('name'))->will($this->returnValue($this->categoryCollection)); $this->categoryCollection->expects($this->once())->method('addRootLevelFilter')->will($this->returnValue($this->categoryCollection)); $this->categoryCollection->expects($this->once())->method('load'); $this->categoryCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$this->category]))); $this->category->expects($this->once())->method('getName')->will($this->returnValue('name')); $this->category->expects($this->once())->method('getId')->will($this->returnValue(3)); $this->assertEquals($expect, $this->model->toOptionArray()); }
/** * Add attribute to filter * * @param int $storeId * @param string $attributeCode * @param mixed $value * @param string $type * @return \Zend_Db_Select|bool */ protected function _addFilter($storeId, $attributeCode, $value, $type = '=') { if (!$this->_select instanceof \Zend_Db_Select) { return false; } if (!isset($this->_attributesCache[$attributeCode])) { $attribute = $this->_categoryResource->getAttribute($attributeCode); $this->_attributesCache[$attributeCode] = array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getId(), 'table' => $attribute->getBackend()->getTable(), 'is_global' => $attribute->getIsGlobal(), 'backend_type' => $attribute->getBackendType()); } $attribute = $this->_attributesCache[$attributeCode]; switch ($type) { case '=': $conditionRule = '=?'; break; case 'in': $conditionRule = ' IN(?)'; break; default: return false; break; } if ($attribute['backend_type'] == 'static') { $this->_select->where('e.' . $attributeCode . $conditionRule, $value); } else { $this->_select->join(array('t1_' . $attributeCode => $attribute['table']), 'e.entity_id = t1_' . $attributeCode . '.entity_id AND t1_' . $attributeCode . '.store_id = 0', array())->where('t1_' . $attributeCode . '.attribute_id=?', $attribute['attribute_id']); if ($attribute['is_global']) { $this->_select->where('t1_' . $attributeCode . '.value' . $conditionRule, $value); } else { $ifCase = $this->_select->getAdapter()->getCheckSql('t2_' . $attributeCode . '.value_id > 0', 't2_' . $attributeCode . '.value', 't1_' . $attributeCode . '.value'); $this->_select->joinLeft(array('t2_' . $attributeCode => $attribute['table']), $this->_getWriteAdapter()->quoteInto('t1_' . $attributeCode . '.entity_id = t2_' . $attributeCode . '.entity_id AND t1_' . $attributeCode . '.attribute_id = t2_' . $attributeCode . '.attribute_id AND t2_' . $attributeCode . '.store_id=?', $storeId), array())->where('(' . $ifCase . ')' . $conditionRule, $value); } } return $this->_select; }
/** * Validate category process * * @param Category $category * @return void * @throws \Magento\Framework\Exception\LocalizedException */ protected function validateCategory(Category $category) { $useConfigFields = []; foreach ($this->useConfigFields as $field) { if (!$category->getData($field)) { $useConfigFields[] = $field; } } $category->setData('use_post_data_config', $useConfigFields); $validate = $category->validate(); if ($validate !== true) { foreach ($validate as $code => $error) { if ($error === true) { $attribute = $this->categoryResource->getAttribute($code)->getFrontend()->getLabel(); throw new \Magento\Framework\Exception\LocalizedException(__('Attribute "%1" is required.', $attribute)); } else { throw new \Magento\Framework\Exception\LocalizedException(__($error)); } } } $category->unsetData('use_post_data_config'); }
/** * Prepare chooser element HTML * * @param AbstractElement $element Form Element * @return AbstractElement */ public function prepareElementHtml(AbstractElement $element) { $uniqId = $this->mathRandom->getUniqueHash($element->getId()); $sourceUrl = $this->getUrl('catalog/product_widget/chooser', ['uniq_id' => $uniqId, 'use_massaction' => false]); $chooser = $this->getLayout()->createBlock('Magento\\Widget\\Block\\Adminhtml\\Widget\\Chooser')->setElement($element)->setConfig($this->getConfig())->setFieldsetId($this->getFieldsetId())->setSourceUrl($sourceUrl)->setUniqId($uniqId); if ($element->getValue()) { $value = explode('/', $element->getValue()); $productId = false; if (isset($value[0]) && isset($value[1]) && $value[0] == 'product') { $productId = $value[1]; } $categoryId = isset($value[2]) ? $value[2] : false; $label = ''; if ($categoryId) { $label = $this->_resourceCategory->getAttributeRawValue($categoryId, 'name', $this->_storeManager->getStore()) . '/'; } if ($productId) { $label .= $this->_resourceProduct->getAttributeRawValue($productId, 'name', $this->_storeManager->getStore()); } $chooser->setLabel($label); } $element->setData('after_element_html', $chooser->toHtml()); return $element; }
/** * Executing parents move method and cleaning cache after it * * @param mixed $category * @param mixed $newParent * @param mixed $prevNode * @return void */ public function move($category, $newParent, $prevNode = null) { $this->_catalogCategory->move($category->getId(), $newParent->getId()); parent::move($category, $newParent, $prevNode); $this->_afterMove(); }