0

I'm updating a plugin and I need to build a select.

I'm new in Magento2, and I found this idea: Magento 2: How add dropdown in admin form?

I'm building something similar but I'm not able to display it in Admin Panel.

here the function:

<?php
namespace Fattura24\AppFatturazione\Block\System\Config;

class SelectFattureCreaFattura extends 
\Magento\Config\Block\System\Config\Form\Field implements 
\Magento\Framework\Option\ArrayInterface
{
public function toOptionArray()
{
$result = [];

foreach (self::getOptionArray() as $index => $value) {
    $result[] = ['value' => $index, 'label' => $value];
}
return $result;
}

/**
* Retrieve option array
*
* @return string[]
*/
public static function getOptionArray()
{
return [
    [1 => __('Disabilitata')],
    [2 => __('Fattura NON Elettronica')],
    [3 => __('Fattura Elettronica')]
];

}

/**
* Retrieve option array with empty value
*
* @return string[]
*/
public function getAllOptions()
{
$result = [];

foreach (self::getOptionArray() as $index => $value) {
    $result[] = ['value' => $index, 'label' => $value];
}

 return $result;
}

/**
* Retrieve option text by option value
*
* @param string $optionId
* @return string
*/
public function getOptionText($optionId)
{
$options = self::getOptionArray();

return isset($options[$optionId]) ? $options[$optionId] : null;
}
}

I know I need a template but I don't know where to place it exactly and how to build it. Thanks to all David

  • "Something goes wrong" we can't work with this. give us more information. – Philipp Sander Feb 7 at 12:22
  • I found this way: magento.stackexchange.com/questions/158262/… Actually I am still not able to display my dropdown in admin menu. I think it should be enough to put the model in the correct path, but I don't know where exactly. – Davide Iandoli Feb 8 at 8:47
  • Actually I'm not able to display the options in admin menu, and I think this is due to my template file and its path. – Davide Iandoli Feb 18 at 15:59
  • @Davide Iandoli Can you specify magento version? – Kirill Korushkin Feb 28 at 15:34
0

I solved it creating CreaFattura.php file like this:

<?php

class Fattura24_AppFatturazione_Model_System_Config_Source_CreaFattura
{
    public function toOptionArray()
    {
        $option = array(
            array('value' => 'd', 'label' => 'Disabilitata'),
            array('value' => 'f', 'label' => 'Fattura NON Elettronica'),
            array('value' => 'fe', 'label' => 'Fattura Elettronica'),
        );

        return $option;
    }
}

then to display my options I added this snippet in system.xml

<crea_fattura translate="label comment">
                            <label>Crea fattura</label>
                            <frontend_type>select</frontend_type>
                            <source_model>appfatturazione/system_config_source_CreaFattura</source_model>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
</crea_fattura>

in source_model there is the path in which I put class CreaFattura

Your Answer

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

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