Magento 2 adminhtml 多选并在保存后显示所选选项

Magento 2 adminhtml multiselect and showing selected options after save

我如何获得带有多个 select 到 select 已保存选项的 Magento2 Adminhtml 表单?

这是我的 Adminhtml 表单 Class 以供参考。

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

我回顾了我在 Magento 1.x 中所做的一些工作,我之前让这个工作的唯一方法是使用 javascript 和 setAfterElementHtml 来设置匹配的选项。我记得我讨厌这种完成它的方法,因为它似乎是一种解决方法。

我发现了问题。 我没有在模型上保存值。

// 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

        ]
    );