title: Magento 2 / Custom Config Field Array Mapping
body: <p>hope your will have the time to help me.
  On any circumstances my Magento 2 Array Mapping will not write selected="selected" to my field. Getting crazy about this.
  The Data will be saved and fetched correct.</p>
  
  <p>Big thanks to your help.</p>
  
  <p>Version: Magento 2.2.2</p>
  
  <p>/app/code/Sumedia/Switcher/Block/Adminhtml/System/Config/Form/Field/Group.php</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace Sumedia\Switcher\Block\Adminhtml\System\Config\Form\Field;
  
  class Group extends \Magento\Framework\View\Element\Html\Select {
  
      /**
       * @var \Magento\Store\Model\StoreManager
       */
      protected $storeManager;
  
      /**
       * Store constructor.
       * @param \Magento\Framework\View\Element\Context $context
       * @param \Magento\Store\Model\StoreManager $storeManager
       * @param array $data
       */
      public function __construct(
          \Magento\Framework\View\Element\Context $context,
          \Magento\Store\Model\StoreManager $storeManager,
          array $data = []
      ){
          parent::__construct($context, $data);
          $this-&gt;storeManager = $storeManager;
      }
  
      /**
       * @return string
       */
      public function _toHtml(){
          if(!$this-&gt;getOptions()){
              $groups = $this-&gt;storeManager-&gt;getGroups();
              foreach($groups AS $row){
                  $this-&gt;addOption($row-&gt;getGroupId(),$row-&gt;getName());
              }
          }
          return parent::_toHtml();
      }
  
      /**
       * @param string $value
       * @return $this
       */
      public function setInputName($value){
          return $this-&gt;setName($value);
      }
  
  }
  </code></pre>
  
  <p>/app/code/Sumedia/Switcher/Block/Adminhtml/System/Config/Form/Field/Groupmap.php</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace Sumedia\Switcher\Block\Adminhtml\System\Config\Form\Field;
  
  use Magento\Backend\Block\Template\Context;
  use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
  
  class Groupmap extends AbstractFieldArray
  {
      /**
       * @var \Sumedia\Switcher\Block\Adminhtml\System\Config\Form\Field\Group
       */
      protected $renderer;
  
      public function __construct(
          Context $context,
          array $data = [])
      {
          parent::__construct($context, $data);
      }
  
      public function getRenderer()
      {
          if(!$this-&gt;renderer){
              $this-&gt;renderer = $this-&gt;getLayout()-&gt;createBlock(
                  'Sumedia\Switcher\Block\Adminhtml\System\Config\Form\Field\Group',
                  '',['data' =&gt; ['is_renderer_to_js_template' =&gt; true]]);
          }
          return $this-&gt;renderer;
      }
  
      protected function _prepareToRender()
      {
          $renderer = $this-&gt;getRenderer();
          $this-&gt;addColumn('store',[
              'label' =&gt; __('Store'),
              'renderer'=&gt;$renderer
          ]);
          $this-&gt;addColumn('name',[
              'label' =&gt; __('Name')
          ]);
          $this-&gt;_addAfter = false;
          $this-&gt;_addButtonLabel = __('Add');
          parent::_prepareToRender();
      }
  
      protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
      {
          $store = $row-&gt;getStore();
          $options = array();
          if($store){
              $options['option_'.$this-&gt;getRenderer()-&gt;calcOptionHash($store)] = 'selected="selected"';
          }
          $row-&gt;setData('option_extra_attrs',$options);
      }
  
      public function renderCellTemplate($columnName)
      {
          if($columnName == 'store'){
              $this-&gt;_columns[$columnName]['class'] = 'input-text required-entry validate-number';
              $this-&gt;_columns[$columnName]['style'] = 'width:50px';
          }
          return parent::renderCellTemplate($columnName);
      }
  
  }
  </code></pre>
  
  <p>/app/code/Sumedia/Switcher/Model/Adminhtml/System/Config/Groupmap.php</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace Sumedia\Switcher\Model\Adminhtml\System\Config;
  
  use Magento\Framework\App\Config\Value;
  
  class Groupmap extends Value {
  
      public function beforeSave(){
          $data = array();
          $value = $this-&gt;getValue();
          if(is_array($value)) {
              foreach($value AS $_data){
                  if(!isset($_data['store']) || !isset($_data['name'])){
                      continue;
                  }
                  $id = uniqid();
                  $data[$id] = array('store' =&gt; $_data['store'],'name' =&gt; $_data['name']);
              }
          }
          $this-&gt;setValue(serialize($data));
          return $this;
      }
  
      public function afterLoad(){
          $value = @unserialize($this-&gt;getValue());
          if (is_array($value)) {
              $data = array();
              foreach($value AS $id =&gt; $_data){
                  $data[$id] = array('store' =&gt; $_data['store'], 'name' =&gt; $_data['name']);
              }
              $this-&gt;setValue($data);
          }
          return $this;
      }
  
  }
  </code></pre>
  
  <p>/app/code/Sumedia/Switcher/etc/adminhtml/system.xml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;    
  &lt;config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"&gt;
      &lt;system&gt;
          &lt;tab id="sumedia" translate="label" sortOrder="100"&gt;
              &lt;label&gt;Sumedia&lt;/label&gt;
          &lt;/tab&gt;
          &lt;section id="sumedia_switcher" translate="label" sortOrder="100" showInDefault="1" showInStore="1" showInWebsite="1"&gt;
              &lt;class&gt;seperator-top&lt;/class&gt;
              &lt;label&gt;Switcher&lt;/label&gt;
              &lt;tab&gt;sumedia&lt;/tab&gt;
              &lt;resource&gt;Sumedia_Switcher::config&lt;/resource&gt;
              &lt;group id="general" translate="label" type="text" sortOrder="10" showInWebsite="1" showInStore="1" showInDefault="1"&gt;
                  &lt;label&gt;General&lt;/label&gt;
                  &lt;field id="enabled" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"&gt;
                      &lt;label&gt;Enabled&lt;/label&gt;
                      &lt;source_model&gt;Magento\Config\Model\Config\Source\Yesno&lt;/source_model&gt;
                  &lt;/field&gt;
              &lt;/group&gt;
              &lt;group id="store_switch" translate="label" type="text" sortOrder="10" showInWebsite="1" showInStore="1" showInDefault="1"&gt;
                  &lt;label&gt;Store Switch&lt;/label&gt;
                  &lt;field id="enabled" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"&gt;
                      &lt;label&gt;Enabled&lt;/label&gt;
                      &lt;source_model&gt;Magento\Config\Model\Config\Source\Yesno&lt;/source_model&gt;
  
                  &lt;/field&gt;
                  &lt;field id="use_mapping" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"&gt;
                      &lt;label&gt;Use Mapping&lt;/label&gt;
                      &lt;source_model&gt;Magento\Config\Model\Config\Source\Yesno&lt;/source_model&gt;
                  &lt;/field&gt;
                  &lt;field id="mapping" translate="label comment tooltip" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"&gt;
                      &lt;label&gt;Mapping&lt;/label&gt;
                      &lt;frontend_model&gt;Sumedia\Switcher\Block\Adminhtml\System\Config\Form\Field\Groupmap&lt;/frontend_model&gt;
                      &lt;backend_model&gt;Sumedia\Switcher\Model\Adminhtml\System\Config\Groupmap&lt;/backend_model&gt;                        
                  &lt;/field&gt;
              &lt;/group&gt;                
          &lt;/section&gt;
      &lt;/system&gt;
  &lt;/config&gt;
  </code></pre>