1
<?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;
}

}

asked Sep 1, 2021 at 14:15
1
  • 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;' Commented Sep 1, 2021 at 14:47

2 Answers 2

1

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()
 ]
 );
answered Sep 1, 2021 at 14:57
7
  • 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 114 Commented Sep 1, 2021 at 14:59
  • i've edited my answer check what implements the class now Commented Sep 1, 2021 at 15:04
  • I am still getting the same error Array to string conversion Commented 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(), Commented Sep 1, 2021 at 15:08
  • added to the question now Commented Sep 1, 2021 at 15:13
1

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

answered Sep 1, 2021 at 16:25
1
  • it wont save because if that's a custom form you'll have to manually set it in the save action controller Commented Sep 3, 2021 at 16:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.