Elenco a discesa attributo personalizzato categoria Magento2.1


10

I passaggi per riprodurre

1. Lo script del modulo UpgradeData.php contiene:

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

Risultato atteso

  1. Nel modulo categoria dovrebbe apparire il menu a discesa selezionare Roflcopter con blocchi CMS come opzioni

Risultato attuale

  1. Menu a discesa vuoto

Risposte:


14

Aggiungi tag opzioni per la creazione di opzioni selezionate. Nel tuo caso questo dovrebbe essere


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


Forse sai se posso mostrare / nascondere questa scheda e / o i suoi attributi in base ad alcune condizioni, ad esempio la profondità della categoria?
Sergejs Zakatovs,

GRAZIE! Lo stavo cercando da tanto tempo. I documenti non sono così chiari su questo argomento. Come fai a saperlo?
CompactCode

I dati non vengono salvati nel database @Sohel Rana
Chirag Parmar il

2

Ho fatto nel mio caso. Ho opzioni personalizzate ex. L1, L2 e L3. Devo ottenerli su un attributo personalizzato come valori. Quindi sono stato creato un file sorgente nel modulo - vendor \ module \ Model \ Config \ Source \ Options.php

questo file contiene il piccolo codice per creare le opzioni, qui puoi seguire il codice

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

    }

poi dopo nel tuo installdata.php devi chiamarlo come sorgente

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

Quindi aggiungere anche la riga nel file 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>

Salvalo, svuota la cache e controlla.

Spero che ti aiuti.

Per favore, dammi una risposta se ti funziona.


Ho riscontrato questo tipo di errore: Elemento "campo": questo elemento non è previsto. Previsto è uno di (impostazioni, colonna, actionsColumn, selectionsColumn). Linea: 681
Pratik Mehta,

come hai salvato i dati,
Mujahidh,

I dati non vengono salvati nel database @Jdprasad V
Chirag Parmar il

Questo ha funzionato per me, controlla gentilmente di nuovo, se hai apportato modifiche alla pagina dello schema.
Jdprasad V,

1
+1 per questo. Per me funziona. ] manca nell'array. Lo modifico.
Chirag Parmar,
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.