Lista niestandardowych atrybutów Magento2.1


10

kroki ku reprodukcji

1. Skrypt modułu UpgradeData.php zawiera:

$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>

Spodziewany wynik

  1. W formularzu kategorii powinien pojawić się menu rozwijane wybierz Roflcopter z blokami CMS jako opcje

Aktualny rezultat

  1. Puste menu rozwijane

Odpowiedzi:


14

Dodaj tag opcji do tworzenia wybranych opcji. W twoim przypadku tak powinno być


<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>


Może wiesz, czy mogę pokazać / ukryć tę kartę i / lub jej atrybuty na podstawie niektórych warunków, na przykład głębokości kategorii?
Sergejs Zakatovs

DZIĘKI! Tak długo tego szukałem. Dokumenty są tak niejasne w tym temacie. Skąd ty to wiesz?
CompactCode

Dane nie są zapisywane w bazie danych @ Sohel Rana
Chirag Parmar

2

Zrobiłem w moim przypadku. Mam niestandardowe opcje np. L1, L2 i L3. Muszę uzyskać je jako niestandardowy atrybut jako wartości. Tak więc zostałem utworzony plik źródłowy w module - vendor \ module \ Model \ Config \ Source \ Options.php

ten plik zawiera mały kod, aby utworzyć opcje. Tutaj możesz śledzić kod

 <?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'),
                ];
        }

    }

potem w installdata.php musisz wywołać to jako źródło

$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,
             ]
        );

Następnie dodaj również linię do pliku 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>

Zapisz, opróżnij pamięć podręczną i sprawdź.

Mam nadzieję, że ci to pomoże.

Proszę dać mi odpowiedź, jeśli to ci odpowiada.


Mam tego rodzaju błąd: Element „field”: Tego elementu nie należy się spodziewać. Oczekiwany jest jeden z (ustawienia, kolumna, akcjeKolumna, selekcjeKolumna). Linia: 681
Pratik Mehta


Dane nie są zapisywane w bazie danych @Jdprasad V
Chirag Parmar

To działa dla mnie, proszę ponownie sprawdzić, czy zrobiłeś jakieś zmiany na stronie schematu.
Jdprasad V

1
+1 za to. Mi to pasuje. ] brakuje w tablicy. Ja to edytuję.
Chirag Parmar
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.