3

I have two custom attributes for categories. The code for both is similar apart from the attribute_code but one is not being saved when trying to add a value in adminhtml.

That's the UpgradeData.php where I add the attribute that's not saved:

$eavSetup->addAttribute(Category::ENTITY, 'megamenu_side_block', [
    'type' => 'text',
    'input' => 'textarea',
    'label' => 'Menu Side Block',
    'required' => 0,
    'user_defined' => 1,
    'wysiwyg_enabled' => 1,
    'is_html_allowed_on_front' => 1,
    'global' => ScopedAttributeInterface::SCOPE_WEBSITE,
]);

That's the part of category_form.xml where I create the adminhtml field:

<field name="megamenu_side_block" template="ui/form/field" sortOrder="51" formElement="wysiwyg">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="wysiwygConfigData" xsi:type="array">
                <item name="settings" xsi:type="array">
                    <item name="theme_advanced_buttons1" xsi:type="string">bold,italic,|,justifyleft,justifycenter,justifyright,|,fontselect,fontsizeselect,|,forecolor,backcolor,|,link,unlink,image,|,bullist,numlist,|,code
                    </item>
                    <item name="theme_advanced_buttons2" xsi:type="boolean">false</item>
                    <item name="theme_advanced_buttons3" xsi:type="boolean">false</item>
                    <item name="theme_advanced_buttons4" xsi:type="boolean">false</item>
                    <item name="theme_advanced_statusbar_location" xsi:type="boolean">false</item>
                </item>
                <item name="files_browser_window_url" xsi:type="boolean">false</item>
                <item name="height" xsi:type="string">250px</item>
                <item name="toggle_button" xsi:type="boolean">true</item>
                <item name="add_variables" xsi:type="boolean">false</item>
                <item name="add_widgets" xsi:type="boolean">false</item>
                <item name="add_images" xsi:type="boolean">false</item>
                <item name="add_directives" xsi:type="boolean">false</item>
            </item>
            <item name="label" xsi:type="string" translate="true">Megamenu Side Block</item>
            <item name="source" xsi:type="string">category</item>
            <item name="template" xsi:type="string">ui/form/field</item>
            <item name="wysiwyg" xsi:type="boolean">true</item>
            <item name="dataScope" xsi:type="string">megamenu_side_block</item>
            <item name="rows" xsi:type="number">8</item>
        </item>
    </argument>
</field>

What I already checked:

  • The attribute is added to the table eav_attribute
  • When I debug into \Magento\Catalog\Controller\Adminhtml\Category\Save::execute() the value is there and after $category->save() I can still get the value with
    $category->getMegamenuSideBlock()
  • I tried to add a value manually but it didn't work either it wasn't loaded in backend
  • When I change dataScope in xml to the attribute_code that is working the other attribute value is loaded just fine

As I said before I have a second attribute that uses the same code as above (just with different attribute_code) and that is working perfectly fine.

I've already checked several posts here but I didn't see any difference to my code or it didn't work.

What could be the reason that it's not saved to the database? Am I missing something in my code?

Update:
As @Alex suggested I tested it within 98-magerun2 dev:console but one is being saved properly into database the other one not.

test with n98-magerun2 dev:console

  • Try n98-magerun2 dev:console to isolate the problem (load category, change, save) – Alex Feb 27 at 14:35
  • @Alex I tried it but it has the same result – Daniel Feb 27 at 14:53
  • I already had a look at this question. My difference in the UpgradeData is just that I leave visible_on_front. sort_order and group as default and set user_defined, wysiwyg_enabled and is_html_allowed_on_front to 1 – Daniel Feb 27 at 15:33
2

The problem was that the attribute was not added to a attribute group.

The following code solved the problem:

$attributeSet = $eavSetup->getDefaultAttributeSetId(Category::ENTITY);
$attributeGroup = $eavSetup->getDefaultAttributeGroupId(Category::ENTITY);
$eavSetup->addAttributeToGroup(
    Category::ENTITY,
    $attributeSet,
    $attributeGroup,
    $eavSetup->getAttributeId(Category::ENTITY, 'megamenu_bottom_block')
)->addAttributeToGroup(
    Category::ENTITY,
    $attributeSet,
    $attributeGroup,
    $eavSetup->getAttributeId(Category::ENTITY, 'megamenu_side_block')
);

The code of both attributes was the same. I have no idea how megamenu_bottom_block was added to a group but by having a look in the database in eav_entity_attribute table I managed to find the only difference between the attributes in the database.

0

Use functions like below

 public function beforeSave($object)
    {
        $attributeName = $this->getAttribute()->getName();
        $value = $object->getData($attributeName);

        if ($imageName = $this->getUploadedImageName($value)) {
            $object->setData($this->additionalData . $attributeName, $value);
            $object->setData($attributeName, $imageName);
        } elseif (is_string($value)) {
            $object->setData($attributeName, null);
        }
        return parent::beforeSave($object);
    }



public function afterSave($object)
    {
        $value = $object->getData($this->additionalData . $this->getAttribute()->getName());

     --Write your code--
        return $this;
    }
  • Do you mean I should write a plugin to save the attribute? But why my other attribute works just fine without a plugin and I would rather fix the actual problem instead of working around it – Daniel Feb 27 at 13:51
  • Interesting idea but very hacky :-/ – Alex Feb 27 at 14:35

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.