希望你有时间帮助我。 在任何情况下,我的Magento 2数组映射都不会将selected =“selected”写入我的字段。为此疯狂。 数据将被保存并正确获取。
非常感谢你的帮助。
版本:Magento 2.2.2
/app/code/Sumedia/Switcher/Block/Adminhtml/System/Config/Form/Field/Group.php
class SubjectCreate(CreateView):
form_class = NewSubjectForm
template_name = 'create.html'
success_url = reverse_lazy('subapp:index')
/app/code/Sumedia/Switcher/Block/Adminhtml/System/Config/Form/Field/Groupmap.php
<?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->storeManager = $storeManager;
}
/**
* @return string
*/
public function _toHtml(){
if(!$this->getOptions()){
$groups = $this->storeManager->getGroups();
foreach($groups AS $row){
$this->addOption($row->getGroupId(),$row->getName());
}
}
return parent::_toHtml();
}
/**
* @param string $value
* @return $this
*/
public function setInputName($value){
return $this->setName($value);
}
}
/app/code/Sumedia/Switcher/Model/Adminhtml/System/Config/Groupmap.php
<?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->renderer){
$this->renderer = $this->getLayout()->createBlock(
'Sumedia\Switcher\Block\Adminhtml\System\Config\Form\Field\Group',
'',['data' => ['is_renderer_to_js_template' => true]]);
}
return $this->renderer;
}
protected function _prepareToRender()
{
$renderer = $this->getRenderer();
$this->addColumn('store',[
'label' => __('Store'),
'renderer'=>$renderer
]);
$this->addColumn('name',[
'label' => __('Name')
]);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');
parent::_prepareToRender();
}
protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
{
$store = $row->getStore();
$options = array();
if($store){
$options['option_'.$this->getRenderer()->calcOptionHash($store)] = 'selected="selected"';
}
$row->setData('option_extra_attrs',$options);
}
public function renderCellTemplate($columnName)
{
if($columnName == 'store'){
$this->_columns[$columnName]['class'] = 'input-text required-entry validate-number';
$this->_columns[$columnName]['style'] = 'width:50px';
}
return parent::renderCellTemplate($columnName);
}
}
/app/code/Sumedia/Switcher/etc/adminhtml/system.xml
<?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->getValue();
if(is_array($value)) {
foreach($value AS $_data){
if(!isset($_data['store']) || !isset($_data['name'])){
continue;
}
$id = uniqid();
$data[$id] = array('store' => $_data['store'],'name' => $_data['name']);
}
}
$this->setValue(serialize($data));
return $this;
}
public function afterLoad(){
$value = @unserialize($this->getValue());
if (is_array($value)) {
$data = array();
foreach($value AS $id => $_data){
$data[$id] = array('store' => $_data['store'], 'name' => $_data['name']);
}
$this->setValue($data);
}
return $this;
}
}
答案 0 :(得分:0)
我看到您已经用数据重新加载了所有需要的方法,但是在 Sumedia \ Switcher \ Block \ Adminhtml \ System \ Config \ Form \中的 getRenderer 方法中存在问题在Field \ Groupmap 类中,尝试将'is_renderer_to_js_template'更改为'is_renderer_to_js_template'
几天前,在开发自定义模块期间遇到了相同的问题,并且仅在查看了magento的核心代码(目录库存模块的代码)=)后才找到解决方案