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

有帮助吗?

解决方案

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.

其他提示

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;
    }
许可以下: CC-BY-SA归因
scroll top