Magento 2 – CMS Block Names in Dropdown

adminformmagento2

I want to display CMS block names (for example, footer links, promotional banners, etc.) and display modes (for example, products only, static block only, static block and products) in the admin form dropdown.

In Magento 1, I have displayed it using the following code:

Display mode:

'values'=>Mage::getModel('catalog/category_attribute_source_mode')->getAllOptions() display mode

CMS block

'values' => Mage::getModel('catalog/category_attribute_source_page')->getAllOptions()

What is the equivalent code for the above in Magento 2 for getting the CMS block name and display modes in the admin form dropdown?

Best Answer

Magento2, also have the source Model for an eav attribute concept like Magento 1.X.

So, you can get display mod and static block using respective source model

Display Mode, you can use Magento\Catalog\Model\Category\Attribute\Source\Mode

Cms static block, you can use

and Magento\Catalog\Model\Category\Attribute\Source\Page

On __construct() function you need to inject this two classes

protected $mode; 
    protected $Staticblocks; 

    public function __construct(
        \Magento\Catalog\Model\Category\Attribute\Source\Mode $mode,
        \Magento\Catalog\Model\Category\Attribute\Source\Page $Staticblocks,

    ) {
    ...
        $this->mode = $mode;
        $this->Staticblocks = $Staticblocks;

        .....
    }

Then at your code, you can get option values & label by

  • $this->mode->getAllOptions();
  • $this->Staticblocks->getAllOptions();
Related Topic