Magento2: Custom category attribute with cms block chooser don't work

Created on 27 May 2016  路  7Comments  路  Source: magento/magento2

Steps to reproduce

  1. Create a new category attribute with source="MagentoCatalogModelCategoryAttributeSourcePage"
    $categorySetup->addAttribute(
    MagentoCatalogModelCategory::ENTITY, 'topmenu_category_banner', [
    'type' => 'int',
    'label' => 'Category Top Menu Banner',
    'input' => 'select',
    'source' => 'MagentoCatalogModelCategoryAttributeSourcePage',
    'required' => false,
    'sort_order' => 50,
    'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
    'group' => 'Display Settings'
    ]
    );
  2. Add a new field in file /vendor/magento/module-catalog/view/adminhtml/ui_component/category_form.xml



    130
    string
    select
    Category Top Menu Banner



    (copied from native landing_page attribute)

Expected result
Attribute rendered in the BO on catalog/category/edit page with the list of options (list of existing cms blocks).

Actual result
The attribute is rendered without any option (list of cms blocks is empty)
http://www.awesomescreenshot.com/0bc5wj7z40

Catalog Format is not valid bug report non-issue

Most helpful comment

@Kustorovsky, i think you need to add options item in the category_form.xml :

<field name="testimony">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Magento\Catalog\Model\Category\Attribute\Source\Page</item>
        <item name="config" xsi:type="array">
            <item name="sortOrder" xsi:type="number">100</item>
            <item name="dataType" xsi:type="string">string</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="label" xsi:type="string" translate="true">Testimony</item>
        </item>
    </argument>
</field>

(It works for me, found at : https://magento.stackexchange.com/questions/123785/magento2-1-category-custom-attribute-dropdown/123858#123858)

All 7 comments

Hi,
Is there any update about this issue?
Thanks

It seems like to custom attributes are not being added to the meta object on:

/* app/code/Magento/Catalog/Model/Category/DataProvider.php:163 */
public function prepareMeta($meta)
    {
        $meta = array_replace_recursive($meta, $this->prepareFieldsMeta(
            $this->getFieldsMap(),
            $this->getAttributesMeta($this->eavConfig->getEntityType('catalog_category'))
        ));

        return $meta;
    }

The $meta object at the end of the function contains ONLY the hard-coded array located at:

/* app/code/Magento/Catalog/Model/Category/DataProvider.php:402 */
 /**
     * @return array
     */
    protected function getFieldsMap()
    {
        return [
            'general' =>
                [
                    'parent',
                    'path',
                    'is_active',
                    'include_in_menu',
                    'name',
                ],
            'content' =>

I have extended DataProvider and it work.

`class DataProvider extends MagentoCatalogModelCategoryDataProvider
{

protected function getFieldsMap()
{
    return [
        'general' =>
            [
                'parent',
                'path',
                'is_active',
                'include_in_menu',
                'name',
            ],
        'content' =>
            [
                'image',
                'description',
                'landing_page',
            ],
        'megamenu' =>
            [
                'typomenu_is_vertical',
                'typomenu_cat_target',`

But if two extensions extend DataProvider, Are there conflict?

Having the same issue with a new attribute with a custom source model.
UI compoment options for custom attribute is empty because the DataProvider strips the data of the new attribute. Should the getFieldsMap method be public so that the fields can be modified via a Plugin or should the DataProvider take custom attributes into account (from form xml maybe) when retrieving the fields map.

@will-b

I had a similar issue trying to add an image upload field to my admin category

What seem to work is creating a Plugin for afterGetData

Example:

etc/di.xml

<type name="Magento\Catalog\Model\Category\DataProvider">
        <plugin name="MagePalCategoryMenuImageDataProvider" type="MagePal\CategoryMenuImage\Plugin\Catalog\Model\Category\DataProvider" />
    </type>

MagePalCategoryMenuImagePluginCatalogModelCategoryDataProvider.php

......
    public function afterGetData(\Magento\Catalog\Model\Category\DataProvider $subject, array $data)
    {

        foreach ($data as &$categoryData) {
            if(array_key_exists('menu_image', $categoryData) && !is_array($categoryData['menu_image']) && !empty($categoryData['menu_image'])){
                $fileName = $categoryData['menu_image'];
                unset($categoryData['menu_image']);
                $categoryData['menu_image'][0]['name'] = $fileName;
                $categoryData['menu_image'][0]['url'] = $this->getImageUrl($fileName);
            }
        }

        return $data;
    }
...

All you need to do is to figure out is how to format the data that you need

@Kustorovsky, i think you need to add options item in the category_form.xml :

<field name="testimony">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Magento\Catalog\Model\Category\Attribute\Source\Page</item>
        <item name="config" xsi:type="array">
            <item name="sortOrder" xsi:type="number">100</item>
            <item name="dataType" xsi:type="string">string</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="label" xsi:type="string" translate="true">Testimony</item>
        </item>
    </argument>
</field>

(It works for me, found at : https://magento.stackexchange.com/questions/123785/magento2-1-category-custom-attribute-dropdown/123858#123858)

Hi @Kustorovsky
This is not a bug.
You should specify <item name="options" for your field in category_form.xml

Was this page helpful?
0 / 5 - 0 ratings