Esempio n. 1
0
 /**
  * Prepares values options to be used as select options or hashed array
  * Result is stored in following keys:
  *  'value_select_options' - normal select array: array(array('value' => $value, 'label' => $label), ...)
  *  'value_option' - hashed array: array($value => $label, ...),
  *
  * @return $this
  */
 protected function _prepareValueOptions()
 {
     // Check that both keys exist. Maybe somehow only one was set not in this routine, but externally.
     $selectReady = $this->getData('value_select_options');
     $hashedReady = $this->getData('value_option');
     if ($selectReady && $hashedReady) {
         return $this;
     }
     // Get array of select options. It will be used as source for hashed options
     $selectOptions = null;
     if ($this->getAttribute() === 'attribute_set_id') {
         $entityTypeId = $this->_config->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId();
         $selectOptions = $this->_attrSetCollection->setEntityTypeFilter($entityTypeId)->load()->toOptionArray();
     } elseif ($this->getAttribute() === 'type_id') {
         foreach ($selectReady as $value => $label) {
             if (is_array($label) && isset($label['value'])) {
                 $selectOptions[] = $label;
             } else {
                 $selectOptions[] = array('value' => $value, 'label' => $label);
             }
         }
         $selectReady = null;
     } elseif (is_object($this->getAttributeObject())) {
         $attributeObject = $this->getAttributeObject();
         if ($attributeObject->usesSource()) {
             if ($attributeObject->getFrontendInput() == 'multiselect') {
                 $addEmptyOption = false;
             } else {
                 $addEmptyOption = true;
             }
             $selectOptions = $attributeObject->getSource()->getAllOptions($addEmptyOption);
         }
     }
     // Set new values only if we really got them
     if ($selectOptions !== null) {
         // Overwrite only not already existing values
         if (!$selectReady) {
             $this->setData('value_select_options', $selectOptions);
         }
         if (!$hashedReady) {
             $hashedOptions = array();
             foreach ($selectOptions as $option) {
                 if (is_array($option['value'])) {
                     continue;
                     // We cannot use array as index
                 }
                 $hashedOptions[$option['value']] = $option['label'];
             }
             $this->setData('value_option', $hashedOptions);
         }
     }
     return $this;
 }