Search code examples
multi-selectadminhtmlmagento2

Magento 2 adminhtml multiselect and showing selected options after save


How do I get the Magento2 Adminhtml Form with a Multiselect to select the saved options?

enter image description here

Here is my Adminhtml Form Class for reference.

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
        $currentCategories = $this->_getExistingCategories( $model );

        // Get all the categories that in the database
        $allCategories = $this->_categories->toOptionArray();

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


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

I looked back at some work I have done in Magento 1.x and I the only way I got this to work before was using javascript and the setAfterElementHtml to set the options that matched. I remember hating this method of getting it accomplished because it seemed like a work around.


Solution

  • I found the issue. I did not have the values saved 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
    
            ]
        );