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?

share|improve this question
up vote 2 down vote accepted

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();
share|improve this answer
    
How can I call this in form once I injected the constructor.Thanks in advance. – V.P yesterday
    
See the update..you can understand – Amit Bera yesterday
    
Thank you so much. It worked. – V.P yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.