0

I am attempting to add an additional category custom attribute to an existing module. A previous developer had already added two custom attributes and I am working on adding a third. I am able to get the third custom attribute to display in the backend but when I save any value inside the field, the new custom attribute does not save but I am able to modify the two existing custom attributes.

Any assistance to resolve this issue is appreciated.

InstallData.php

<?php

namespace WholeLatteLove\CategoryMedia\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Catalog\Model\Category;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;

/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
 * EAV setup factory
 *
 * @var EavSetupFactory
 */
private $eavSetupFactory;

/**
 * Init
 *
 * @param EavSetupFactory $eavSetupFactory
 */
public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    /**
     * Add attributes to the eav/attribute
     */

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'header_img',
        [
            'type' => 'varchar',
            'label' => 'Header Image',
            'input' => 'image',
            'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image',
            'required' => false,
            'sort_order' => 5,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
        ]
    );

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'youtube_id',
        [
            'group' => 'General Information',
            'type' => 'varchar',
            'label' => 'YouTube ID',
            'input' => 'text',
            'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Varchar',
            'sort_order' => 5,
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => '',
        ]
    );

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'cinematic_img',
        [
            'type' => 'varchar',
            'label' => 'Cinematic Image',
            'input' => 'image',
            'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image',
            'required' => false,
            'sort_order' => 5,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
        ]
    );

    }
}

category_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="catvideoattribute">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Youtube ID</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">100</item>
        </item>
    </argument>
    <field name="youtube_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">1</item>
                <item name="dataType" xsi:type="string">string</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="label" xsi:type="string" translate="true">Youtube Video ID</item>
            </item>
        </argument>
    </field>
</fieldset>

<fieldset name="headerimgattribute">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Header Image</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">101</item>
        </item>
    </argument>
    <field name="header_img">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">2</item>
                <item name="dataType" xsi:type="string">image</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="label" xsi:type="string" translate="true">Header Image</item>
            </item>
        </argument>
    </field>
</fieldset>

<fieldset name="cinematicimgattribute">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Cinematic Image</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">102</item>
        </item>
    </argument>
    <field name="cinematic_img">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">3</item>
                <item name="dataType" xsi:type="string">image</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="label" xsi:type="string" translate="true">Cinematic Image</item>
            </item>
        </argument>
    </field>
</fieldset>

0

This issue has been solved. Since the module was already installed, I had to update the module by using a UpgradeData.php file. I removed the new custom attribute from the Install.php file and placed it into a new UpgradeData.php file.

I also needed to update the composer.json and module.xml file with a new version number.

Without using the UpgradeData.php, the database schema was not updating and that was why the new attribute values were not saving.

InstallData.php

<?php

namespace WholeLatteLove\CategoryMedia\Setup;

use Magento\Framework\Setup\{
ModuleContextInterface,
ModuleDataSetupInterface,
InstallDataInterface
};

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
/**
 * EAV setup factory
 *
 * @var EavSetupFactory
 */
private $eavSetupFactory;

/**
 * Init
 *
 * @param EavSetupFactory $eavSetupFactory
 */
public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    /**
     * Add attributes to the eav/attribute
     */

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'header_img',
        [
            'type' => 'varchar',
            'label' => 'Header Image',
            'input' => 'image',
            'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image',
            'required' => false,
            'sort_order' => 5,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
        ]
    );

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'youtube_id',
        [
            'group' => 'General Information',
            'type' => 'varchar',
            'label' => 'YouTube ID',
            'input' => 'text',
            'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Varchar',
            'sort_order' => 5,
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => '',
        ]
    );


  }
}

UpgradeData.php

<?php

namespace WholeLatteLove\CategoryMedia\Setup;

use Magento\Framework\Setup\{
ModuleContextInterface,
ModuleDataSetupInterface,
UpgradeDataInterface
};

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

/**
 * @codeCoverageIgnore
 */
class UpgradeData implements UpgradeDataInterface
{
/**
 * EAV setup factory
 *
 * @var EavSetupFactory
 */
private $eavSetupFactory;

/**
 * Init
 *
 * @param EavSetupFactory $eavSetupFactory
 */
public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();

    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    /**
     * Add attributes to the eav/attribute
     */

    if ($context->getVersion() && version_compare($context->getVersion(), '1.1.0') < 0) {

      $eavSetup->addAttribute(
          \Magento\Catalog\Model\Category::ENTITY,
          'cinematic_img',
          [
              'type' => 'varchar',
              'label' => 'Cinematic Image',
              'input' => 'image',
              'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image',
              'required' => false,
              'sort_order' => 5,
              'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
              'group' => 'General Information',
          ]
      );

    }

    $setup->endSetup();

  }
}

category_form.xml is same as above.

composer.json

{
  "name": "wholelattelove/categorymedia",
  "description": "N/A",
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0"
  },
  "type": "magento2-module",
  "version": "1.1.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "autoload": {
    "files": [ "registration.php" ],
    "psr-4": {
      "WholeLatteLove\\CategoryMedia\\": ""
    }
  }
}

module.xml

<?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="WholeLatteLove_CategoryMedia" setup_version="1.1.0">
         <sequence>
             <module name="Magento_Catalog"/>
         </sequence>
     </module>
 </config>
  • This is not working. Can you please double check the code you have provided? – Cypher909 Dec 7 '17 at 17:05
  • Can you clarify the issue you are having? – anaron Dec 8 '17 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.