1

Anybody please let me know how to define a "toOptionArray()" kind of function in Magento 2 such that any "array" (I have "array" formatted data not object[] format) can be defined and assigned to Admin custom form "select" field.

Like below:

public function getBannerEffects()
        {
                return array(
                array('value' => 'none', 'label'=>__('None')),
                        array('value' => 'blindX', 'label'=>__('Blind X')),
                        array('value' => 'blindY', 'label'=>__('Blind Y')),
                        array('value' => 'blindZ', 'label'=>__('Blind Z')),
                        array('value' => 'cover', 'label'=>__('Cover')),
                        array('value' => 'curtainX', 'label'=>__('Curtain X')),
                        array('value' => 'curtainY', 'label'=>__('Curtain Y')),
                        array('value' => 'fade', 'label'=>__('Fade')),
                        array('value' => 'fadeZoom', 'label'=>__('Fade Zoom')),
                        array('value' => 'growX', 'label'=>__('Grow X')),
                        array('value' => 'growY', 'label'=>__('Grow Y')),
                        array('value' => 'scrollUp', 'label'=>__('Scroll Up')),
                        array('value' => 'scrollDown', 'label'=>__('Scroll Down')),
                        array('value' => 'scrollLeft', 'label'=>__('Scroll Left')),
                        array('value' => 'scrollRight', 'label'=>__('Scroll Right')),
                        array('value' => 'scrollHorz', 'label'=>__('Scroll Horizontal')),
                        array('value' => 'scrollVert', 'label'=>__('Scroll Vertical')),
                        array('value' => 'shuffle', 'label'=>__('Shuffle')),
                        array('value' => 'toss', 'label'=>__('Toss')),
                        array('value' => 'turnUp', 'label'=>__('Turn Up')),
                        array('value' => 'turnDown', 'label'=>__('Turn Down')),
                        array('value' => 'turnLeft', 'label'=>__('Turn Left')),
                        array('value' => 'turnRight', 'label'=>__('Turn Right')),
                        array('value' => 'uncover', 'label'=>__('Uncover')),
                        array('value' => 'wipe', 'label'=>__('Wipe')),
                        array('value' => 'zoom', 'label'=>__('Zoom'))
                );
        }

Can this be declared in Model or Helper ?

How to assign this data to custom module Admin panel form "select" field ?

2 Answers 2

4

You create source model class RedPage implements \Magento\Framework\Option\ArrayInterface

public function toOptionArray() {
        return [
            [
                'value' => 0,
                'label' => __('Page1'),
            ],
            [
                'value' => 1,
                'label' => __('Page2'),
            ],
            [
                'value' => 2,
                'label' => __('Page3'),
            ],
        ];

    }
4
  • @Pratik nope, it's not working. It crashes the page immeidately. Commented Dec 7, 2015 at 13:03
  • Please send me your code and also magento 2 version Commented Dec 7, 2015 at 13:09
  • Magento 2.0.0 stable version, will post code tomorrow for this. Commented Dec 7, 2015 at 13:18
  • It works for me. Thx @Pratik Commented Dec 25, 2015 at 16:31
0

In system.xml file custom select field

<field id="select_field" translate="label" type="select" sortOrder="2" showInDefault="2" showInWebsite="1" showInStore="1">
                    <label>Custom Select Field</label>
                    <source_model>Learning\Custom\Model\Config\Source\CustomeField</source_model>
                </field>

and you should create Model file to load the select field toOptionsArray() values

<?php

namespace Learning\Custom\Model\Config\Source;

class CustomeField implements \Magento\Framework\Option\ArrayInterface
{


    public function toOptionArray()
    {

        $attributesArrays = array();

        $attributesArrays[0] =array(
            'label' => '0',
            'value' => 'One'
        );
        $attributesArrays[1] =array(
            'label' => '1',
            'value' => 'Two'
        );
        $attributesArrays[2] =array(
            'label' => '3',
            'value' => 'Three'
        );
       return $attributesArrays;

    }

}

let me know if it's not working..

Your Answer

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.