1

enter image description here

How to Store category id and also Store category name in custom table two entity

public function toOptionArray()
    {
        $options = [];
        $categories = $this->collectionFactory->create()->addFieldToSelect('is_active', 1)
            ->addFieldToSelect('is_active')->addFieldToSelect('name')
        ->addFieldToSelect('entity_id')->addAttributeToFilter('level' , 2)->setStoreId(2);

        $options[] = array('value' => '0', 'label' => 'All Category');
        foreach ($categories as $category){
            $options[] = [
                'value' => $category->getEntityId(),
                'label' => $category->getName(),
            ];
        }
        return $options;
    }

Admin Form code

 <field name="category_id" sortOrder="18" formElement="select">
        <settings>
            <validation>
                <rule name="required-entry" xsi:type="boolean">true</rule>
            </validation>
            <dataType>text</dataType>
            <label translate="true">Categry Id</label>
            <dataScope>category_id</dataScope>
        </settings>
        <formElements>
            <select>
                <settings>
                    <options class="Vrinsoft\Appscreens\Model\Allsection\Source\Categories"/>
                </settings>
            </select>
        </formElements>
    </field>
    <field name="category_name" sortOrder="19" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">allsection</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
                <dataType>text</dataType>
                <label translate="true">Category Name</label>
                <dataScope>category_name</dataScope>
            </settings>
        </field>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.