Magento2.1 카테고리 커스텀 속성 드롭 다운


10

재현 단계

1. Module UpgradeData.php 스크립트는 다음을 포함합니다 :

$categorySetup->addAttribute(Category::ENTITY, 'roflcopter', [
                    'type' => 'int',
                    'label' => 'CMS Block',
                    'input' => 'select',
                    'source' => 'Magento\Catalog\Model\Category\Attribute\Source\Page',
                    'required' => false,
                    'sort_order' => 20,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'group' => 'Display Settings',
            ]);

2. view / adminhtml / ui_component / 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="Navigation">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Navigation</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">100</item>
            </item>
        </argument>
        <field name="roflcopter">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">60</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">Roflcopter</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

예상 결과

  1. 범주 형태로 드롭 다운이 나타납니다. 옵션으로 CMS 블록이있는 Roflcopter를 선택하십시오.

실제 결과

  1. 빈 드롭 다운

답변:


14

선택 옵션을 작성하기위한 옵션 태그를 추가하십시오. 귀하의 경우 이것은


<field name="roflcopter">
    <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">70</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">Roflcopter</item>
        </item>
    </argument>
</field>


어쩌면 카테고리 깊이와 같은 일부 조건에 따라이 탭 및 / 또는 해당 속성을 표시하거나 숨길 수 있는지 알고 있습니까?
Sergejs Zakatovs

감사! 나는 이것을 오랫동안 찾고 있었다. 이 주제에 대한 문서는 명확하지 않습니다. 이것을 어떻게 알 수 있습니까?
CompactCode

데이터는 데이터베이스 @Sohel라나에 저장되지 않습니다
치라 Parmar을

2

나는 내 경우에 해냈다. 예. 맞춤 옵션이 있습니다. L1, L2 및 L3. 사용자 정의 속성에서 값으로 가져와야합니다. 그래서 나는 모듈-vendor \ module \ Model \ Config \ Source \ Options.php에 소스 파일을 만들었습니다.

이 파일에는 옵션을 만드는 작은 코드가 들어 있습니다. 여기에서 코드를 따를 수 있습니다

 <?php
    /**
     * Copyright © 2013-2017 Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    namespace Vendor\module\Model\Config\Source;
    /**
     * Catalog category landing page attribute source
     *
     * @author      Magento Core Team <core@magentocommerce.com>
     */
    class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
    {
        /**
         * {@inheritdoc}
         * @codeCoverageIgnore
         */
        public function getAllOptions()
        {
            if (!$this->_options) {
                $this->_options = [
                    ['value' => 'l1', 'label' => __('L1')],
                    ['value' => 'l2', 'label' => __('L2')],
                    ['value' => 'l3', 'label' => __('L3')],
                ];
            }
            return $this->_options;
        }
          /**
         * Get options in "key-value" format
         *
         * @return array
         */
        public function toArray()
        {
            return [
                'l1' => __('L1'),
                'l2' => __('L2'),
                'L3' => __('L3'),
                ];
        }

    }

그런 다음 installdata.php에서 소스로 호출해야합니다.

$eavSetup->addAttribute(
            Category::ENTITY,
            'category_level_rendering',
            [
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'Category Level rendering',
                'input' => 'select',
                'required' => false,
                'sort_order' => 100,
                'source' => '',
                'visible'  => true,
                'source' => 'vendor\module\Model\Config\Source\Options',
                'default'  => '0',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group' => 'General Information',
                'used_in_product_listing' => true,
             ]
        );

그런 다음 XML 파일에 줄을 추가하십시오.

<field name="category_level_rendering">
                <argument name="data" xsi:type="array">
/*Here is the code added to get the options on dropdown*/
<item name="options" xsi:type="object">Vendor\module\Model\Config\Source\Options</item>
                    <item name="config" xsi:type="array">
                        <item name="sortOrder" xsi:type="number">10</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">Category Level Rendering</item>
                    </item>
                </argument>
            </field>

저장하고 캐시를 플러시하고 확인하십시오.

잘하면 그것은 당신에게 도움이됩니다.

그것이 당신에게 효과가 있다면 답장을 보내 주시기 바랍니다.


이런 종류의 오류가 발생했습니다 : 요소 '필드':이 요소는 예상되지 않았습니다. (settings, column, actionsColumn, selectionsColumn) 중 하나입니다. 줄 : 681
Pratik Mehta

데이터를 어떻게 저장 했습니까?
Mujahidh

데이터는 데이터베이스 @Jdprasad V에 저장되지 않습니다
Chirag Parmar

이것은 스키마 페이지에서 변경 사항이 있으면 친절하게 다시 확인하십시오.
Jdprasad V

1
이것을 위해 +1. 그것은 나를 위해 작동합니다. ] 배열에 없습니다. 편집했습니다.
Chirag Parmar
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.