Domanda

System Configuration

<field id="test" translate="label" type="multiselect" sortOrder="11" showInDefault="1" showInStore="1" showInWebsite="1">
 <label>Cms Page</label>
<source_model>Namespace\Module\Model\Test</source_model>
</field>

Test.php

public function toOptionArray()
    {
        $options = [
            [
                'label' => __('-- Please select --'),
                'value' => '',
            ],
            [
                'label' => __('Home Page '),
                'value' => 'cms_index_index',
            ],
            [
                'label' => __('404 Page'),
                'value' => 'cms_page_view',
            ],
            [
                'label' => __('Product'),
                'value' => 'catalog_product_view',
            ],
            [
                'label' => __('Category'),
                'value' => 'catalog_category_view',
            ],

        ];
        return $options;

How to get all pages dynamically ?

È stato utile?

Soluzione

You need to use the Magento\Cms\Model\Config\Source\Page source model.

So replace:

<source_model>Namespace\Module\Model\Test</source_model>

With:

<source_model>Magento\Cms\Model\Config\Source\Page</source_model>

Altri suggerimenti

You can use CMS config model into your system.xml to get pages dynamically.

<field id="test" translate="label" type="multiselect" sortOrder="11" showInDefault="1" showInStore="1" showInWebsite="1">
   <label>Cms Page</label>
   <source_model>Magento\Cms\Model\Config\Source\Page</source_model>
</field>

For reference you can find the below file.

\vendor\magento\module-cms\etc\adminhtml\system.xml
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top