Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ?

share|improve this question

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

    }
share|improve this answer
    
@Pratik nope, it's not working. It crashes the page immeidately. – Vicky Dev Dec 7 '15 at 13:03
    
Please send me your code and also magento 2 version – Pratik Dec 7 '15 at 13:09
    
Magento 2.0.0 stable version, will post code tomorrow for this. – Vicky Dev Dec 7 '15 at 13:18
    
It works for me. Thx @Pratik – Arni Dec 25 '15 at 16:31

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.