Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Custom product attribute to select a CMS page

I want to add a new product attribute that renders a drop-down list with all CMS pages as its options. I created an extension and wanted to add this attribute via the install script. but after running this, Magento corrupted the eav_entity_type table...

What I tried so far:

mysql4-install-0.1.0.php:

$installer = $this;

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$installer->startSetup();

$setup->addAttribute('catalog_product', 'test_report', array(
    'label' => 'Test report',
    'required' => false,
    'input' => 'select',
    'source' => 'cmspageselect/entity_source',
    'default' => 'none',
    'position' => 1,
    'sort_order' => 3,
));

$installer->endSetup();

Source for cmspageselect/entity_source class:

class Mandarin_CMSPageSelect_Model_Source extends Mage_Eav_Model_Entity_Attribute_Source_Abstract {
    /**
    * Retrieve Full Option values array
    *
    * @param bool $withEmpty       Add empty option to array
    * @return array
    */
    public function getAllOptions($withEmpty = true)
    {
        $storeId = $this->getAttribute()->getStoreId();
        if (!is_array($this->_options)) {
            $this->_options = array();
        }
        if (!isset($this->_options[$storeId])) {
            $collection = Mage::getResourceModel('cms/page_collection')
                ->setPositionOrder('asc')
                ->setStoreFilter($this->getAttribute()->getStoreId())
                ->load();
            $this->_options[$storeId] = $collection->toOptionIdArray();
        }

        $options = $this->_options[$storeId];
        if ($withEmpty) {
            array_unshift($options, array('label' => '', 'value' => ''));
        }

        return $options;
    }

    /**
    * Get a text for option value
    *
    * @param string|integer $value
    * @return string
    */
    public function getOptionText($value)
    {
        $isMultiple = false;
        if (strpos($value, ',')) {
            $isMultiple = true;
            $value = explode(',', $value);
        }

        $options = $this->getAllOptions(false);

        if ($isMultiple) {
            $values = array();
            foreach ($options as $item) {
                if (in_array($item['value'], $value)) {
                    $values[] = $item['label'];
                }
            }
            return $values;
        }

        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }
}

As I said, instead of creating the new attribute "test_report", Magento just corrupted the eav_entity_type table. There was no error message nor log entry about what happened.

How can I create such an attribute?

Thanks, aeno

like image 690
aeno Avatar asked Jan 18 '12 08:01

aeno


1 Answers

The $installer class itself should be able to add the attribute, I don't believe you need to load the Entity_Setup class.

$installer = $this;
$installer->startSetup();

$installer->addAttribute('catalog_product', 'test_report', array(
    'label' => 'Test report',
    'required' => false,
    'input' => 'select',
    'source' => 'cmspageselect/entity_source',
    'default' => 'none',
    'position' => 1,
    'sort_order' => 3,
));

$installer->endSetup();

Other than that I think your source value is incorrect, judging by the source element in the array, you should be naming your model Mandarin_CMSPageSelect_Model_Entity_Source. I am assuming that you have added in the appropriate <models> element into config.xml, in order to tell Magento where to load your model from.

like image 61
Peter O'Callaghan Avatar answered Sep 29 '22 13:09

Peter O'Callaghan