5

Since Magento 2.1 you have to define a UI component for custom category attributes (See Magento 2.1: how to create category attrbute programmatically?)

I have one multiselect attribute with a custom source model and cannot get it to work. I took available_sort_by as an example and managed to show the attribute with a multiselect input but it has no options, i.e. it is not using my source model.

This is the relevant part of category_form.xml:

 <field name="solr_remove_filters">
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="sortOrder" xsi:type="number">20</item>
 <item name="additionalClasses" xsi:type="string">admin__field-default</item>
 <item name="formElement" xsi:type="string">multiselect</item>
 <item name="source" xsi:type="string">category</item>
 <item name="label" xsi:type="string" translate="true">Remove Filters</item>
 <item name="tooltip" xsi:type="array">
 <item name="link" xsi:type="string">#</item>
 <item name="description" xsi:type="string" translate="true">Hold the CTRL key to select multiple filters</item>
 </item>
 <item name="scopeLabel" xsi:type="string">[STORE VIEW]</item>
 </item>
 </argument>
 </field>

The attribute solr_remove_filters has been added with the following install script:

 $eavSetup->addAttribute(
 Category::ENTITY,
 'solr_remove_filters',
 [
 'type' => 'text',
 'input' => 'multiselect',
 'source' => FilterableProductAttributeSource::class,
 'backend' => FilterableProductAttributeBackend::class,
 'label' => 'Remove Filters',
 'note' => 'Hold the CTRL key to select multiple filters',
 'required' => 0,
 'user_defined' => 0,
 'group' => 'Solr',
 'global' => 0,
 'visible' => 1,
 ]
 );

I have set a debug breakpoint in FilterableProductAttributeSource::getAllOptions() and see that it is executed and returns an array in the form

[
 ['value' => 'value1', 'label' => 'label1'],
 ['value' => 'value2', 'label' => 'label2'],
 ['value' => 'value3', 'label' => 'label3'],
]

which confirms that the source model is instantiated and works.

What do I need to do to populate the UI component with these values?

asked Aug 4, 2016 at 13:13

2 Answers 2

5

Found it, thanks to GitHub issue #5438. The source model has to be specified in category_form.xml as well:

 <field name="solr_remove_filters">
 <argument name="data" xsi:type="array">
 <item name="options" xsi:type="object">IntegerNet\Solr\Model\Entity\Attribute\Source\FilterableProductAttribute</item>
 <item name="config" xsi:type="array">
 ...

Just don't ask me why it works without that for available_sort_by and why all this redundancy is necessary...

answered Aug 4, 2016 at 13:42
1
0

You need to consider source model to display options because you have custom source model if you wants to you can use array backend for source model t display options in admin for category multi select attribute. Your code works because you are telling ui_component that you are going to use following options for you multi select attribute.

answered Jun 15, 2017 at 10:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.