Magento, Item Attributes and Images

By mrbagnall | 10 July 2015

Lately I've been working on a headless integration of Magento with Drupal 7. The task is pretty straight forward even if the implementation has its challenges. One of the first I have encountered id synchronizing attribute fields (used in products and attribute sets for types of products) with Drupal. The Magento API does allow you to dump the attributes of a set via the API, but there are fields that are hard-coded which it will ignore. Among these include gallery and image media files. Because these are hard-coded it will require overriding the constructs for two of the API classes in Magento core. This includes overriding the Product and Attribute API models of the Catalog class.

The _ignoredAttributeCodes and _ignoredAttributeTypes arrays need to be re-set in /Catalog/Model/Attribute/Api.php to reflect the following changes:

$this->_storeIdSessionField = 'product_store_id';
//$this->_ignoredAttributeCodes[] = 'type_id';
//$this->_ignoredAttributeTypes[] = 'gallery';
//$this->_ignoredAttributeTypes[] = 'media_image';
$this->_entityTypeId = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();

THe same variables need to be re-set or nullified in /Catalog/Model/Product/Api.php:

//$this->_ignoredAttributeTypes[] = 'gallery';
//$this->_ignoredAttributeTypes[] = 'media_image';

This stems from the API's insistence on checking these variables for ignored types in the _isAllowedAttribute method inside /app/code/core/Mage/Catalog/Model/Api/Resource.php:

   protected function _isAllowedAttribute($attribute, $attributes = null)
    {

        if (Mage::getSingleton('api/server')->getApiName() == 'rest') {
            if (!$this->_checkAttributeAcl($attribute)) {
                return false;
            }
        }

        if (is_array($attributes)
            && !( in_array($attribute->getAttributeCode(), $attributes)
                  || in_array($attribute->getAttributeId(), $attributes))) {
            return false;
        }

        return !in_array($attribute->getFrontendInput(), $this->_ignoredAttributeTypes)
               && !in_array($attribute->getAttributeCode(), $this->_ignoredAttributeCodes);
    }

The highlighted lines indicate where the point of failure occurs assuming the permissions on the fields for the API user are set correctly. Instead of commenting out those two lines, we should seek just to re-define the variables.

I have looked for other ways to accomplish this but given the hard-coded nature and the fact the hard coding occurs within a constructor leads me to think the class itself must be overridden. I am open to other solutions if you wish to leave them in the comments.