2

I have a new Magento 2 install with an Admin form. I have a multi select that works fine but unable to have old values show up as selected after save enter image description here

Here is my Class and how I am loading that form element

namespace RussellAlbin\Blog\Block\Adminhtml\Post\Edit;

/**
 * Adminhtml blog post edit form
 */
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{

    /**
     * @var \RussellAlbin\Blog\Model\Category\Source\ListCategories
     */
    protected $_categories;

    /**
     * @var \RussellAlbin\Blog\Model\Postcategory
     */
    protected $_postcategory;

    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;


    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Store\Model\System\Store $systemStore
     * @param \RussellAlbin\Blog\Model\Category\Source\ListCategories $categories
     * @param \RussellAlbin\Blog\Model\Postcategory $postcategory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Store\Model\System\Store $systemStore,
        \RussellAlbin\Blog\Model\Category\Source\ListCategories $categories,
        \RussellAlbin\Blog\Model\Postcategory $postcategory,
        array $data = []
    ) {
        $this->_categories = $categories;
        $this->_systemStore = $systemStore;
        $this->_postcategory = $postcategory;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Init form
     *
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('blog_post_form');
        $this->setTitle(__('Blog Post Information'));
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {
        /** @var \RussellAlbin\Blog\Model\Post $model */
        $model = $this->_coreRegistry->registry('blog_post');

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create(
            ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
        );

        $form->setHtmlIdPrefix('post_');

        $fieldset = $form->addFieldset(
            'base_fieldset',
            ['legend' => __('General Information'), 'class' => 'fieldset-wide']
        );

        if ($model->getPostId()) {
            $fieldset->addField('post_id', 'hidden', ['name' => 'post_id']);
        }

        // Gather our existing categories
        $categories = $this->_getExistingCategories( $model );
        // Get all the categories that in the database
        $optionArray = $this->_categories->toOptionArray();

        $field = $fieldset->addField(
            'blog_categories',
            'multiselect',
            [
                'label' => __('Categories'),
                'required' => true,
                'name' => 'blog_categories',
                'values' => $optionArray,
                'value' => $categories
            ]
        );


        $form->setValues($model->getData());
        $form->setUseContainer(true);
        $this->setForm($form);

        return parent::_prepareForm();
    }

    /**
     * @param $model
     * @return array
     */
    private function _getExistingCategories( $model )
    {
        // Get our collection
        $existingCategories = $this->_postcategory->getCollection()
            ->addFieldToSelect('category_id')
            ->addFieldToFilter('post_id', $model->getId());

        // Setup our placeholder for the array of categories needed to set back on the value of the multiselect
        $itemList = array();
        foreach($existingCategories as $_item)
        {
            $itemList[] = $_item['category_id'];
        }
        return $itemList;
    }
}
  • _getExistingCategories return correct array ? – Maciej Dec 19 '15 at 17:38
  • Yes $categories = array(0="2") for example and that is the category ID The values of the select are also the category ID. I assumed that is how it would know to select that option. – RussellAlbin Dec 19 '15 at 17:44
1

I found the answer. I did not have the values set on the model.

// This is what shows it as selected on reload
    $model->setData('blog_categories', $categories);

    $fieldset->addField(
        'blog_categories',
        'multiselect',
        [
                'name' => 'blog_categories[]',
                'label' => __('Categories'),
                'title' => __('Categories'),
                'required' => true,
                'values' => $optionArray,
                'disabled' => false

        ]
    );

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.