<?php
namespace [Vendor\Module]\Model\System\Config;
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;
use Magento\Framework\Data\OptionSourceInterface;
class CmsBlocks implements OptionSourceInterface
{
/**
* @var array
*/
private $options;
/**
* @var \Magento\Cms\Model\ResourceModel\Block\CollectionFactory
*/
private $collectionFactory;
/**
* @param \Magento\Cms\Model\ResourceModel\Block\CollectionFactory $collectionFactory
*/
public function __construct(
CollectionFactory $collectionFactory
) {
$this->collectionFactory = $collectionFactory;
}
/**
* {@inheritdoc}
*/
public function toOptionArray()
{
if (!$this->options) {
$this->options = $this->collectionFactory->create()->toOptionIdArray();
}
return $this->options;
}
}
it complains Array to string conversion Can anyone guide why i am getting this
I am trying to call this in admin grid
$fieldset->addField(
'cms_block',
'select',
[
'name' => 'cms_block',
'label' => __('CMS Block'),
'title' => __('CMS Block'),
'options' => $this->_cmsBlock->toOptionArray(),
]
);
Admin grid
<?php
namespace [Vendor\Module]\Model\Block\Adminhtml\TestModule\Edit\Tab;
use Magento\Backend\Block\Widget\Form\Generic; use Magento\Backend\Block\Widget\Tab\TabInterface; use Magento\Backend\Block\Template\Context; use Magento\Framework\Registry; use Magento\Framework\Data\FormFactory; use [Vendor\Module]\Model\Model\System\Config\CmsBlocks;
class Adverts extends Generic implements TabInterface {
/**
* @var \[Vendor\Module]\Model\\Model\System\Config\Store
*/
protected $_cmsBlock;
public function __construct(
Context $context,
Registry $registry,
FormFactory $formFactory,
CmsBlocks $cmsBlock,
array $data = []
) {
$this->_cmsBlock = $cmsBlock;
parent::__construct($context, $registry, $formFactory, $data);
}
/**
* Prepare form fields
*
* @return \Magento\Backend\Block\Widget\Form
*/
protected function _prepareForm()
{
/** @var $model \Magetop\Helloworld\Model\PostsFactory */
$model = $this->_coreRegistry->registry('testmodule_testmodule');
/** @var \Magento\Framework\Data\Form $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('post_');
$form->setFieldNameSuffix('testmodule');
// new filed
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Adverts')]
);
if ($model->getId()) {
$fieldset->addField(
'entity_id',
'hidden',
['name' => 'id']
);
}
$fieldset->addField(
'cms_block',
'select',
[
'name' => 'cms_block',
'label' => __('CMS Block'),
'title' => __('CMS Block'),
'options' => $this->_cmsBlock->toOptionArray(),
]
);
$data = $model->getData();
$form->setValues($data);
$this->setForm($form);
return parent::_prepareForm();
}
/**
* Prepare label for tab
*
* @return string
*/
public function getTabLabel()
{
return __('Posts Info');
}
/**
* Prepare title for tab
*
* @return string
*/
public function getTabTitle()
{
return __('Posts Info');
}
/**
* {@inheritdoc}
*/
public function canShowTab()
{
return true;
}
/**
* {@inheritdoc}
*/
public function isHidden()
{
return false;
}
}
-
I have tried this way but not returning expected values '$data = $this->collectionFactory->create(); // $options = []; $options = $data->toOptionArray(); foreach ($options as $block) { $options = ['label' => $block['label'],'value' => $block['value']]; } return $options;'Tj1997– Tj19972021年09月01日 14:47:32 +00:00Commented Sep 1, 2021 at 14:47
2 Answers 2
Try this:
namespace [Vendor\Module]\Model\System\Config;
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;
use Magento\Framework\Option\ArrayInterface;
class CmsBlocks implements ArrayInterface {
/**
* @var array
*/
private $options;
/**
* @var \Magento\Cms\Model\ResourceModel\Block\CollectionFactory
*/
private $collectionFactory;
/**
* @param \Magento\Cms\Model\ResourceModel\Block\CollectionFactory $collectionFactory
*/
public function __construct(
CollectionFactory $collectionFactory
) {
$this->collectionFactory = $collectionFactory;
}
public function toOptionArray(){
$options = array();
$collection = $this->collectionFactory->create();
foreach($collection as $block){
$options[] = [
'value' => $block->getData('identifier'),
'label' => $block->getData('title')
];
}
return $options;
}
}
And try change the addField like this:
$fieldset->addField(
'cms_block',
'select',
[
'name' => 'cms_block',
'label' => __('CMS Block'),
'title' => __('CMS Block'),
'options' => $this->_cmsBlock->toOptionArray()
]
);
-
I did try this but i was getting following error Notice: Array to string conversion in vendor/magento/framework/Data/Form/Element/Select.php on line 114Tj1997– Tj19972021年09月01日 14:59:33 +00:00Commented Sep 1, 2021 at 14:59
-
i've edited my answer check what implements the class nowAlan Zavagli– Alan Zavagli2021年09月01日 15:04:16 +00:00Commented Sep 1, 2021 at 15:04
-
I am still getting the same error Array to string conversionTj1997– Tj19972021年09月01日 15:05:14 +00:00Commented Sep 1, 2021 at 15:05
-
Seems like the issue is not here, can you show me the declaration inside the admin grid of the class? (Use, construct...) and try to remove the last comma: $this->_cmsBlock->toOptionArray(),Alan Zavagli– Alan Zavagli2021年09月01日 15:08:36 +00:00Commented Sep 1, 2021 at 15:08
-
added to the question nowTj1997– Tj19972021年09月01日 15:13:10 +00:00Commented Sep 1, 2021 at 15:13
I think i have found the answer but not sure why this worked.
public function toOptionArray(){
$collection = $this->collectionFactory->create();
$options = array();
foreach ($collection as $key => $value) {
$options[$key] = $value['title'];
}
return $options;
But now the value I selected won't save
-
it wont save because if that's a custom form you'll have to manually set it in the save action controllerAlan Zavagli– Alan Zavagli2021年09月03日 16:13:35 +00:00Commented Sep 3, 2021 at 16:13