123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Attribute add/edit form options tab
- *
- * @author Magento Core Team <core@magentocommerce.com>
- */
- namespace Magento\Eav\Block\Adminhtml\Attribute\Edit\Options;
- use Magento\Store\Model\ResourceModel\Store\Collection;
- /**
- * @api
- * @since 100.0.2
- */
- class Options extends \Magento\Backend\Block\Template
- {
- /**
- * @var \Magento\Framework\Registry
- */
- protected $_registry;
- /**
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory
- */
- protected $_attrOptionCollectionFactory;
- /**
- * @var string
- */
- protected $_template = 'Magento_Catalog::catalog/product/attribute/options.phtml';
- /**
- * @var \Magento\Framework\Validator\UniversalFactory $universalFactory
- */
- protected $_universalFactory;
- /**
- * @param \Magento\Backend\Block\Template\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
- * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
- * @param array $data
- */
- public function __construct(
- \Magento\Backend\Block\Template\Context $context,
- \Magento\Framework\Registry $registry,
- \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
- \Magento\Framework\Validator\UniversalFactory $universalFactory,
- array $data = []
- ) {
- parent::__construct($context, $data);
- $this->_registry = $registry;
- $this->_attrOptionCollectionFactory = $attrOptionCollectionFactory;
- $this->_universalFactory = $universalFactory;
- }
- /**
- * Is true only for system attributes which use source model
- * Option labels and position for such attributes are kept in source model and thus cannot be overridden
- *
- * @return bool
- */
- public function canManageOptionDefaultOnly()
- {
- $attribute = $this->getAttributeObject();
- return !$attribute->getCanManageOptionLabels() &&
- !$attribute->getIsUserDefined() &&
- $attribute->getSourceModel();
- }
- /**
- * Retrieve stores collection with default store
- *
- * @return array
- */
- public function getStores()
- {
- if (!$this->hasStores()) {
- $this->setData('stores', $this->_storeManager->getStores(true));
- }
- return $this->_getData('stores');
- }
- /**
- * Returns stores sorted by Sort Order
- *
- * @return array
- * @since 100.1.0
- */
- public function getStoresSortedBySortOrder()
- {
- $stores = $this->getStores();
- if (is_array($stores)) {
- usort($stores, function ($storeA, $storeB) {
- if ($storeA->getSortOrder() == $storeB->getSortOrder()) {
- return $storeA->getId() < $storeB->getId() ? -1 : 1;
- }
- return ($storeA->getSortOrder() < $storeB->getSortOrder()) ? -1 : 1;
- });
- }
- return $stores;
- }
- /**
- * Retrieve attribute option values if attribute input type select or multiselect
- *
- * @return array
- */
- public function getOptionValues()
- {
- $values = $this->_getData('option_values');
- if ($values === null) {
- $values = [];
- $attribute = $this->getAttributeObject();
- $optionCollection = $this->_getOptionValuesCollection($attribute);
- if ($optionCollection) {
- $values = $this->_prepareOptionValues($attribute, $optionCollection);
- }
- $this->setData('option_values', $values);
- }
- return $values;
- }
- /**
- * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
- * @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $optionCollection
- * @return array
- */
- protected function _prepareOptionValues(
- \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute,
- $optionCollection
- ) {
- $type = $attribute->getFrontendInput();
- if ($type === 'select' || $type === 'multiselect') {
- $defaultValues = explode(',', $attribute->getDefaultValue());
- $inputType = $type === 'select' ? 'radio' : 'checkbox';
- } else {
- $defaultValues = [];
- $inputType = '';
- }
- $values = [];
- $isSystemAttribute = is_array($optionCollection);
- foreach ($optionCollection as $option) {
- $bunch = $isSystemAttribute ? $this->_prepareSystemAttributeOptionValues(
- $option,
- $inputType,
- $defaultValues
- ) : $this->_prepareUserDefinedAttributeOptionValues(
- $option,
- $inputType,
- $defaultValues
- );
- foreach ($bunch as $value) {
- $values[] = new \Magento\Framework\DataObject($value);
- }
- }
- return $values;
- }
- /**
- * Retrieve option values collection
- * It is represented by an array in case of system attribute
- *
- * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
- * @return array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
- */
- protected function _getOptionValuesCollection(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute)
- {
- if ($this->canManageOptionDefaultOnly()) {
- $options = $this->_universalFactory->create(
- $attribute->getSourceModel()
- )->setAttribute(
- $attribute
- )->getAllOptions();
- return $options;
- } else {
- return $this->_attrOptionCollectionFactory->create()->setAttributeFilter(
- $attribute->getId()
- )->setPositionOrder(
- 'asc',
- true
- )->load();
- }
- }
- /**
- * Prepare option values of system attribute
- *
- * @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option $option
- * @param string $inputType
- * @param array $defaultValues
- * @param string $valuePrefix
- * @return array
- */
- protected function _prepareSystemAttributeOptionValues($option, $inputType, $defaultValues, $valuePrefix = '')
- {
- if (is_array($option['value'])) {
- $values = [];
- foreach ($option['value'] as $subOption) {
- $bunch = $this->_prepareSystemAttributeOptionValues(
- $subOption,
- $inputType,
- $defaultValues,
- $option['label'] . ' / '
- );
- $values[] = $bunch[0];
- }
- return $values;
- }
- $value['checked'] = in_array($option['value'], $defaultValues) ? 'checked="checked"' : '';
- $value['intype'] = $inputType;
- $value['id'] = $option['value'];
- $value['sort_order'] = 0;
- foreach ($this->getStores() as $store) {
- $storeId = $store->getId();
- $value['store' . $storeId] = $storeId ==
- \Magento\Store\Model\Store::DEFAULT_STORE_ID ? $valuePrefix . $this->escapeHtml($option['label']) : '';
- }
- return [$value];
- }
- /**
- * Prepare option values of user defined attribute
- *
- * @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option $option
- * @param string $inputType
- * @param array $defaultValues
- * @return array
- */
- protected function _prepareUserDefinedAttributeOptionValues($option, $inputType, $defaultValues)
- {
- $optionId = $option->getId();
- $value['checked'] = in_array($optionId, $defaultValues) ? 'checked="checked"' : '';
- $value['intype'] = $inputType;
- $value['id'] = $optionId;
- $value['sort_order'] = $option->getSortOrder();
- foreach ($this->getStores() as $store) {
- $storeId = $store->getId();
- $storeValues = $this->getStoreOptionValues($storeId);
- $value['store' . $storeId] = isset(
- $storeValues[$optionId]
- ) ? $this->escapeHtml(
- $storeValues[$optionId]
- ) : '';
- }
- return [$value];
- }
- /**
- * Retrieve attribute option values for given store id
- *
- * @param int $storeId
- * @return array
- */
- public function getStoreOptionValues($storeId)
- {
- $values = $this->getData('store_option_values_' . $storeId);
- if ($values === null) {
- $values = [];
- $valuesCollection = $this->_attrOptionCollectionFactory->create()->setAttributeFilter(
- $this->getAttributeObject()->getId()
- )->setStoreFilter(
- $storeId,
- false
- )->load();
- foreach ($valuesCollection as $item) {
- $values[$item->getId()] = $item->getValue();
- }
- $this->setData('store_option_values_' . $storeId, $values);
- }
- return $values;
- }
- /**
- * Retrieve attribute object from registry
- *
- * @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute
- * @codeCoverageIgnore
- */
- protected function getAttributeObject()
- {
- return $this->_registry->registry('entity_attribute');
- }
- }
|