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

}

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

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

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.