保存

时间:2015-12-18 03:35:57

标签: multi-select adminhtml magento2

如何使用Multiselect获取Magento2 Adminhtml表单以选择已保存的选项?

enter image description here

这是我的Adminhtml表单类供参考。     

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来设置匹配的选项是唯一可行的方法。我记得讨厌这种让它完成的方法,因为它似乎是一种解决方法。

1 个答案:

答案 0 :(得分:2)

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

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

        ]
    );